Android 使用自定义Drawable 设置圆角矩形或者圆形图片
转自 Android Drawable 那些不为人知的高效用法
本文出自:【张鸿洋的博客】 http://blog.csdn.net/lmj623565791/article/details/43752383
调用方式:
imageView.setImageDrawable(new RoundImageDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.login)));
RoundImageDrawable.java
package de.bvb.rxdemo.widget; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.Drawable; /** * 设置圆形图片,调用方式为为: * imageView.setImageDrawable(new RoundImageDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.login))); */ public class RoundImageDrawable extends Drawable { private Bitmap bitmap; private Paint paint; private RectF rectF; public RoundImageDrawable(Bitmap bitmap) { this.bitmap = bitmap; paint = new Paint(); paint.setAntiAlias(true); paint.setDither(true); paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); } @Override public void setBounds(int left, int top, int right, int bottom) { super.setBounds(left, top, right, bottom); rectF = new RectF(left, top, right, bottom); } @Override public void draw(Canvas canvas) { canvas.drawRoundRect(rectF, 40, 40, paint); } @Override public int getIntrinsicHeight() { return bitmap.getHeight(); } @Override public int getIntrinsicWidth() { return bitmap.getWidth(); } @Override public void setAlpha(int alpha) { paint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter colorFilter) { paint.setColorFilter(colorFilter); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } }
CircleImageDrawable.java
package de.bvb.rxdemo.widget; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Shader; import android.graphics.drawable.Drawable; /** * 设置圆形图片,调用方式为为: * imageView.setImageDrawable(new CircleImageDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.login))); */ public class CircleImageDrawable extends Drawable { private Bitmap bitmap; private Paint paint; private int radius; public CircleImageDrawable(Bitmap bitmap) { this.bitmap = bitmap; paint = new Paint(); paint.setAntiAlias(true); paint.setDither(true); paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2; } @Override public int getIntrinsicWidth() { return radius * 2; } @Override public int getIntrinsicHeight() { return radius * 2; } @Override public void draw(Canvas canvas) { canvas.drawCircle(radius, radius, radius, paint); } @Override public void setAlpha(int alpha) { paint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter colorFilter) { paint.setColorFilter(colorFilter); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } }