Android学习笔记(十二)--自己来实现CircleImageView

本文介绍了一种使用Android自定义View实现圆形和圆角图片的方法。通过定义样式属性和使用BitmapShader来绘制不同类型的图像,可以灵活地创建圆形或圆角效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先自定义参数

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCirCleView">
        <attr name="borderRadius"/>
        <attr name="type"/>
    </declare-styleable>
    <attr name="borderRadius" format="dimension"/>
    <attr name="type">
        <enum name="circle" value="0"/>
        <enum name="round" value="1"/>
    </attr>
</resources>

然后就开始我们的自定义View了。原理是利用BItmapShader。

package com.sp.viewtry;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;

/**
 * Created by Administrator on 2017/2/5.
 */

public class MyCirCleView extends ImageView {
    private static final String TAG = "MyCirCleView";
    private int type;
    private static final int TYPE_CIRCLE=0;
    private static final int TYPE_ROUND=1;
    private static final int BORDER_RADIUS_DEFAULT=10;
    private int mBorderRadius;
    private Paint mBitmapPaint;
    private int mRadius;
    private Matrix mMatrix;
    private BitmapShader mBitmapShader;
    private int mWidth;
    private RectF mRoundRect;
    public MyCirCleView(Context context){
        super(context);
    }
    public MyCirCleView(Context context, AttributeSet attributeSet){
        this(context,attributeSet,0);
        Log.e(TAG, "MyCirCleView: oncreate" );

    }
    public MyCirCleView(Context context,AttributeSet attributeSet,int defstyle){
        super(context,attributeSet,defstyle);
        mMatrix=new Matrix();
        mBitmapPaint=new Paint();
        mBitmapPaint.setAntiAlias(true);
        TypedArray array=context.obtainStyledAttributes(attributeSet,R.styleable.MyCirCleView);
        mBorderRadius=array.getDimensionPixelSize(R.styleable.MyCirCleView_borderRadius,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,BORDER_RADIUS_DEFAULT,
                        getResources().getDisplayMetrics()));
        type=array.getInt(R.styleable.MyCirCleView_type,TYPE_CIRCLE);
        array.recycle();
    }
    @Override
    protected void onMeasure(int WidthmeasureSpec,int HightmeasureSpec){
        super.onMeasure(WidthmeasureSpec, HightmeasureSpec);
        if (type == TYPE_CIRCLE)
        {
            mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
            mRadius = mWidth / 2;
            setMeasuredDimension(mWidth, mWidth);
        }
    }
    @Override
    protected void onDraw(Canvas canvas){
        if (getDrawable()==null){
            return;
        }
        setUpShader();
        if (type == TYPE_ROUND)
        {
            canvas.drawRoundRect(mRoundRect, mBorderRadius, mBorderRadius,
                    mBitmapPaint);
        } else
        {
            canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint);
        }
    }
    private void setUpShader(){
        Drawable drawable=getDrawable();
        if (drawable==null){
            return;
        }
        Bitmap bmp = drawableToBitamp(drawable);
        mBitmapShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale = 1.0f;
        if (type == TYPE_CIRCLE)
        {
            int bSize = Math.min(bmp.getWidth(), bmp.getHeight());
            scale = mWidth * 1.0f / bSize;

        } else if (type == TYPE_ROUND)
        {
            scale = Math.max(getWidth() * 1.0f / bmp.getWidth(), getHeight()
                    * 1.0f / bmp.getHeight());
        }
        mMatrix.setScale(scale, scale);
        mBitmapShader.setLocalMatrix(mMatrix);
        mBitmapPaint.setShader(mBitmapShader);
    }
    private Bitmap drawableToBitamp(Drawable drawable)
    {
        if (drawable instanceof BitmapDrawable)
        {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        // 圆角图片的范围
        if (type == TYPE_ROUND)
            mRoundRect = new RectF(0, 0, getWidth(), getHeight());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值