【问题标题】:Rounded corners in imageView [duplicate]imageView中的圆角[重复]
【发布时间】:2012-05-21 11:35:26
【问题描述】:

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

【问题讨论】:

标签: android android-layout android-imageview


【解决方案1】:

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

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

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

【讨论】:

  • 仅供参考,他问的是圆角 ImageView 而不是角位图
  • 它会工作的。它将以编程方式传入的位图文件转换为ImageView
  • 性能会是个问题。有什么办法可以硬加速吗?
【解决方案2】:

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>

【讨论】:

  • 这个xml怎么用??
  • 如何显示带圆角的图像?它不是简单地替换图像吗?
  • @AmiuelS: 像这样使用 -> android:background="@drawable/ash_arrow" 并设置图片 Original image android:src="@drawable/ic_launcher"
  • @Aerrow 考虑编辑你的答案,因为它目前是错误的。
  • @AmiuelS:现在可以了吗?
【解决方案3】:

如果您尝试为 ImageView 设置背景,在 xml 中定义一个仅包含“corner”属性的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多