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);}}上面控制元件中設定的是上面兩個叫為圓角,具體需求可改變對應引數來達到目的