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o/v0NjIJeqfZtE1sdHhO14058/m2QTBJ/fMhMzH5Mx99Hp3+HV4482AEzsemaj+Zph8zE9O52eDk8hpCw1S1hgu3QzATbNjPBts1MsG0zE2zbzATbNjPBts1MsG0zE2zbzATbNjPBts1MsG0zE2zbzATbNjPBtv0fqEaE/39lDLoAAAAASUVORK5CYII=" alt="" />
2. 自定义的控件类
package com.chaowen.yixin; import android.content.Context;
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.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView; public class RoundedImageView extends ImageView { public RoundedImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
} public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
} public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} @Override
protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) {
return;
} if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0,0, null); } public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if(bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output); final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
sbmp.getWidth() / 2+0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint); return output;
} }
3. 使用控件
<com.chaowen.yixin.RoundedImageView
android:id="@+id/headImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25.0dip"
android:adjustViewBounds="true"
android:maxHeight="80dip"
android:maxWidth="80dip"
android:src="@drawable/head_default_yixin" />
4. ok 大功告成