安卓无法将ImageView的角落变圆?

3

我正在制作一个应用程序,我想通过其角来圆化我的ImageView。但它没有从角落处变圆,我已经使用了以下代码:

<?xml version="1.0" encoding="utf-8"?>

 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="8dp"
    android:shape="rectangle" >

<solid android:color="#6BC1F8" />

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

</shape>

用于圆角化图片,但我得到的结果像这样: enter image description here

我想做出下面这样:

enter image description here

我不确定我哪里做错了,请指导我:)


你需要将你的图片也变成圆形,目前你只是将imageView变成了圆形,但是你的图片仍然是矩形的。请参考https://dev59.com/13I-5IYBdhLWcg3wu7BU来解决这个问题。 - Shayan Pourvatan
可能是重复的问题 https://dev59.com/xHE95IYBdhLWcg3wGZ9_ - Haresh Chhelana
请使用这个链接:https://github.com/vinc3m1/RoundedImageView - SweetWisher ツ
你有检查这个吗??? - SweetWisher ツ
4个回答

2

在Android中使用Shape来制作圆角

在drawable文件夹中创建名为roundcorner.xml的xml文件

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#33DDFF" />
        <corners android:radius="4dp" />
    </shape>

在您的ImageButton中添加此属性android:background="@drawable/roundcorner",并使用android:src设置imagebutton的图像。 或者 使用ImageView设置android:src = "your image"android:background= "your shape "android:clickable="true"

我正在使用一个XML文件作为背景,该XML文件指定角落应该是圆的。@thealeksandr - Lal
背景将会被圆角化,但是图片呢? - thealeksandr
android:src 属性中,指定您想要的图像... @thealeksandr - Lal
@Lal,你能给我你的邮箱吗? - User11
@Lal,你住在特里凡得琅吗?我需要一些信息,你能帮我吗? - User11
显示剩余6条评论

1

有一种很好的方法可以通过编程来创建圆形ImageView,你可以试试看。

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class ImageHelper {
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
}

此外,您可以参考由Google工程师Romain Guy创建的示例应用程序。

https://docs.google.com/a/impressol.com/file/d/0B3dxhm5xm1sia2NfM3VKTXNjUnc/edit?pli=1

参考文献,

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

如何制作带圆角的ImageView?


1

尝试这个

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

    <gradient
        android:angle="270"
        android:endColor="#cccccc"
        android:startColor="#cccccc" />

    <corners android:radius="9dp" />

    <stroke
        android:width="1px"
        android:color="#000000" />


</shape> 

使用android:background="@drawable/custom作为您的imageview背景,您将得到以下输出。

Screenshot of output

编辑:如果你想要更圆的角度,增加android:radius的值。如果你不想要黑色边框,则移除<stroke ..... />

希望这对你有所帮助。


-1

据我所知,你不能仅仅添加属性。你只能对形状进行操作。但是你可以尝试以下方法:添加两个ImageView。第一个ImageView内部有一个带圆角的透明区域。第二个ImageView放在第一个ImageView下面。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接