android 头像 圆形遮罩
2014-03-28 11:41
363 查看
用户头像圆形遮罩两种实现方法:
方法一(推荐):
调用:
xml:
<.....view.CircularImage
android:id="@+id/ic_user"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_user_default" />
java:
CircularImage cover_user_photo = (CircularImage) findViewById(R.id.cover_user_photo2);
cover_user_photo.setImageResource(R.drawable.face);
方法二(毛边过滤):
方法一(推荐):
public abstract class MaskedImageView extends ImageView { private static final Xfermode MASK_XFERMODE; private Bitmap mask; private Paint paint; static { PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN; MASK_XFERMODE = new PorterDuffXfermode(localMode); } public MaskedImageView(Context paramContext) { super(paramContext); } public MaskedImageView(Context paramContext, AttributeSet paramAttributeSet) { super(paramContext, paramAttributeSet); } public MaskedImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) { super(paramContext, paramAttributeSet, paramInt); } public abstract Bitmap createMask(); protected void onDraw(Canvas paramCanvas) { Drawable localDrawable = getDrawable(); if (localDrawable == null) return; try { if (this.paint == null) { Paint localPaint1 = new Paint(); this.paint = localPaint1; this.paint.setFilterBitmap(false); Paint localPaint2 = this.paint; Xfermode localXfermode1 = MASK_XFERMODE; @SuppressWarnings("unused") Xfermode localXfermode2 = localPaint2 .setXfermode(localXfermode1); } float f1 = getWidth(); float f2 = getHeight(); int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, 31); int j = getWidth(); int k = getHeight(); localDrawable.setBounds(0, 0, j, k); localDrawable.draw(paramCanvas); if ((this.mask == null) || (this.mask.isRecycled())) { Bitmap localBitmap1 = createMask(); this.mask = localBitmap1; } Bitmap localBitmap2 = this.mask; Paint localPaint3 = this.paint; paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3); paramCanvas.restoreToCount(i); return; } catch (Exception localException) { Log.e("ender", "MaskedImageView exception = " + localException); } } }
public class CircularImage extends MaskedImageView { public CircularImage(Context paramContext) { super(paramContext); } public CircularImage(Context paramContext, AttributeSet paramAttributeSet) { super(paramContext, paramAttributeSet); } public CircularImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) { super(paramContext, paramAttributeSet, paramInt); } public Bitmap createMask() { int i = getWidth(); int j = getHeight(); Bitmap.Config localConfig = Bitmap.Config.ARGB_8888; Bitmap localBitmap = Bitmap.createBitmap(i, j, localConfig); Canvas localCanvas = new Canvas(localBitmap); Paint localPaint = new Paint(1); localPaint.setColor(-16777216); float f1 = getWidth(); float f2 = getHeight(); RectF localRectF = new RectF(0.0F, 0.0F, f1, f2); localCanvas.drawOval(localRectF, localPaint); return localBitmap; } }
调用:
xml:
<.....view.CircularImage
android:id="@+id/ic_user"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_user_default" />
java:
CircularImage cover_user_photo = (CircularImage) findViewById(R.id.cover_user_photo2);
cover_user_photo.setImageResource(R.drawable.face);
方法二(毛边过滤):
public class ImageCircleView extends ImageView{ Path path;
public abstract class MaskedImageView extends ImageView { private static final Xfermode MASK_XFERMODE; private Bitmap mask; private Paint paint; static { PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN; MASK_XFERMODE = new PorterDuffXfermode(localMode); } public MaskedImageView(Context paramContext) { super(paramContext); } public MaskedImageView(Context paramContext, AttributeSet paramAttributeSet) { super(paramContext, paramAttributeSet); } public MaskedImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) { super(paramContext, paramAttributeSet, paramInt); } public abstract Bitmap createMask(); protected void onDraw(Canvas paramCanvas) { Drawable localDrawable = getDrawable(); if (localDrawable == null) return; try { if (this.paint == null) { Paint localPaint1 = new Paint(); this.paint = localPaint1; this.paint.setFilterBitmap(false); Paint localPaint2 = this.paint; Xfermode localXfermode1 = MASK_XFERMODE; @SuppressWarnings("unused") Xfermode localXfermode2 = localPaint2 .setXfermode(localXfermode1); } float f1 = getWidth(); float f2 = getHeight(); int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, 31); int j = getWidth(); int k = getHeight(); localDrawable.setBounds(0, 0, j, k); localDrawable.draw(paramCanvas); if ((this.mask == null) || (this.mask.isRecycled())) { Bitmap localBitmap1 = createMask(); this.mask = localBitmap1; } Bitmap localBitmap2 = this.mask; Paint localPaint3 = this.paint; paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3); paramCanvas.restoreToCount(i); return; } catch (Exception localException) { Log.e("ender", "MaskedImageView exception = " + localException); } }
}
调用: ImageCircleView qq = (ImageCircleView)findViewById(R.id.cover_user_photo2); qq.setImageResource(R.drawable.face);
相关文章推荐
- android 遮罩制作 圆形( 多边形)头像
- Android实战之设置圆形头像
- android 圆形头像
- Android 圆形头像+渐变+白框
- Android控件-圆形头像RoundedImageView
- android自定义圆形头像view,继承imageview
- Android 中CircleImageView自定义View(圆形头像)
- Android 绘制圆形头像
- android圆形图片,圆形头像实现方法(可直接运用到工程中)
- android ImageView 实现圆形图片(QQ头像)
- Android 自定义View -->圆形头像
- Android 两种方式实现圆形头像
- android分析之自定义圆形头像
- Android自定义控件仿QQ编辑和选取圆形头像
- Android圆形头像
- Android实现带圆环的圆形头像
- android自定义View圆形头像
- android圆形头像的实现类CircleImageView.java
- android给用户头像设置圆形加边框
- Android 圆形头像