自定义View之绘制圆形控件
1. 创建一个类继承自View
1 package com.bwf.view_05; 2 3 import android.content.Context; 4 5 import android.graphics.Bitmap; 6 import android.graphics.Bitmap.Config; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.graphics.PorterDuff.Mode; 11 import android.graphics.PorterDuffXfermode; 12 import android.graphics.Rect; 13 import android.graphics.drawable.BitmapDrawable; 14 import android.graphics.drawable.Drawable; 15 import android.util.AttributeSet; 16 import android.widget.ImageView; 17 18 public class MyCircleImageView extends ImageView { 19 20 public MyCircleImageView(Context context) { 21 super(context); 22 // TODO Auto-generated constructor stub 23 } 24 25 public MyCircleImageView(Context context, AttributeSet attrs) { 26 super(context, attrs); 27 } 28 29 public MyCircleImageView(Context context, AttributeSet attrs, int defStyle) { 30 super(context, attrs, defStyle); 31 } 32 33 @Override 34 protected void onDraw(Canvas canvas) { 35 36 Drawable drawable = getDrawable(); 37 38 if (drawable == null) { 39 return; 40 } 41 42 if (getWidth() == 0 || getHeight() == 0) { 43 return; 44 } 45 46 Bitmap b = ((BitmapDrawable) drawable).getBitmap(); 47 48 if (null == b) { 49 return; 50 } 51 52 Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 53 54 int w = getWidth(), h = getHeight(); 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(), sbmp.getHeight(), 68 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, 81 sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint); 82 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 83 canvas.drawBitmap(sbmp, rect, rect, paint); 84 85 return output; 86 } 87 88 }
2.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.bwf.view_05.MyCircleImageView android:id="@+id/iv" android:layout_width="150dp" android:layout_height="150dp" android:layout_centerInParent="true" android:src="@drawable/ldh" /> </RelativeLayout>
3.activity
1 package com.bwf.view_05; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 7 public class MainActivity extends Activity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 } 14 15 @Override 16 public boolean onCreateOptionsMenu(Menu menu) { 17 // Inflate the menu; this adds items to the action bar if it is present. 18 getMenuInflater().inflate(R.menu.main, menu); 19 return true; 20 } 21 22 }
浙公网安备 33010602011771号