面具ImageView与圆角背景

GrI*_*sHu 32 android listview imageview android-layout

我有一个Custom ListView包含ImageViewTextView.一切都很好.

我想要的是列表中显示的图像是在圆角.从Webservice我得到矩形的图像.但我想在圆角显示ImageView如下.

在此输入图像描述

任何人都可以告诉我如何在圆角掩盖图像?

我已经尝试过创建如下的drawable文件并将其作为src应用于ImageView.但没有什么对我有用.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
   <item>
      <shape android:shape="oval" >
         <solid android:color="#FFFFFF" />
         <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />
         <corners android:radius="5dp" />
      </shape>
   </item>
   <item>
      <shape android:shape="oval" >
         <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
         <solid android:color="#FFFFFF" />
      </shape>
   </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

编辑:

在此输入图像描述 请帮我.

任何帮助将不胜感激.

谢谢

我申请了以下解决方案:

<FrameLayout
    android:id="@+id/imagemaskframe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/op_ivpic"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/iv_mask_op"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/imgmask" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

sri*_*ani 32

最好的方法是Canvas使用PorterDuff操作和/或Shaders.假设您Bitmap可以使用并存储mBitmap.

选项1:使用着色器.

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Load the bitmap as a shader to the paint.
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final Shader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    paint.setShader(shader);

    // Draw a circle with the required radius.
    final float halfWidth = canvas.getWidth()/2;
    final float halfHeight = canvas.getHeight()/2;
    final float radius = Math.max(halfWidth, halfHeight);
    canvas.drawCircle(halfWidth, halfHeight, radius, paint);
}
Run Code Online (Sandbox Code Playgroud)

选项2:使用PorterDuff模式.

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Create a circular path.
    final float halfWidth = canvas.getWidth()/2;
    final float halfHeight = canvas.getHeight()/2;
    final float radius = Math.max(halfWidth, halfHeight);
    final Path path = new Path();
    path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);

    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawPath(path, paint);
}
Run Code Online (Sandbox Code Playgroud)

注意:

  1. 在onDraw()调用中创建对象并不好.因此,您最初应该将油漆和着色器放在其他位置.将图像位图设置为视图时,可能会执行此操作.
  2. Canvas可能需要保存并在没有硬件纹理支持时恢复.这里没有提到围绕它的一般想法.
  3. 记得添加setWillNotDraw(false);到构造函数中.

其他参考文献:

  1. https://sriramramani.wordpress.com/2012/12/21/shaders/有关于的信息Shaders.
  2. http://mxr.mozilla.org/mozilla-central/source/mobile/android/base/ShapedButton.java用于PathFirefox for Android中的弧形按钮.
  3. http://sriramramani.wordpress.com/2012/08/27/constructing-squishy-buttons/提供有关CanvasICS之前的保存,恢复和特殊情况的信息.

  • 选项2适合我,但它有深色背景.知道怎么解决吗?http://stackoverflow.com/q/24054326/513413 (3认同)
  • @Hesam,附加参考#3解决了深色背景的问题. (3认同)
  • 很棒的答案!非常感谢!:)只设置XferMode必须是paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); (2认同)

小智 20

我肯定会像其他人一样推荐毕加索.我的一个Activity类的这段代码为我做了诀窍.它使用了我在color.xml中定义的颜色和简单的布局(如下所示).

       ImageView profile_image = (ImageView) findViewById(R.id.profile_image);
       mContext = profile_image.getContext();

        // ----------------------------------------------------------------
        // apply rounding to image
        // see: https://github.com/vinc3m1/RoundedImageView
        // ----------------------------------------------------------------
        Transformation transformation = new RoundedTransformationBuilder()
                .borderColor(getResources().getColor(R.color.my_special_orange))
                .borderWidthDp(5)
                .cornerRadiusDp(50)
                .oval(false)
                .build();

        Picasso.with(mContext)
                .load("http://{some_url}.jpg")
                .fit()
                .transform(transformation)
                .into(profile_image);
Run Code Online (Sandbox Code Playgroud)

和相应的布局文件:

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:padding="12dp">


        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_gravity="center"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:padding="12dp">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="First-Name"
                android:id="@+id/profile_first_name"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Lastname"
                android:id="@+id/profile_last_name" />
        </LinearLayout>



        </LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是结果:

图片


小智 17

我建议你使用另一种方法:

一个FrameLayout和两个ImageView可以做到这一点.

<FrameLayout>
    <ImageView />  your image 
    <ImageView />  put a image which has a transparent circle in it
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

然后你的图像可以通过透明圆圈看到.

  • 糟糕的解决方案,如果你想要被阻挡区域后面的透明度 (38认同)
  • 订单不正确.在Android中,代码越晚,视图就越接近用户.顺序应该是反过来的.好方案! (12认同)
  • 这会使图像在一个正方形区域之外仍然存在,对吗? (2认同)

God*_*uke 15

RoundedBitmapDrawableV4支持库可被应用于一个ImageView以实现期望的效果:

ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap avatar = BitmapFactory.decodeResource(getResources(), R.drawable.avatar);
RoundedBitmapDrawable roundDrawable = RoundedBitmapDrawableFactory.create(getResources(), avatar);
roundDrawable.setCircular(true);
imageView.setImageDrawable(roundDrawable);
Run Code Online (Sandbox Code Playgroud)


0x1*_*313 5

一个简单的解决方案,没有黑色背景

public class CircleImageView extends ImageView
{
    public CircleImageView(Context context)
    {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        // Create a circular path.
        final float halfWidth = canvas.getWidth()/2;
        final float halfHeight = canvas.getHeight()/2;
        final float radius = Math.max(halfWidth, halfHeight);
        final Path path = new Path();
        path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);

        canvas.clipPath(path);

        super.onDraw(canvas);
    }
}
Run Code Online (Sandbox Code Playgroud)