图片圆形,矩形圆角

转载:http://blog.csdn.net/lmj623565791/article/details/40162163

public class CustomImageView   extends View {
private  int type;
    private static final int TYPE_CIRCLE = 0;
    private static  final int TYPE_ROUND = 1;

    //图片
    private Bitmap mSrc;
    //圆角的大小
    private int mRadius;
    //控件的宽度,高度
    private int mWidth,mHeight;
    public CustomImageView(Context context) {
        this(context,null,0);
    }

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

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyleAttr, 0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr){
                case R.styleable.CustomImageView_src:
                    mSrc = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomImageView_type:
                    type = typedArray.getInt(attr, 0);
                    break;
                case R.styleable.CustomImageView_borderRadius:
                    mRadius = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics()));
                    break;
            }
        }
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置宽度
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if(specMode == MeasureSpec.EXACTLY){
            mWidth = specSize;
        }else {
            //由图片决定的宽度
            int desireByImg = getPaddingLeft() +getPaddingRight()+mSrc.getWidth();
            if(specMode == MeasureSpec.AT_MOST){
                mWidth = Math.min(desireByImg, specSize);
            }else {
                mWidth = desireByImg;
            }
        }
        /***设置高度***/
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            mHeight = specSize;
        }else {
            int desire = getPaddingBottom()+getPaddingTop() + mSrc.getHeight();
            if(specMode == MeasureSpec.AT_MOST){
                mHeight = Math.min(desire,specSize);
            }else{
                mHeight = desire;
            }
        }
        setMeasuredDimension(mWidth,mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (type){
            //如果type_cicle绘制圆形
            case TYPE_CIRCLE:
                int min = Math.min(mWidth, mHeight);
                //长度不一致,按小的值进行压缩
                mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);
                canvas.drawBitmap(creatCircleImage(mSrc, min), 0, 0, null);
                break;
            case TYPE_ROUND:
                canvas.drawBitmap(createRoundConerImage(mSrc),0,0,null);
                break;
        }
    }

    /**
     * 根据图原长和边长绘图形图片
     */
    private Bitmap creatCircleImage(Bitmap soure,int min){
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
        //产生一个同样的画布
        Canvas canvas = new Canvas(target);
        //首先绘制圆形
        canvas.drawCircle(min / 2, min / 2, min / 2, paint);
        //使用SRC_IN,
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //绘制 图片
        canvas.drawBitmap(soure,0,0,paint);
        return target;
    }
    /**
     * 根据图添加圆角
     *
     */

    private Bitmap createRoundConerImage(Bitmap soure){
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        RectF rect = new RectF(0, 0, soure.getWidth(), soure.getHeight());
        canvas.drawRoundRect(rect,mRadius,mRadius,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(soure,0,0,paint);
        return target;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:xxl="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.testapplication.Main2Activity"
    >
  <com.example.administrator.testapplication.CustomImageView
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      xxl:borderRadius="5dp"
      xxl:src="@mipmap/a"
      xxl:type="round"
      ></com.example.administrator.testapplication.CustomImageView>
    <com.example.administrator.testapplication.CustomImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        xxl:borderRadius="5dp"
        xxl:src="@mipmap/a"
        xxl:type="circle"
        ></com.example.administrator.testapplication.CustomImageView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="borderRadius" format="dimension"/>
    <attr name="type">
        <enum name="circle" value="0"/>
        <enum name="round" value="1"/>
    </attr>
    <attr name="src" format="reference"/>
    <declare-styleable name="CustomImageView">
        <attr name="borderRadius"/>
        <attr name="type"/>
        <attr name="src"/>
    </declare-styleable>
</resources>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值