自定義圓形頭像CircleImageView的使用

效果圖:


使用:


2個(gè)類:

CircleImageView類:

package com.lzyi.tpm.utils;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.util.AttributeSet;

import io.jchat.android.view.BaseImageView;

public class CircleImageViewextends BaseImageView {

public CircleImageView(Context context) {

super(context);

? ? }

public CircleImageView(Context context, AttributeSet attrs) {

super(context, attrs);

? ? }

public CircleImageView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

? ? }

public static BitmapgetBitmap(int width, int height) {

Bitmap bitmap = Bitmap.createBitmap(width, height,

? ? ? ? ? ? ? ? Bitmap.Config.ARGB_8888);

? ? ? ? Canvas canvas =new Canvas(bitmap);

? ? ? ? Paint paint =new Paint(Paint.ANTI_ALIAS_FLAG);

? ? ? ? paint.setColor(Color.BLACK);

? ? ? ? canvas.drawOval(new RectF(0.0f, 0.0f, width, height), paint);

? ? ? ? return bitmap;

? ? }

@Override

? ? public BitmapgetBitmap() {

return getBitmap(getWidth(), getHeight());

? ? }

}

BaseImageView類:

package io.jchat.android.view;

import android.annotation.SuppressLint;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.Xfermode;

import android.graphics.drawable.Drawable;

import android.util.AttributeSet;

import android.util.Log;

import android.widget.ImageView;

import java.lang.ref.WeakReference;

public abstract class BaseImageViewextends ImageView {

private static final StringTAG = BaseImageView.class.getSimpleName();

? ? protected ContextmContext;

? ? private static final XfermodesXfermode =new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

//? ? private BitmapShader mBitmapShader;

? ? private BitmapmMaskBitmap;

? ? private PaintmPaint;

? ? private WeakReferencemWeakBitmap;

? ? public BaseImageView(Context context) {

super(context);

? ? ? ? sharedConstructor(context);

? ? }

public BaseImageView(Context context, AttributeSet attrs) {

super(context, attrs);

? ? ? ? sharedConstructor(context);

? ? }

public BaseImageView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

? ? ? ? sharedConstructor(context);

? ? }

private void sharedConstructor(Context context) {

mContext = context;

? ? ? ? mPaint =new Paint(Paint.ANTI_ALIAS_FLAG);

? ? }

public void invalidate() {

mWeakBitmap =null;

? ? ? ? if (mMaskBitmap !=null) {mMaskBitmap.recycle(); }

super.invalidate();

? ? }

@SuppressLint("DrawAllocation")

@Override

? ? protected void onDraw(Canvas canvas) {

if (!isInEditMode()) {

int i = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(),

? ? ? ? ? ? ? ? ? ? null, Canvas.ALL_SAVE_FLAG);

? ? ? ? ? ? try {

Bitmap bitmap =mWeakBitmap !=null ?mWeakBitmap.get() :null;

? ? ? ? ? ? ? ? // Bitmap not loaded.

? ? ? ? ? ? ? ? if (bitmap ==null || bitmap.isRecycled()) {

Drawable drawable = getDrawable();

? ? ? ? ? ? ? ? ? ? if (drawable !=null) {

// Allocation onDraw but it's ok because it will not always be called.

? ? ? ? ? ? ? ? ? ? ? ? bitmap = Bitmap.createBitmap(getWidth(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getHeight(), Bitmap.Config.ARGB_8888);

? ? ? ? ? ? ? ? ? ? ? ? Canvas bitmapCanvas =new Canvas(bitmap);

? ? ? ? ? ? ? ? ? ? ? ? drawable.setBounds(0, 0, getWidth(), getHeight());

? ? ? ? ? ? ? ? ? ? ? ? drawable.draw(bitmapCanvas);

? ? ? ? ? ? ? ? ? ? ? ? // If mask is already set, skip and use cached mask.

? ? ? ? ? ? ? ? ? if (mMaskBitmap ==null ||mMaskBitmap.isRecycled()) {

mMaskBitmap = getBitmap();

? ? ? ? ? ? ? ? ? }

// Draw Bitmap.

? ? ? ? ? ? ? ? ? ? ? ? mPaint.reset();

? ? ? ? ? ? ? ? ? ? ? ? mPaint.setFilterBitmap(false);

? ? ? ? ? ? ? ? ? ? ? ? mPaint.setXfermode(sXfermode);

//? ? ? ? ? ? ? ? ? ? ? ? mBitmapShader = new BitmapShader(mMaskBitmap,

//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

//? ? ? ? ? ? ? ? ? ? ? ? mPaint.setShader(mBitmapShader);

? ? ? ? ? ? ? ? ? ? ? ? bitmapCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mPaint);

? ? ? ? ? ? ? ? ? ? ? ? mWeakBitmap =new WeakReference(bitmap);

? ? ? ? ? ? ? ? ? ? }

}

// Bitmap already loaded.

? ? ? ? ? ? ? ? if (bitmap !=null) {

mPaint.setXfermode(null);

//? ? ? ? ? ? ? ? ? ? mPaint.setShader(null);

? ? ? ? ? ? ? ? ? ? canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);

return;

? ? ? ? ? ? ? ? }

}catch (Exception e) {

System.gc();

? ? ? ? ? ? ? ? Log.e(TAG, String.format("Failed to draw, Id :: %s. Error occurred :: %s", getId(), e.toString()));

? ? ? ? ? ? }finally {

canvas.restoreToCount(i);

? ? ? ? ? ? }

}else {

super.onDraw(canvas);

? ? ? ? }

}

public abstract BitmapgetBitmap();

}

?著作權(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)容

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