Android自定義圓角ImageView
更新時間:2016年01月11日 10:49:15 作者:whyrjj3
這篇文章主要介紹了Android自定義圓角ImageView的相關資料,需要的朋友可以參考下
廢話不多說了,直接給大家貼代碼了。
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 App開發(fā)中使用RecyclerView實現Gallery畫廊的實例
這篇文章主要介紹了Android App開發(fā)中使用RecyclerView實現Gallery畫廊的實例,比普通的ListView實現的效果更為強大,需要的朋友可以參考下2016-04-04
View事件分發(fā)原理和ViewPager+ListView嵌套滑動沖突
這篇文章主要介紹了View事件分發(fā)原理和ViewPager+ListView嵌套滑動沖突,文章圍繞主題展開詳細的內容介紹,具有一定的參考價,需要的小伙伴可以參考一下2022-05-05
Android Accessibility 輔助功能簡單介紹
這篇文章主要介紹了Android Accessibility 輔助功能簡單介紹的相關資料,文字轉語音,觸覺反饋,手勢操作,軌跡球和手柄操作,需要的朋友可以參考下2016-11-11
OKhttp攔截器實現實踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實現實踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android車載多媒體開發(fā)MediaSession框架示例詳解
這篇文章主要為大家介紹了Android車載多媒體開發(fā)MediaSession框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

