Android_自定義View_圓角頭像

效果圖如下

Paste_Image.png

原圖如下


實(shí)現(xiàn)方法:

自定義View 代碼如下(可直接Copy適用)

package com.bxlt.customstudy;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 自定義圓角頭像
 * Created by Lrxc on 2017/5/22.
 */

public class CircleIco extends View {
    private Bitmap bitmap;

    public CircleIco(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //繪制
        if (bitmap != null)
            canvas.drawBitmap(getCircleBitmap(), 0, 0, null);
    }

    // 設(shè)置bitmap
    public void setImageBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        invalidate();
    }

    //獲取圓角圖片
    public Bitmap getCircleBitmap() {
        //獲取屏幕寬高
        int w = getWidth();
        int h = getHeight();

        //新建一個(gè)位圖文件
        Bitmap newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        //在此位圖上進(jìn)行繪制
        Canvas canvas = new Canvas(newBitmap);

        //初始化畫筆
        Paint paint = new Paint();
//        paint.setStrokeWidth(5);//畫筆寬度
//        paint.setAntiAlias(true);//是否抗鋸齒
//        paint.setDither(true); //防抖動(dòng)
//        paint.setStyle(Paint.Style.FILL); //畫筆類型 STROKE空心 FILL 實(shí)心
//        paint.setColor(Color.BLUE);//畫筆顏色

        //繪制一個(gè)圓
        int radius = Math.min(w, h) / 2;//獲取寬和高的較小數(shù)
        canvas.drawCircle(w / 2, h / 2, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//圖片相交模式

        //繪制圖片底圖
        Matrix matrix = new Matrix();
//        matrix.postScale(1, 1);//不縮放,原圖顯示
        matrix.postScale((float) w / bitmap.getWidth(), (float) h / bitmap.getHeight(), 0, 0);//縮放全部顯示
        canvas.drawBitmap(bitmap, matrix, paint);
        return newBitmap;
    }

    // 測(cè)量模式
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (bitmap == null) return;

       int bw = bitmap.getWidth();
        int bh = bitmap.getHeight();

        int sizeW = MeasureSpec.getSize(widthMeasureSpec);
        int sizeH = MeasureSpec.getSize(heightMeasureSpec);
        int modeW = MeasureSpec.getMode(widthMeasureSpec);
        int modeH = MeasureSpec.getMode(heightMeasureSpec);

        if (modeW == MeasureSpec.AT_MOST)
            sizeW = bw;

        if (modeH == MeasureSpec.AT_MOST)
            sizeH = bh;

        setMeasuredDimension(sizeW, sizeH);
    }
}

使用自定義View:

布局頁面中,加上這個(gè)就行了

 <com.bxlt.customstudy.CircleIco
        android:id="@+id/customCreame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

主頁面調(diào)用

    CircleIco circleIco = (CircleIco) findViewById(R.id.customCreame);
     circleIco.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.test));

好了,就這么簡(jiǎn)單而已。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,070評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,393評(píng)論 4 61
  • 小時(shí)候,經(jīng)常聽到別人對(duì)我說,你吃飯了嗎,吃飽了嗎? 以前家里經(jīng)濟(jì)條件并不是很好,家里有什么,就吃什么,沒有...
    夢(mèng)緣_21閱讀 238評(píng)論 0 0

友情鏈接更多精彩內(nèi)容