Android 自定义形状ImageView

使用方法:在xml文件中,在src中传入形状的图片,然后再代码中调用setImageBitmap(bitmap)方法,传入显示的bitmap,
xml中,layout_width和layout_height属性不能为full_parent

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * Created by huangqiyu on 2016/8/19.
 */
public class CustomShapeImageView extends ImageView {
    Context mContext;
    Bitmap mBitmapShape;
    private int mWidth;
    private int mHeight;
    private Paint paint;
    private Bitmap mBitmap;
    private Matrix mMatrixShape;
    private Matrix mMatrixPic;


    public CustomShapeImageView(Context context) {
        this(context, null);
    }

    public CustomShapeImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomShapeImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        mMatrixShape = new Matrix();
        mMatrixPic = new Matrix();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        setBitmapByExactly(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBitmapShape == null) {
            super.onDraw(canvas);
        } else {
            canvas.drawBitmap(mBitmapShape, 0, 0, null);
        }
    }

    public void setImageBitmap(Bitmap bitmap) {
        mBitmap = bitmap;
    }

    /**
     * xml中width和height设置为warp_parent或具体数值时
     *
     * @param bitmap 设置显示的Bitmap
     */
    private void setBitmapByExactly(Bitmap bitmap) {
        Bitmap bitmapPic = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        BitmapDrawable bitmapDrawable = (BitmapDrawable) getDrawable();
        mBitmapShape = bitmapDrawable.getBitmap().copy(Bitmap.Config.ARGB_8888, true);

        float scaleShapeWidth = mWidth / ((float) mBitmapShape.getWidth());
        float scaleShapeHeight = mHeight / ((float) mBitmapShape.getHeight());
        float scalePicWidth = mWidth / ((float) bitmapPic.getWidth());
        float scalePicHeight = mHeight / ((float) bitmapPic.getHeight());

        mMatrixShape.postScale(scaleShapeWidth, scaleShapeHeight);
        mMatrixPic.postScale(scalePicWidth, scalePicHeight);
        mBitmapShape = Bitmap.createBitmap(mBitmapShape, 0, 0, mBitmapShape.getWidth(), mBitmapShape.getHeight(), mMatrixShape, true);
        Bitmap bitmapPicResize = Bitmap.createBitmap(bitmapPic, 0, 0, bitmapPic.getWidth(), bitmapPic.getHeight(), mMatrixPic, true);

        Canvas canvas = new Canvas(mBitmapShape);
        canvas.drawBitmap(bitmapPicResize, 0, 0, paint);
        canvas.save();

        invalidate();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值