码迷,mamicode.com
首页 > 移动开发 > 详细

[转]android 自定义圆形imageview控件

时间:2015-10-22 22:36:10      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

 

 

首先,定义定义圆形Imageview类:

[java] view plaincopy
 
  1. import android.content.Context;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Bitmap.Config;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.PorterDuff.Mode;  
  9. import android.graphics.PorterDuffXfermode;  
  10. import android.graphics.Rect;  
  11. import android.graphics.drawable.BitmapDrawable;  
  12. import android.graphics.drawable.Drawable;  
  13. import android.util.AttributeSet;  
  14. import android.widget.ImageView;  
  15.   
  16. public class RoundImageView extends ImageView {  
  17.   
  18. public RoundImageView(Context context) {  
  19.     super(context);  
  20.     // TODO Auto-generated constructor stub  
  21. }  
  22.   
  23. public RoundImageView(Context context, AttributeSet attrs) {  
  24.     super(context, attrs);  
  25. }  
  26.   
  27. public RoundImageView(Context context, AttributeSet attrs, int defStyle) {  
  28.     super(context, attrs, defStyle);  
  29. }  
  30.   
  31. @Override  
  32. protected void onDraw(Canvas canvas) {  
  33.   
  34.     Drawable drawable = getDrawable();  
  35.   
  36.     if (drawable == null) {  
  37.         return;  
  38.     }  
  39.   
  40.     if (getWidth() == 0 || getHeight() == 0) {  
  41.         return;   
  42.     }  
  43.       
  44.     Bitmap b =  ((BitmapDrawable)drawable).getBitmap();  
  45.       
  46.     if(null == b)  
  47.     {  
  48.         return;  
  49.     }  
  50.       
  51.     Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);  
  52.   
  53.     int w = getWidth(), h = getHeight();  
  54.   
  55.   
  56.     Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);  
  57.     canvas.drawBitmap(roundBitmap, 0,0, null);  
  58.   
  59. }  
  60.   
  61. public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {  
  62.     Bitmap sbmp;  
  63.     if(bmp.getWidth() != radius || bmp.getHeight() != radius)  
  64.         sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);  
  65.     else  
  66.         sbmp = bmp;  
  67.     Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),  
  68.             sbmp.getHeight(), Config.ARGB_8888);  
  69.     Canvas canvas = new Canvas(output);  
  70.   
  71.     final int color = 0xffa19774;  
  72.     final Paint paint = new Paint();  
  73.     final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());  
  74.   
  75.     paint.setAntiAlias(true);  
  76.     paint.setFilterBitmap(true);  
  77.     paint.setDither(true);  
  78.     canvas.drawARGB(0, 0, 0, 0);  
  79.     paint.setColor(Color.parseColor("#BAB399"));  
  80.     canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,  
  81.             sbmp.getWidth() / 2+0.1f, paint);  
  82.     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  83.     canvas.drawBitmap(sbmp, rect, rect, paint);  
  84.   
  85.   
  86.             return output;  
  87. }  
  88.   
  89. }  


然后在别的布局文件中使用该控件即可,如:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/side_right"  
  6.     android:gravity="center"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:gravity="center"  
  13.         android:layout_marginTop="35dip"  
  14.         android:orientation="vertical" >  
  15.   
  16.         <<span style="color:#ff0000;">com.founder.reader.view.RoundImageView</span>  
  17.             android:id="@+id/right_login_head"  
  18.             android:layout_width="60dip"  
  19.             android:layout_height="60dip"  
  20.             android:scaleType="centerInside"  
  21.             android:src="@drawable/user" />  

[转]android 自定义圆形imageview控件

标签:

原文地址:http://www.cnblogs.com/ZhuRenWang/p/4903008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!