ImageView 只再左上右上畫角度

1,先建立一個Util類


public class RoundishImageViewextends android.support.v7.widget.AppCompatImageView {

public static final int CORNER_NONE =0;

public static final int CORNER_TOP_LEFT =1;

public static final int CORNER_TOP_RIGHT =2;

public static final int CORNER_BOTTOM_RIGHT =4;

public static final int CORNER_BOTTOM_LEFT =8;

public static final int CORNER_ALL =15;

private static final int[]CORNERS = {CORNER_TOP_LEFT,

CORNER_TOP_RIGHT,

CORNER_BOTTOM_RIGHT,

CORNER_BOTTOM_LEFT};

private final Pathpath =new Path();

private int cornerRadius;

private int roundedCorners;

public RoundishImageView(Context context) {

this(context,null);

}

public RoundishImageView(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public RoundishImageView(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundishImageView);

cornerRadius = a.getDimensionPixelSize(R.styleable.RoundishImageView_cornerRadius,0);

roundedCorners = a.getInt(R.styleable.RoundishImageView_roundedCorners,CORNER_NONE);

a.recycle();

}

public int getCornerRadius() {

return cornerRadius;

}

public void setCornerRadius(int radius) {

if (cornerRadius != radius) {

cornerRadius = radius;

setPath();

invalidate();

}

}

public void setRoundedCorners(int corners) {

if (roundedCorners != corners) {

roundedCorners = corners;

setPath();

invalidate();

}

}

public boolean isCornerRounded(int corner) {

return (roundedCorners & corner) == corner;

}

@Override

? ? protected void onDraw(Canvas canvas) {

if (!path.isEmpty()) {

canvas.clipPath(path);

}

super.onDraw(canvas);

}

@Override

? ? protected void onSizeChanged(int w,int h,int oldw,int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

setPath();

}

private void setPath() {

path.rewind();

if (cornerRadius >=1f &&roundedCorners !=CORNER_NONE) {

final float[] radii =new float[8];

for (int i =0; i <4; i++) {

if (isCornerRounded(CORNERS[i])) {

radii[2 * i] =cornerRadius;

radii[2 * i +1] =cornerRadius;

}

}

path.addRoundRect(new RectF(0,0, getWidth(), getHeight()),

radii, Path.Direction.CW);

}

}

}

2,第二布加入

<declare-styleable name="RoundishImageView">

? ? ? ? <attr name="cornerRadius" format="dimension" />

? ? ? ? <attr name="roundedCorners">

? ? ? ? ? ? <flag name="topLeft" value="1" />

? ? ? ? ? ? <flag name="topRight" value="2" />

? ? ? ? ? ? <flag name="bottomRight" value="4" />

? ? ? ? ? ? <flag name="bottomLeft" value="8" />

? ? ? ? ? ? <flag name="all" value="15" />

? ? ? ? </attr>

? ? </declare-styleable>

第三步就可以用了。

<a.b.ImageView

? ? android:id="@+id/service_image"

? ? android:layout_width="94dp"

? ? android:layout_height="94dp"

? ? android:scaleType="centerCrop"

? ? android:src="@mipmap/bg_center_top"

? ? app:cornerRadius="5dp"

? ? app:roundedCorners="topLeft|topRight" />

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容