Android 圆形头像(2)

来源:互联网 发布:foxit pdf editor mac 编辑:程序博客网 时间:2024/06/15 16:30
package com.example.customview;


import java.lang.ref.WeakReference;


import com.example.demolibrary.R;
import android.annotation.SuppressLint;
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.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;


/**
 * xml
 * @author zlx
 *<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:zhy="http://schemas.android.com/apk/res/com.zhy.variousshapeimageview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >




        <ImageView
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_margin="10dp"
            android:src="@drawable/qiqiu" >
        </ImageView>
        <com.zhy.view.RoundImageViewByXfermode
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_margin="10dp"
            android:src="@drawable/qiqiu" >
        </com.zhy.view.RoundImageViewByXfermode>


        
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:src="@drawable/aa"
            >
        </ImageView>
        <com.zhy.view.RoundImageViewByXfermode
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:src="@drawable/aa"
            zhy:borderRadius="30dp"
            zhy:type="round" >
        </com.zhy.view.RoundImageViewByXfermode>


        
        
        <com.zhy.view.RoundImageViewByXfermode
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:src="@drawable/aa"
            zhy:type="circle" >
        </com.zhy.view.RoundImageViewByXfermode>


        <com.zhy.view.RoundImageViewByXfermode
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="10dp"
            android:src="@drawable/aa"
            zhy:type="circle" >
        </com.zhy.view.RoundImageViewByXfermode>


    </LinearLayout>


</ScrollView>
 */
public class RoundImageViewByXfermode extends ImageView
{


private Paint mPaint;
private Xfermode mXfermode = new PorterDuffXfermode(Mode.DST_IN);
private Bitmap mMaskBitmap;


private WeakReference<Bitmap> mWeakBitmap;


/**
* 鍥剧墖鐨勭被鍨嬶紝鍦嗗舰or鍦嗚
*/
private int type;
public static final int TYPE_CIRCLE = 0;
public static final int TYPE_ROUND = 1;
/**
* 鍦嗚澶у皬鐨勯粯璁わ拷??
*/
private static final int BODER_RADIUS_DEFAULT = 10;
/**
* 鍦嗚鐨勫ぇ锟�?
*/
private int mBorderRadius;


public RoundImageViewByXfermode(Context context)
{
this(context,null);


mPaint = new Paint();
mPaint.setAntiAlias(true);
}


public RoundImageViewByXfermode(Context context, AttributeSet attrs)
{
super(context, attrs);


mPaint = new Paint();
mPaint.setAntiAlias(true);


TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.RoundImageViewByXfermode);


mBorderRadius = a.getDimensionPixelSize(
R.styleable.RoundImageViewByXfermode_borderRadius, (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
BODER_RADIUS_DEFAULT, getResources()
.getDisplayMetrics()));// 榛樿锟�?10dp
Log.e("TAG", mBorderRadius+"");
type = a.getInt(R.styleable.RoundImageViewByXfermode_type, TYPE_CIRCLE);// 榛樿涓篊ircle


a.recycle();
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);


/**
* 濡傛灉绫诲瀷鏄渾褰紝鍒欏己鍒舵敼鍙榲iew鐨勫楂樹竴鑷达紝浠ュ皬鍊间负锟�?
*/
if (type == TYPE_CIRCLE)
{
int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
setMeasuredDimension(width, width);
}


}


@Override
public void invalidate()
{
mWeakBitmap = null;
if (mMaskBitmap != null)
{
mMaskBitmap.recycle();
mMaskBitmap = null;
}
super.invalidate();
}


@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas)
{
//鍦ㄧ紦瀛樹腑鍙栧嚭bitmap
Bitmap bitmap = mWeakBitmap == null ? null : mWeakBitmap.get();


if (null == bitmap || bitmap.isRecycled())
{
//鎷垮埌Drawable
Drawable drawable = getDrawable();
//鑾峰彇drawable鐨勫鍜岄珮
int dWidth = drawable.getIntrinsicWidth();
int dHeight = drawable.getIntrinsicHeight();


if (drawable != null)
{
//鍒涘缓bitmap
bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_8888);
float scale = 1.0f;
//鍒涘缓鐢诲竷
Canvas drawCanvas = new Canvas(bitmap);
//鎸夌収bitmap鐨勫楂橈紝浠ュ強view鐨勫楂橈紝璁$畻缂╂斁姣斾緥锛涘洜涓鸿缃殑src瀹介珮姣斾緥鍙兘鍜宨mageview鐨勫楂樻瘮渚嬩笉鍚岋紝杩欓噷鎴戜滑涓嶅笇鏈涘浘鐗囧け鐪燂紱
if (type == TYPE_ROUND)
{
// 濡傛灉鍥剧墖鐨勫鎴栵拷?锟介珮涓巚iew鐨勫楂樹笉鍖归厤锛岃绠楀嚭锟�?瑕佺缉鏀剧殑姣斾緥锛涚缉鏀惧悗鐨勫浘鐗囩殑瀹介珮锛屼竴瀹氳澶т簬鎴戜滑view鐨勫楂橈紱锟�?浠ユ垜浠繖閲屽彇澶э拷?锟斤紱
scale = Math.max(getWidth() * 1.0f / dWidth, getHeight()
* 1.0f / dHeight);
} else
{
scale = getWidth() * 1.0F / Math.min(dWidth, dHeight);
}
//鏍规嵁缂╂斁姣斾緥锛岃缃産ounds锛岀浉褰撲簬缂╂斁鍥剧墖锟�?
drawable.setBounds(0, 0, (int) (scale * dWidth),
(int) (scale * dHeight));
drawable.draw(drawCanvas);
if (mMaskBitmap == null || mMaskBitmap.isRecycled())
{
mMaskBitmap = getBitmap();
}
// Draw Bitmap.
mPaint.reset();
mPaint.setFilterBitmap(false);
mPaint.setXfermode(mXfermode);
//缁樺埗褰㈢姸
drawCanvas.drawBitmap(mMaskBitmap, 0, 0, mPaint);
mPaint.setXfermode(null);
//灏嗗噯澶囧ソ鐨刡itmap缁樺埗鍑烘潵
canvas.drawBitmap(bitmap, 0, 0, null);
//bitmap缂撳瓨璧锋潵锛岄伩鍏嶆瘡娆¤皟鐢╫nDraw锛屽垎閰嶅唴锟�?
mWeakBitmap = new WeakReference<Bitmap>(bitmap);
}
}
//濡傛灉bitmap杩樺瓨鍦紝鍒欑洿鎺ョ粯鍒跺嵆锟�?
if (bitmap != null)
{
mPaint.setXfermode(null);
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);
return;
}


}
/**
* 缁樺埗褰㈢姸
* @return
*/
public Bitmap getBitmap()
{
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);


if (type == TYPE_ROUND)
{
canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),
mBorderRadius, mBorderRadius, paint);
} else
{
canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2,
paint);
}


return bitmap;
}
}
0 0