Android 自定义圆角ImageView

本文介绍了一个自定义的Android圆角图片视图类,通过调整圆角宽度和高度来实现图片的圆角效果,并提供了XML配置方式。通过实例展示了如何在布局中使用此自定义视图,并附上了实现代码和使用示例。

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

[java]  view plain copy
  1.   
[java]  view plain copy
  1. java类如下:  
[java]  view plain copy
  1.   
[java]  view plain copy
  1. import android.content.Context;  
  2. import android.content.res.TypedArray;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Bitmap.Config;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Path;  
  9. 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;  
  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.         drawLiftUp(canvas2);  
  65.         drawRightUp(canvas2);  
  66.         drawLiftDown(canvas2);  
  67.         drawRightDown(canvas2);  
  68.         canvas.drawBitmap(bitmap, 00, paint2);  
  69.         bitmap.recycle();  
  70.     }  
  71.       
  72.     private void drawLiftUp(Canvas canvas) {  
  73.         Path path = new Path();  
  74.         path.moveTo(0, roundHeight);  
  75.         path.lineTo(00);  
  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 drawLiftDown(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()), 090);  
  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. }  
定义一个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>
使用示例如下:
先要声明属性的名字空间:
然后再写跟一般定义View一样:
<cn.dotcreate.tt.ui.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、付费专栏及课程。

余额充值