美文网首页
ImageView 只再左上右上画角度

ImageView 只再左上右上画角度

作者: 今天天气很好我心情也很好 | 来源:发表于2018-11-21 16:07 被阅读0次

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" />

相关文章

网友评论

      本文标题:ImageView 只再左上右上画角度

      本文链接:https://www.haomeiwen.com/subject/ymycqqtx.html