【问题标题】:Rounded corners for an Imageview in androidandroid中Imageview的圆角
【发布时间】:2012-09-15 01:03:14
【问题描述】:

我在线性布局中有一个 textview 和 imageview。 Textview 在顶部,imageview 在底部。我使用下面的线为线性布局设置圆角。但是 imageview 的角不是圆角的。我看到只有线性布局的顶角是四舍五入的。 我怎样才能使 imageview 的底角变圆?(如果我删除 imageview,我看到所有的角都是圆的)

rounded_corners.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#ffffff" />


<corners
    android:bottomLeftRadius="8dp"
    android:bottomRightRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp" />

</shape>

main.xml

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="50dp"
    android:background="@xml/rounded_corners"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="xxxxxxxx" />

    <ImageView     
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/my_image_view" />
</LinearLayout>

屏幕截图:

【问题讨论】:

  • 如果您将圆角形状设置为布局的背景并且在输出中它不是圆角,则不太可能。您能否上传当前输出的快照。
  • 我看过那个例子。为什么我们不能在 xml 中带来相同的效果?我哪里错了?
  • @user1670443 我不认为这可以在 xml 中实现,除非您将图像编辑为圆角。
  • 我认为问题是您已将 centercrop 设置为 imageview。

标签: android imageview android-linearlayout rounded-corners


【解决方案1】:

你可以把图片的左下角和右下角弄圆,像这样:

代码:

public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    final RectF rectF = new RectF(0, 0, w, h);

    canvas.drawRoundRect(rectF, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rectF, paint);

    /**
     * here to define your corners, this is for left bottom and right bottom corners
     */
    final Rect clipRect = new Rect(0, 0, w, h - radius);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    canvas.drawRect(clipRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rectF, paint);

    bitmap.recycle();

    return output;
}

这种方法可以给你一个左下角和右下角圆角的图像。

【讨论】:

  • 我怎样才能改变这个,让上面的两个角变成圆角?
  • 我想通了,改变这一行--> final Rect rectBottom = new Rect(0, radius, w, h);感谢您的帮助!
  • 我想要左边的圆角...请告诉我怎么做??
  • @Pranav Mahajan 只需将 clipRect 替换为最终的 Rect clipRect = new Rect(radius, 0, w, h);
  • @JishiChen:谢谢……它确实有效。你能告诉我我只想要左上角还是左下角??
【解决方案2】:

对于图像视图的圆角,将您的图像转换为位图,然后尝试以下代码:

public static Bitmap roundCorner(Bitmap src, float round) 
{
    // image size
    int width = src.getWidth();
    int height = src.getHeight();

    // create bitmap output
    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    // set canvas for painting
    Canvas canvas = new Canvas(result);
    canvas.drawARGB(0, 0, 0, 0);

    // config paint
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);

    // config rectangle for embedding
    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    // draw rect to canvas
    canvas.drawRoundRect(rectF, round, round, paint);

    // create Xfer mode
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    // draw source image to canvas
    canvas.drawBitmap(src, rect, rect, paint);

    // return final image
     return result;
}

【讨论】:

    【解决方案3】:

    您的线性布局是圆角,这是毫无疑问的,但您的图像不是。在屏幕截图中,图像与底部的线性布局重叠。尝试在线性布局中添加一些填充(android:padding="20dp")。这应该可以。

    【讨论】:

    • 是的。图像与底部的线性布局重叠。我希望我的图像视图应该适合线性布局(如我的屏幕截图所示),并且 iamge sholud 底部有圆角。如果我使用填充,我会在图像、线性布局之间产生一些差距
    • 在应用填充后,您的线性布局有圆角??
    • 是的。现在我的Linearlayout 有圆角.. 但差距是存在的。我使用了 android:paddingBottom = "5dp"
    • 其实线性布局还是矩形,背景是圆角的。
    • 这是您可以使用 xml 实现的最大值...尽管您可以为此应用一些技巧。您可以放置​​另一个与图像视图重叠的圆角视图。尝试解决这些问题,您将实现您想要实现的目标。
    猜你喜欢
    • 1970-01-01
    • 2021-06-09
    • 2014-02-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多