中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android自定義圓角ImageView

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

我們經(jīng)常看到一些app中可以顯示圓角圖片,比如qq的聯(lián)系人圖標(biāo)等等,實(shí)現(xiàn)圓角圖片一種辦法是直接使用圓角圖片資源,當(dāng)然如果沒(méi)有圓角圖片資源,我們也可以自己通過(guò)程序?qū)崿F(xiàn)的,下面介紹一個(gè)自定義圓角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() */  
    @Override  
    public void draw(Canvas canvas) {  
          
        //實(shí)例化一個(gè)和ImageView一樣大小的bitmap  
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),  
                Config.ARGB_8888);  
          
        //實(shí)例化一個(gè)canvas,這個(gè)canvas對(duì)應(yīng)的內(nèi)存為上面的bitmap  
        Canvas canvas2 = new Canvas(bitmap);  
        if (bitmap.isRecycled()) {  
            bitmap = Bitmap.createBitmap(getWidth(), getHeight(),  
                    Config.ARGB_8888);  
            canvas2 = new Canvas(bitmap);  
        }  
          
        //將imageView自己繪制到canvas2上,這個(gè)導(dǎo)致bitmap里面存放了imageView  
        super.draw(canvas2);  
          
        //利用canvas畫一個(gè)圓角矩形,這個(gè)會(huì)修改bitmap的數(shù)據(jù)  
        drawRoundAngle(canvas2);  
          
        //將裁剪好的bitmap繪制到系統(tǒng)當(dāng)前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);  
          
        //這是設(shè)置了填充模式,非常關(guān)鍵  
        maskPath.setFillType(Path.FillType.INVERSE_WINDING);  
        canvas.drawPath(maskPath, maskPaint);  
    }  
}  

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:經(jīng)典算法5:用分治法實(shí)現(xiàn)元素選擇

下一篇: 經(jīng)典算法8:檢索與周游之廣度和深度優(yōu)先遍歷圖