Android自定义圆角ImageView

所属分类: 软件编程 / Android 阅读数: 660
收藏 0 赞 0 分享

废话不多说了,直接给大家贴代码了。

java类如下:

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.Path; 
import android.graphics.PorterDuff; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.RectF; 
import android.util.AttributeSet; 
import android.widget.ImageView; 
import cn.dotcreate.tt.R; 
public class RoundAngleImageView extends ImageView { 
private Paint paint; 
private int roundWidth = 5; 
private int roundHeight = 5; 
private Paint paint2; 
public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) { 
super(context, attrs, defStyle); 
init(context, attrs); 
} 
public RoundAngleImageView(Context context, AttributeSet attrs) { 
super(context, attrs); 
init(context, attrs); 
} 
public RoundAngleImageView(Context context) { 
super(context); 
init(context, null); 
} 
private void init(Context context, AttributeSet attrs) { 
if(attrs != null) { 
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView); 
roundWidth= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth); 
roundHeight= a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight); 
}else { 
float density = context.getResources().getDisplayMetrics().density; 
roundWidth = (int) (roundWidth*density); 
roundHeight = (int) (roundHeight*density); 
} 
paint = new Paint(); 
paint.setColor(Color.WHITE); 
paint.setAntiAlias(true); 
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); 
paint2 = new Paint(); 
paint2.setXfermode(null); 
} 
@Override 
public void draw(Canvas canvas) { 
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); 
Canvas canvas2 = new Canvas(bitmap); 
super.draw(canvas2); 
drawLiftUp(canvas2); 
drawRightUp(canvas2); 
drawLiftDown(canvas2); 
drawRightDown(canvas2); 
canvas.drawBitmap(bitmap, 0, 0, paint2); 
bitmap.recycle(); 
} 
private void drawLiftUp(Canvas canvas) { 
Path path = new Path(); 
path.moveTo(0, roundHeight); 
path.lineTo(0, 0); 
path.lineTo(roundWidth, 0); 
path.arcTo(new RectF( 
0, 
0, 
roundWidth*2, 
roundHeight*2), 
-90, 
-90); 
path.close(); 
canvas.drawPath(path, paint); 
} 
private void drawLiftDown(Canvas canvas) { 
Path path = new Path(); 
path.moveTo(0, getHeight()-roundHeight); 
path.lineTo(0, getHeight()); 
path.lineTo(roundWidth, getHeight()); 
path.arcTo(new RectF( 
0, 
getHeight()-roundHeight*2, 
0+roundWidth*2, 
getHeight()), 
90, 
90); 
path.close(); 
canvas.drawPath(path, paint); 
} 
private void drawRightDown(Canvas canvas) { 
Path path = new Path(); 
path.moveTo(getWidth()-roundWidth, getHeight()); 
path.lineTo(getWidth(), getHeight()); 
path.lineTo(getWidth(), getHeight()-roundHeight); 
path.arcTo(new RectF( 
getWidth()-roundWidth*2, 
getHeight()-roundHeight*2, 
getWidth(), 
getHeight()), 0, 90); 
path.close(); 
canvas.drawPath(path, paint); 
} 
private void drawRightUp(Canvas canvas) { 
Path path = new Path(); 
path.moveTo(getWidth(), roundHeight); 
path.lineTo(getWidth(), 0); 
path.lineTo(getWidth()-roundWidth, 0); 
path.arcTo(new RectF( 
getWidth()-roundWidth*2, 
0, 
getWidth(), 
0+roundHeight*2), 
-90, 
90); 
path.close(); 
canvas.drawPath(path, paint); 
} 
} 

定义一个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" />

效果如图:

以上代码简单介绍了Android自定义圆角ImageView的相关知识,希望本文分享对大家有所帮助。

更多精彩内容其他人还在看

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多