android自定义一圆角ImageView

本文介绍了一种自定义的圆角ImageView控件实现方式,通过定义不同角度的圆角来灵活设置ImageView的显示效果。该控件支持通过XML属性自定义圆角的宽度和高度。

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

原文链接:http://blog.youkuaiyun.com/whyrjj3/article/details/7975480#

JAVA 类:

<span style="font-family:KaiTi_GB2312;font-size:18px;">01.import android.content.Context;  
02.import android.content.res.TypedArray;  
03.import android.graphics.Bitmap;  
04.import android.graphics.Bitmap.Config;  
05.import android.graphics.Canvas;  
06.import android.graphics.Color;  
07.import android.graphics.Paint;  
08.import android.graphics.Path;  
09.import android.graphics.PorterDuff;  
10.import android.graphics.PorterDuffXfermode;  
11.import android.graphics.RectF;  
12.import android.util.AttributeSet;  
13.import android.widget.ImageView;  
14.import cn.dotcreate.tt.R;  //替换为你的R文件所在路径
15.  
16.public class RoundAngleImageView extends ImageView {  
17.  
18.    private Paint paint;  
19.    private int roundWidth = 5;  
20.    private int roundHeight = 5;  
21.    private Paint paint2;  
22.  
23.    public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {  
24.        super(context, attrs, defStyle);  
25.        init(context, attrs);  
26.    }  
27.  
28.    public RoundAngleImageView(Context context, AttributeSet attrs) {  
29.        super(context, attrs);  
30.        init(context, attrs);  
31.    }  
32.  
33.    public RoundAngleImageView(Context context) {  
34.        super(context);  
35.        init(context, null);  
36.    }  
37.      
38.    private void init(Context context, AttributeSet attrs) {  
39.          
40.        if(attrs != null) {     
41.            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);   
42.            roundWidth= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);  
43.            roundHeight= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);  
44.        }else {  
45.            float density = context.getResources().getDisplayMetrics().density;  
46.            roundWidth = (int) (roundWidth*density);  
47.            roundHeight = (int) (roundHeight*density);  
48.        }   
49.          
50.        paint = new Paint();  
51.        paint.setColor(Color.WHITE);  
52.        paint.setAntiAlias(true);  
53.        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));  
54.          
55.        paint2 = new Paint();  
56.        paint2.setXfermode(null);  
57.    }  
58.      
59.    @Override  
60.    public void draw(Canvas canvas) {  
61.        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);  
62.        Canvas canvas2 = new Canvas(bitmap);  
63.        super.draw(canvas2);  
64.        drawLeftUp(canvas2);  
65.        drawRightUp(canvas2);  
66.        drawLeftDown(canvas2);  
67.        drawRightDown(canvas2);  
68.        canvas.drawBitmap(bitmap, 0, 0, paint2);  
69.        bitmap.recycle();  
70.    }  
71.      
72.    private void drawLeftUp(Canvas canvas) {  
73.        Path path = new Path();  
74.        path.moveTo(0, roundHeight);  
75.        path.lineTo(0, 0);  
76.        path.lineTo(roundWidth, 0);  
77.        path.arcTo(new RectF(  
78.                0,   
79.                0,   
80.                roundWidth*2,   
81.                roundHeight*2),   
82.                -90,   
83.                -90);  
84.        path.close();  
85.        canvas.drawPath(path, paint);  
86.    }  
87.      
88.    private void drawLeftDown(Canvas canvas) {  
89.        Path path = new Path();  
90.        path.moveTo(0, getHeight()-roundHeight);  
91.        path.lineTo(0, getHeight());  
92.        path.lineTo(roundWidth, getHeight());  
93.        path.arcTo(new RectF(  
94.                0,   
95.                getHeight()-roundHeight*2,   
96.                0+roundWidth*2,   
97.                getHeight()),  
98.                90,   
99.                90);  
100.        path.close();  
101.        canvas.drawPath(path, paint);  
102.    }  
103.      
104.    private void drawRightDown(Canvas canvas) {  
105.        Path path = new Path();  
106.        path.moveTo(getWidth()-roundWidth, getHeight());  
107.        path.lineTo(getWidth(), getHeight());  
108.        path.lineTo(getWidth(), getHeight()-roundHeight);  
109.        path.arcTo(new RectF(  
110.                getWidth()-roundWidth*2,   
111.                getHeight()-roundHeight*2,   
112.                getWidth(),   
113.                getHeight()), 0, 90);  
114.        path.close();  
115.        canvas.drawPath(path, paint);  
116.    }  
117.      
118.    private void drawRightUp(Canvas canvas) {  
119.        Path path = new Path();  
120.        path.moveTo(getWidth(), roundHeight);  
121.        path.lineTo(getWidth(), 0);  
122.        path.lineTo(getWidth()-roundWidth, 0);  
123.        path.arcTo(new RectF(  
124.                getWidth()-roundWidth*2,   
125.                0,   
126.                getWidth(),   
127.                0+roundHeight*2),   
128.                -90,   
129.                90);  
130.        path.close();  
131.        canvas.drawPath(path, paint);  
132.    }  
133.  
134.} </span>
定义一个attr.xml的文件,放在values目录下面,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundAngleImageView">
<attr name="roundWidth" format="dimension" />
<attr name="roundHeight" format="dimension" />
</declare-styleable>
</resources>

使用示例如下:
使用时先要声明属性的名字空间:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/你的包名"

然后再写跟一般定义View一样:
<xxx.xxx.xxx.RoundAngleImageView
android:id="@+id/headIV"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
app:roundWidth="10dp"
app:roundHeight="10dp"
android:src="@drawable/default_head_icon" />


效果图就不粘了,在此基础上可以定义的更为灵活,比如左上角,左下角,右上角,右下角。这样就可以想倒那个角就倒那个角。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值