Android ImageView 四個角自定義角度,以及角度的變換
2023-04-12
背景:
在正常開發(fā)過程中,我們經(jīng)常會發(fā)現(xiàn)一些圖片有各種各樣的顯示,有圓角、直角、圓形、不規(guī)則圖形等?比較常見的是圓形,還有圓角。今天我們將講述圓角、四個角不同度數(shù)以及通過圓角巧妙變成圓形
1.如果大家不熟悉圓形或者path的以及canvas.clipPath,可以參考我的一篇文章:圓形頭像
2.今天我們依舊通過Canvas的畫布剪切來完成,有所不同的是,這次的path不是一個圓,而是在矩形中畫圓addRoundRect
廢話不多說:直接上代碼
public class MyRoundJiaoImageView extends AppCompatImageView {
private int leftTopRadius;
private int rightTopRadius;
private int leftBottomRadius;
private int rightBottomRadius;
private int allRadius;
public MyRoundJiaoImageView(Context context) {
super(context);
}
public MyRoundJiaoImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initAtter(context, attrs);
}
private void initAtter(Context context, AttributeSet attrs) {
TypedArray typed = context.obtainStyledAttributes(attrs, R.styleable.roundRadion);
if (typed == null)
return;
leftTopRadius = typed.getInt(R.styleable.roundRadion_mleftTopRadius, 0);
rightTopRadius = typed.getInt(R.styleable.roundRadion_mrightRadius, 0);
leftBottomRadius = typed.getInt(R.styleable.roundRadion_mleftButtomRadius, 0);
rightBottomRadius = typed.getInt(R.styleable.roundRadion_mrightButtomRadius, 0);
allRadius = typed.getInt(R.styleable.roundRadion_allRadius, 0);
typed.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
int width = getMeasuredWidth();
int height = getMeasuredHeight();
Path path = new Path();
/*向路徑中添加圓角矩形。radii數(shù)組定義圓角矩形的四個圓角的x,y半徑。radii長度必須為8*/
if (allRadius > 0) {
float rids[] = {allRadius, allRadius, allRadius, allRadius, allRadius, allRadius, allRadius, allRadius};
path.addRoundRect(new RectF(0, 0, width, height), rids, Path.Direction.CW);
} else {
float rids[] = {leftTopRadius, leftTopRadius, rightTopRadius, rightTopRadius, leftBottomRadius, leftBottomRadius, rightBottomRadius, rightBottomRadius};
path.addRoundRect(new RectF(0, 0, width, height), rids, Path.Direction.CW);
}
canvas.clipPath(path);
super.onDraw(canvas);
}
public void setAllRadius(int allRadius) {
this.allRadius = allRadius;
invalidate();
}
}
講解:
如果對畫布的操作,需要在onDraw的super之前完成,否則將不會生效
1.我們這邊是通過自定義圓角角度,如何自定義參數(shù),可參考Android View自定義參數(shù)declare-styleable介紹與使用
參數(shù)如下:
2.path的addRoundRect的使用
public void addRoundRect(RectF rect, float[] radii, Direction dir)
RectF :矩形的范圍
radii:四個角的角度參數(shù),正常需要8個值,因為我們知道一個角確定下來需要兩個角度的坐標

所以這個數(shù)組正常就是:
float rids[] = {leftTopRadius, leftTopRadius, rightTopRadius, rightTopRadius, leftBottomRadius, leftBottomRadius, rightBottomRadius, rightBottomRadius};
A.(leftTopRadius, leftTopRadius)
B(rightTopRadius, rightTopRadius)
C(leftBottomRadius, leftBottomRadius)
D(rightBottomRadius, rightBottomRadius)

坐標里面的值需一樣,否則開角不同,會導致不生效
Direction :繪制的方向
3.只要我們path路徑準備好,canvs繪制路徑即可。
說明:
1.這種剪切是支持不同角的角度值剪切,但是,圖片的畫布只是針對前景色,也就是ImageView的drawable或者src的部分,背景大小還是原來的尺寸,接下來我們將會用一組動畫說明。
2.當角度都是360度的時候,這個圖片就是圓形,我們常見的頭像做法。這種剪切方法可以剪切出任意效果,只要我們path繪制的夠完美。

本文僅代表作者觀點,版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請在文中注明來源及作者名字。
免責聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請及時與我們聯(lián)系進行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com






