imageView中的圆角

时间:2012-05-21 11:35:26

标签: android android-layout android-imageview

这是我的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>

如何向我的ImageView显示圆角?

3 个答案:

答案 0 :(得分:10)

使用以下代码。它可能会帮助你 -

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

将你的Bitmap图像传递给这个方法并将其作为圆角。

答案 1 :(得分:3)

<强> 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

答案 2 :(得分:1)

如果您尝试为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>