Android中Imageview的圆角

use*_*443 5 android rounded-corners imageview android-linearlayout

我在linearlayout中有一个textview和imageview.Textview位于顶部,imageview位于底部.我使用下面的线条为linearlayout设置了圆角.但是imageview角落并没有四舍五入.我看到只有linearlayout的顶角是圆形的.我如何才能获得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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

屏幕截图: 在此输入图像描述

Jis*_*hen 7

您可以将图像的左下角和右下角四舍五入,如下所示:

在此输入图像描述

码:

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;
}
Run Code Online (Sandbox Code Playgroud)

此方法可以为您提供左下角和右下角圆角的图像.


kar*_*arn 0

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