一、编写自定义iconview
package com.android.MyShapeIcon;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class MyShapeIcon extends View {
private Context mContext;
private Drawable mDrawable;
public MyShapeIcon(Context context) {
super(context);
}
public MyShapeIcon(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public MyShapeIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
public MyShapeIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
if( mDrawable != null) {
Bitmap bitmap = drawableToBitmap(mDrawable);
bitmap = getRoundCornerBitmap(bitmap,20,(int)mContext.getResources().getDimension(R.dimen.accessibility_floating_menu_small_width_height),(int)mContext.getResources().getDimension(R.dimen.accessibility_floating_menu_small_width_height));
canvas.drawBitmap(bitmap, 0, 0, paint);
}
}
public void setDrawableRes(Drawable drawable) {
mDrawable = drawable;
}
private Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
return bitmap;
}
private Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX,int desImg_width,int desImg_height){
int src_width = bitmap.getWidth();
int src_height = bitmap.getHeight();
float scale_w =(float) desImg_width/src_width;
float scale_h =(float) desImg_height/src_height;
Matrix matrix = new Matrix();
matrix.postScale(scale_w,scale_h);
bitmap = Bitmap.createBitmap(bitmap, 0,0, src_width, src_height, matrix,true);
Bitmap bitmap2 = Bitmap.createBitmap(desImg_width, desImg_height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap2);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, desImg_width, desImg_height);
final RectF rectF = new RectF(rect);
paint.setColor(color);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, roundPX, roundPX, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return bitmap2;
}
}
二、在布局中加入
<MyShapeIcon
android:id="@+id/iv_custom"
android:layout_width="40dp"
android:layout_height="40dp"/>
三、在java文件获取MyShapeIcon调用setDrawableRes()方法设置要显示的图片