android 圓角ImageView類,可設定弧度,androidimageview

來源:互聯網
上載者:User

android 圓角ImageView類,可設定弧度,androidimageview

public class RoundImageView extends ImageView {    private Paint paint;    private int roundWidth = 50;    private int roundHeight = 50;    private Paint paint2;    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(context, attrs);    }    public RoundImageView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context, attrs);    }    public void SetRoundValue(float roundValue) {        roundWidth = (int) roundValue;        roundHeight = (int) roundValue;    }    public RoundImageView(Context context) {        super(context);        init(context, null);    }    @SuppressLint("Recycle")    private void init(Context context, AttributeSet attrs) {        if (attrs != null) {            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);            roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);            roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);        } else {            float density = context.getResources().getDisplayMetrics().density;            roundWidth = (int) (roundWidth * density);            roundHeight = (int) (roundHeight * density);        }        paint = new Paint();        paint.setColor(Color.WHITE);        paint.setAntiAlias(true);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));        paint2 = new Paint();        paint2.setXfermode(null);    }    @Override    public void draw(Canvas canvas) {        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);        Canvas canvas2 = new Canvas(bitmap);        super.draw(canvas2);        drawLiftUp(canvas2);        drawRightUp(canvas2);        drawLiftDown(canvas2);        drawRightDown(canvas2);        canvas.drawBitmap(bitmap, 0, 0, paint2);        bitmap.recycle();        bitmap = null;    }    private void drawLiftUp(Canvas canvas) {        Path path = new Path();        path.moveTo(0, roundHeight);        path.lineTo(0, 0);        path.lineTo(roundWidth, 0);        path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);        path.close();        canvas.drawPath(path, paint);    }    private void drawLiftDown(Canvas canvas) {        Path path = new Path();        path.moveTo(0, getHeight() - roundHeight);        path.lineTo(0, getHeight());        path.lineTo(roundWidth, getHeight());        path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90);        path.close();        canvas.drawPath(path, paint);    }    private void drawRightDown(Canvas canvas) {        Path path = new Path();        path.moveTo(getWidth() - roundWidth, getHeight());        path.lineTo(getWidth(), getHeight());        path.lineTo(getWidth(), getHeight() - roundHeight);        path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0,                90);        path.close();        canvas.drawPath(path, paint);    }    private void drawRightUp(Canvas canvas) {        Path path = new Path();        path.moveTo(getWidth(), roundHeight);        path.lineTo(getWidth(), 0);        path.lineTo(getWidth() - roundWidth, 0);        path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);        path.close();        canvas.drawPath(path, paint);    }}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.