圆形图像视图与背景

时间:2016-07-18 09:12:36

标签: android bitmap

这很奇怪,但我很困惑。我需要下载用户个人资料照片,如下图所示。我有外面的背景作为png。但我很困惑如何才能实现这一目标。如果有人知道更好的优化解决方案,请在此处进行纠正。我需要下载的图像将覆盖 只有内在的白色部分。 enter image description here

3 个答案:

答案 0 :(得分:0)

与您分享

public class MyCircleImageView extends ImageView {

private  Paint paint;

public MyCircleImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.RED);
}

@Override
protected void onDraw(Canvas canvas) {

   Drawable drawable = getDrawable();
    if (drawable != null && drawable instanceof BitmapDrawable)
    {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;

        Bitmap bitmap = bitmapDrawable.getBitmap();
        canvas.saveLayer(0,0,getWidth(),getHeight(),null,0);
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        int minR = (bitmapHeight<bitmapWidth?bitmapHeight:bitmapWidth)/2;
        canvas.drawCircle(bitmapWidth/2,bitmapHeight/2,minR,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

 //            canvas.drawBitmap(bitmap,0,0,paint);
        Rect srcRect = new Rect(0,0,bitmapWidth,bitmapHeight);
        Rect dstRect = new Rect(bitmapWidth/2-minR,0,bitmapWidth/2+minR,bitmapHeight);

        canvas.drawBitmap(bitmap,srcRect,dstRect,paint);

        paint.setXfermode(null);

        canvas.restore();

    }else {
        super.onDraw(canvas);
    }
}
}

答案 1 :(得分:0)

下面, 你需要两件事circleimageview + background(蓝色图片)。

如果你想要所有设备的最佳效果,那么设置蓝色图像的背景,并根据设备为cirlceimageview提供border_width,这样你就可以获得如上所述的imageview。

<RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
   <de.hdodenhof.circleimageview.CircleImageView
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:id="@+id/profile_image"
       android:layout_width="wrap_content"
       android:layout_centerInParent="true"
       android:layout_height="wrap_content"
       android:src="@mipmap/profile" //user profile
       android:background="@mipmap/njvkf" //Blue image
       app:civ_border_width="4dp"
       app:civ_border_color="#31A1DA"/>
      </RelativeLayout>

添加Cirlceimageview类you can see result of above code snippet

dependencies {
        compile 'de.hdodenhof:circleimageview:2.1.0'
}

答案 2 :(得分:0)

您可以使用:app:civ_fill_color

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/v_image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginEnd="8dp"
    app:civ_fill_color="@color/"
    android:src="@drawable/car" />