android ImageView 圆角控件 设置上下左右某几个为圆角

需求描述:ImageView的上面两个角为圆角,下面两个角不变

解决方案:重写ImageView控件,在onDraw方法中,设置Path来达到目的

控件代码如下:

public class RoundRectImageView extends ImageView {

    /*左上角xy半径,右上角,右下角,左下角*/
    private float[] rids = {10.0f, 10.0f, 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f,};

    public RoundRectImageView(Context context) {
        super(context);
    }

 @Override
protected void onDraw(Canvas canvas) { Path path = new Path() ; int w = this.getWidth() ; int h = this.getHeight() ; path.addRoundRect( new RectF( 0 , 0 , w , h) , rids , Path.Direction. CW) ; canvas.clipPath(path) ; super.onDraw(canvas) ; }}


上面控件中设置的是上面两个叫为圆角,具体需求可改变对应参数来达到目的

你可能感兴趣的:(Android)