activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:CustomImageView="http://schemas.android.com/apk/res/com.example.customview05imageview" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.example.customview05imageview.RoundImageView android:layout_width="wrap_content" android:layout_margin="5dp" android:layout_height="wrap_content" android:src="@drawable/icon" /> <ImageView android:id="@+id/imageview" android:layout_margin="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" /> <com.example.customview05imageview.view.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" CustomImageView:src="@drawable/icon" CustomImageView:type="circle" /> <com.example.customview05imageview.view.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" CustomImageView:borderRadius="10dp" CustomImageView:src="@drawable/icon" CustomImageView:type="round" /> <com.example.customview05imageview.view.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" CustomImageView:borderRadius="20dp" CustomImageView:src="@drawable/icon" CustomImageView:type="round" /> </LinearLayout> </ScrollView>
MainActivity
package com.example.customview05imageview; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imageview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview = (ImageView) findViewById(R.id.imageview); Drawable drawable = getResources().getDrawable(R.drawable.icon); BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; Bitmap bitmap = bitmapDrawable.getBitmap(); Bitmap roundCorner = toRoundCorner(bitmap, 30); imageview.setImageBitmap(roundCorner); } public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { //创建bitmap对象 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); //创建画笔、画布 Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final int color = 0xff424242; //创建矩形-位置 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); //圆角 final float roundPx = pixels; paint.setAntiAlias(true); //透明 canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } }CustomImageView
package com.example.customview05imageview.view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import com.example.customview05imageview.R; public class CustomImageView extends View { private int type; private static final int TYPE_CIRCLE = 0; private static final int TYPE_ROUND = 1; private Bitmap mSrc; private int mRadius; private int mWidth; private int mHeight; /** * 初始化一些自定义的参数 */ public CustomImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomImageView(Context context) { this(context, null); } public CustomImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0); int n = a.getIndexCount(); // 遍历属性 for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomImageView_src: mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0)); break; case R.styleable.CustomImageView_type: // 默认为Circle--0 type = a.getInt(attr, 0); break; case R.styleable.CustomImageView_borderRadius: // 转变为标准尺寸的一个函数-- 默认为10DIP mRadius = a.getDimensionPixelSize(attr, (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics())); break; } } a.recycle(); } /** * 计算控件的高度和宽度 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /** * 参数:它们指明控件可获得的空间以及关于这个空间描述的元数据 设置宽度 */ int specMode = MeasureSpec.getMode(widthMeasureSpec); int specSize = MeasureSpec.getSize(widthMeasureSpec); /** * MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip" * 或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。 */ if (specMode == MeasureSpec.EXACTLY)// match_parent { mWidth = specSize; } else { // 由图片决定的宽 int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth(); if (specMode == MeasureSpec.AT_MOST)// wrap_content { /** * 此时控件尺寸只要不超过父控件允许的最大尺寸即可 */ mWidth = Math.min(desireByImg, specSize); } else mWidth = desireByImg; } /*** * 设置高度 */ specMode = MeasureSpec.getMode(heightMeasureSpec); specSize = MeasureSpec.getSize(heightMeasureSpec); if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate { mHeight = specSize; } else { int desire = getPaddingTop() + getPaddingBottom() + mSrc.getHeight(); if (specMode == MeasureSpec.AT_MOST)// wrap_content { mHeight = Math.min(desire, specSize); } else mHeight = desire; } setMeasuredDimension(mWidth, mHeight); } /** * 绘制 */ @Override protected void onDraw(Canvas canvas) { switch (type) { case TYPE_CIRCLE: int min = Math.min(mWidth, mHeight); // Bitmap 创建 一个新的,缩放后的 Bitmap mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false); //在某个位置画圆 canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null); break; case TYPE_ROUND: canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null); break; } } /** * 根据原图和变长绘制圆形图片 */ private Bitmap createCircleImage(Bitmap source, int min) { final Paint paint = new Paint(); paint.setAntiAlias(true); //该函数创建一个带有特定宽度、高度和颜色格式的位图 Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888); //产生一个同样大小的画布 Canvas canvas = new Canvas(target); //首先绘制圆形-原心xy坐标以及半径 canvas.drawCircle(min / 2, min / 2, min / 2, paint); //取两层绘制交集。显示上层 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //绘制图片 canvas.drawBitmap(source, 0, 0, paint); return target; } /** * 根据原图添加圆角 */ private Bitmap createRoundConerImage(Bitmap source) { final Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888); Canvas canvas = new Canvas(target); RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rect, mRadius, mRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source, 0, 0, paint); return target; } }RoundImageView
package com.example.customview05imageview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.util.AttributeSet; import android.widget.ImageView; public class RoundImageView extends ImageView { public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public RoundImageView(Context context) { super(context); init(); } private void init() { maskPaint.setAntiAlias(true); maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // zonePaint.setAntiAlias(true); zonePaint.setColor(Color.RED); // float density = getResources().getDisplayMetrics().density; rect_adius = rect_adius * density; } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); int w = getWidth(); int h = getHeight(); roundRect.set(0, 0, w, h); } private final RectF roundRect = new RectF(); private float rect_adius = 20; private final Paint maskPaint = new Paint(); private final Paint zonePaint = new Paint(); @Override public void draw(Canvas canvas) { canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG); //产生一个红色的圆角矩形 canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint); // canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG); super.draw(canvas); canvas.restore(); } }