Android imageView 中的圆角

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10684469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:36:54  来源:igfitidea点击:

Rounded corners in imageView

androidandroid-layoutandroid-imageview

提问by edi233

This is my xml layout:

这是我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="75dp"
    android:padding="3dp" >
    <ImageView
        android:id="@+id/img"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:scaleType="fitCenter" 

        />

    <TextView
        android:id="@+id/name"
        android:layout_width="208dp"
        android:layout_height="75dp"
        android:paddingLeft="15dp"
        android:paddingTop="15dp" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:src="@drawable/ash_arrow" />

</LinearLayout>

How can i show my ImageViewwith rounded corners?

我怎样才能ImageView用圆角显示我的?

回答by Praveenkumar

Use below code. It may helps you -

使用下面的代码。它可能会帮助你——

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
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 = 12;

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;
}

Pass your Bitmap image into this method and get that as rounded corner.

将您的位图图像传递到此方法中并将其作为圆角。

回答by Aerrow

ash_arrow.xml

ash_arrow.xml

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

    <!--Background with solid color-->

    <solid android:color="@color/colorWhite"/>

    <!--background with gradient-->
    <!--<gradient-->
    <!--android:centerColor="#fff"-->
    <!--android:centerX="50%"-->
    <!--android:centerY="50%"-->
    <!--android:endColor="#f0f"-->
    <!--android:gradientRadius="100"-->
    <!--android:startColor="#f00"-->
    <!--android:type="radial"/>-->

    <!--Stoke with dashed line-->
    <!--<stroke-->
    <!--android:width="8dp"-->
    <!--android:color="#f00"-->
    <!--android:dashGap="8dp"-->
    <!--android:dashWidth="8dp"/>-->

    <!--Stroke normal-->
    <stroke
        android:width="1dp"
        android:color="@color/willow_grove"/>

    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"/>

</shape>

enter image description here

在此处输入图片说明

回答by Jeje Doudou

What if you try to set a background to your ImageView, defining a Shape in xml that would only contain a "corner" Attribute? Something like that:

如果您尝试为 ImageView 设置背景,在 xml 中定义一个只包含“角”属性的 Shape,该怎么办?类似的东西:

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