按自定义形状显示图片View

该博客介绍了一个自定义视图`MyShapeIcon`,用于将Drawable转换为带有圆角的Bitmap并显示。在布局中添加此视图,并在Java代码中设置要显示的图片资源,可以创建具有圆形边角的图标。博客内容涵盖了Android自定义视图的绘制方法,包括Bitmap的处理和图形变换。
摘要由CSDN通过智能技术生成

一、编写自定义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()方法设置要显示的图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值