安卓学习笔记---自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示(矩形圆角加边框)

做头像一般都是圆形或者圆角矩形,这个都是显而易见的,我觉得这位博主写的就很好了

博客地址:

http://blog.csdn.net/DickyQie/article/details/52438920


基本的已经够用了呢,貌似是没有加边框哦,但是我的需求里面是需要加一个边框的在圆角矩形上,想的办法就是在它的外面在套一个布局,通过这个shape来实现,具体效果如下:

  貌似效果还可以是吧,方法如下:

1.首先定义一个shape文件:

img_icon_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    
    <!-- 设置框内的填充色  -->
    <solid android:color="@android:color/transparent"></solid>

    <!-- 设置边框的宽度和颜色 -->
    <stroke
        android:width="@dimen/dimen1"
        android:color="#95d5b2"></stroke>

    <!-- 设置圆角 -->
    <corners android:radius="@dimen/dimen5"></corners>

    <!-- 设置内边距 -->
    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"
        ></padding>

</shape>

2.之后是博主写的自定义的ImageView

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
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.TypedValue;
import android.widget.ImageView;

/**
 * 实现圆形、圆角,椭圆等自定义图片View。
 * @author zq
 *
 */
public class  ZQImageViewRoundOval extends ImageView {

    private Paint mPaint;

    private int mWidth;

    private int mHeight;

    private int mRadius;//圆半径

    private RectF mRect;//矩形凹行大小

    private int mRoundRadius;// 圆角大小

    private BitmapShader mBitmapShader;//图形渲染

    private Matrix mMatrix;

    private int mType;// 记录是圆形还是圆角矩形

    public static final int TYPE_CIRCLE = 0;// 圆形
    public static final int TYPE_ROUND = 1;// 圆角矩形
    public static final int TYPE_OVAL = 2;//椭圆形
    public static final int DEFAUT_ROUND_RADIUS = 10;//默认圆角大小

    public ZQImageViewRoundOval(Context context) {
        this(context, null);
        // TODOAuto-generated constructor stub
    }

    public ZQImageViewRoundOval(Context context, AttributeSet attrs) {
        this(context,attrs, 0);
        // TODOAuto-generated constructor stub
    }

    public ZQImageViewRoundOval(Context context, AttributeSet attrs, int defStyle){
        super(context,attrs, defStyle);
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mMatrix = new Matrix();
        mRoundRadius = DEFAUT_ROUND_RADIUS;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODOAuto-generated method stub
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        // 如果是绘制圆形,则强制宽高大小一致
        if (mType == TYPE_CIRCLE) {
            mWidth = Math.min(getMeasuredWidth(),getMeasuredHeight());
            mRadius = mWidth / 2;
            setMeasuredDimension(mWidth, mWidth);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (null ==getDrawable()) {
            return;
        }
        setBitmapShader();
        if (mType == TYPE_CIRCLE) {
            canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
        } else if (mType == TYPE_ROUND) {
            mPaint.setColor(Color.RED);
            canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);
        }else if(mType == TYPE_OVAL){
            canvas.drawOval(mRect, mPaint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODOAuto-generated method stub
        super.onSizeChanged(w,h, oldw, oldh);
        mRect = new RectF(0,0, getWidth(), getHeight());
    }

    /**
     * 设置BitmapShader
     */
    private void setBitmapShader() {
        Drawable drawable = getDrawable();
        if (null ==drawable) {
            return;
        }
        Bitmap bitmap = drawableToBitmap(drawable);
        // 将bitmap作为着色器来创建一个BitmapShader
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale =1.0f;
        if (mType == TYPE_CIRCLE) {
            // 拿到bitmap宽或高的小值
            int bSize =Math.min(bitmap.getWidth(), bitmap.getHeight());
            scale = mWidth * 1.0f /bSize;

        } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {
            // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
            scale = Math.max(getWidth() * 1.0f/ bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
        }
        // shader的变换矩阵,我们这里主要用于放大或者缩小
        mMatrix.setScale(scale,scale);
        // 设置变换矩阵
        mBitmapShader.setLocalMatrix(mMatrix);
        mPaint.setShader(mBitmapShader);

    }

    /**
     * drawable转bitmap
     *
     * @paramdrawable
     * @return
     */
    private Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable =(BitmapDrawable) drawable;
            return bitmapDrawable.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;
    }
    /**
     * 单位dp转单位px
     */
    public int dpTodx(int dp){

        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dp,getResources().getDisplayMetrics());
    }

    public int getType(){
        return mType;
    }
    /**
     * 设置图片类型:圆形、圆角矩形、椭圆形
     * @param mType
     */
    public void setType(int mType) {
        if(this.mType !=mType){
            this.mType = mType;
            invalidate();
        }

    }
    public int getRoundRadius() {
        return mRoundRadius;
    }
    /**
     * 设置圆角大小
     * @parammRoundRadius
     */
    public void setRoundRadius(int mRoundRadius) {
        if(this.mRoundRadius !=mRoundRadius){
            this.mRoundRadius =mRoundRadius;
            invalidate();
        }

    }
}

3.布局文件定义如下:

<LinearLayout
    android:layout_width="@dimen/dimen92"
    android:layout_height="@dimen/dimen92"
    android:orientation="vertical"
    android:background="@drawable/img_icon_shape"
    android:gravity="center"
    android:layout_marginRight="@dimen/dimen10"
    >

    <com.hxlm.leying.view.ZQImageViewRoundOval
        android:id="@+id/img_info_icon"
        android:layout_width="@dimen/dimen90"
        android:layout_height="@dimen/dimen90"

         />
</LinearLayout>

4.代码如下:

private ZQImageViewRoundOval mImg_info_icon;
mImg_info_icon = (ZQImageViewRoundOval) findViewById(R.id.img_info_icon);
mImg_info_icon.setType(ZQImageViewRoundOval.TYPE_ROUND);
mImg_info_icon.setRoundRadius(10);//矩形凹行大小


@Override
protected void onResume() {
    super.onResume();
    //更新选择头像
    ArrayList<PhotoItem> sysPhotos = FileUtils.getInst().findPicsInDir(
            FileUtils.getInst().getUserInfoPhotoSavedPath());
    if(sysPhotos!=null&&sysPhotos.size()>0)
    {
        Glide.with(UserInfoActivity.this).load(sysPhotos.get(0).getImageUri()).into(mImg_info_icon);
    }
}

基本就是这样了,最后运行出来的效果如上图所示,感谢博主的无私哦


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值