Android自己定義圓角ImageView

來源:互聯網
上載者:User

標籤:article   介紹   圓角   long   view   float   聯絡   int   creat   

我們常常看到一些app中能夠顯示圓角圖片。比方qq的連絡人表徵圖等等,實現圓角圖片一種辦法是直接使用圓角圖片資源,當然假設沒有圓角圖片資源。我們也能夠自己通過程式實現的,以下介紹一個自己定義圓角ImageView的方法:

package com.yulongfei.imageview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.RectF;import android.util.AttributeSet;import android.widget.ImageView;public class RoundAngleImageView extends ImageView {private int roundWidth = 13;private int roundHeight = 13;public RoundAngleImageView(Context context) {super(context);init(context, null);}public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(context, attrs);}public RoundAngleImageView(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs);}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);a.recycle();} else {float density = context.getResources().getDisplayMetrics().density;roundWidth = (int)(roundWidth * density);roundHeight = (int)(roundHeight * density);}}/** 重寫draw() */@Overridepublic void draw(Canvas canvas) {//執行個體化一個和ImageView一樣大小的bitmapBitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),Config.ARGB_8888);//執行個體化一個canvas,這個canvas相應的記憶體為上面的bitmapCanvas canvas2 = new Canvas(bitmap);if (bitmap.isRecycled()) {bitmap = Bitmap.createBitmap(getWidth(), getHeight(),Config.ARGB_8888);canvas2 = new Canvas(bitmap);}//將imageView自己繪製到canvas2上,這個導致bitmap裡面存放了imageViewsuper.draw(canvas2);//利用canvas畫一個圓角矩形,這個會改動bitmap的資料drawRoundAngle(canvas2);//將裁剪好的bitmap繪製到系統當前canvas上,這樣裁剪好的imageview就能顯示到螢幕上Paint paint = new Paint();paint.setXfermode(null);canvas.drawBitmap(bitmap, 0, 0, paint);bitmap.recycle();}public void setRoundWidth(int roundWidth, int roundHeight) {this.roundWidth = roundWidth;this.roundHeight = roundHeight;}private void drawRoundAngle(Canvas canvas){Paint maskPaint = new Paint();        maskPaint.setAntiAlias(true);maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));        Path maskPath = new Path();          maskPath.addRoundRect(new RectF(0.0F, 0.0F, getWidth(), getHeight()), roundWidth, roundHeight, Path.Direction.CW);                //這是設定了填充模式。很關鍵        maskPath.setFillType(Path.FillType.INVERSE_WINDING);        canvas.drawPath(maskPath, maskPaint);}}

Android自己定義圓角ImageView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.