自定义ImageView->圆角矩形+圆形头像

时间:2021-08-01 16:03:16

参考自:http://blog.csdn.net/lmj623565791/article/details/24555655

 

package com.example.customshapedemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CustomImgeView extends ImageView {
    public static final int circle = 0;
    public static final int round = 1;
    Paint paint = new Paint();
    private int radius = 0;
    private int type = -1;

    public CustomImgeView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomImgeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        initParam(context, attrs);
    }

    public CustomImgeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initParam(context, attrs);
    }

    void init() {
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
    }

    // 解析参数
    void initParam(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.CustomImageView);
        // 获取图片类型
        type = typedArray.getInt(0, -1);
        // 如果是圆角矩形的类型,就获取圆角的角度
        if (type == round) {
            radius = typedArray.getInt(1, 0);
        }
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Drawable drawable = getDrawable();
        if (null == drawable || type == -1) {
            super.onDraw(canvas);
            return;
        }
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        paint.reset();
        init();
        if (circle == type) {
            int r = 0;
            int left = 0, top = 0;
            if (w > h) {
                r = h / 2;
                left = (w - h) / 2;
            } else {
                r = w / 2;
                top = (h - w) / 2;
            }
            Bitmap b = getCircleBitmap(bitmap, r);
            canvas.drawBitmap(b, left, top, paint);
        } else if (round == type) {
            Bitmap b = getRoundConerBitmap(bitmap, radius);
            canvas.drawBitmap(b, 0, 0, paint);
        }
    }

    // 将Bitmap合成为一个圆角的Bitmap
    public Bitmap getCircleBitmap(Bitmap bitmap, int r) {
        Bitmap b = Bitmap.createBitmap(2 * r, 2 * r, Config.ARGB_8888);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.WHITE);
        Canvas canvas = new Canvas(b);
        // 在底层画一个半径为r的圆形
        canvas.drawCircle(r, r, r, p);
        // 设置SRC_IN模式,这种模式取两层图片叠加的并集 展现上面的那一层
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 0, 0, p);
        return b;
    }

    // 将Bitmap合成为一个圆角的Bitmap
    public Bitmap getRoundConerBitmap(Bitmap bitmap, int round) {
        Bitmap b = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Config.ARGB_8888);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.WHITE);
        Canvas canvas = new Canvas(b);
        // 在底层画一个矩形
        RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawRoundRect(rect, round, round, p);
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        // 设置SRC_IN模式,这种模式取两层图片叠加的并集 展现上面的那一层
        canvas.drawBitmap(bitmap, 0, 0, p);
        return b;
    }
}

 

在values/styles.xml中添加自定义属性:

 

<declare-styleable name="CustomImageView">
    <attr name="type">  
            <enum name="circle" value="0" />  
            <enum name="round" value="1" />  
        </attr>  
        <attr name="radius" format="integer" />
</declare-styleable>

xml文件中定义使用自定义类 引入namespace,以及用报名+类名的形式使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:customshape="http://schemas.android.com/apk/res/com.example.customshapedemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.example.customshapedemo.CustomImgeView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="44dp"
        customshape:type="round"
        customshape:radius="15"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/ddw1" >
    </com.example.customshapedemo.CustomImgeView>

    <com.example.customshapedemo.CustomImgeView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="119dp"
        android:src="@drawable/ddw1"
        customshape:type="circle" />

</RelativeLayout>