【问题标题】:Images with rounded corners in a ListViewListView 中带有圆角的图像
【发布时间】:2014-02-25 12:55:21
【问题描述】:

我开发了一个 RSS 应用程序。我希望包含标题和图像的 ListView 具有圆角的图像。我在网上做了一个示例代码,但问题是图像仍然是矩形的。奇怪的是我有一个滑动菜单,当切换它时,它会将 rss ListView 推开,而它被推时图像有圆角!当他们停止推动动画时,再次变成矩形。这对我来说是一个非常奇怪的问题,所以有什么帮助吗? 圆形图像类:

public class RoundedImageView extends ImageView
{
    private float radius = 20.0f;

    public RoundedImageView(Context context) 
    {
        super(context);
    }

    public RoundedImageView(Context context,AttributeSet attrs)
    {
        super(context,attrs);
    }

    public RoundedImageView(Context context,AttributeSet attrs,int defStyle)
    {
        super(context,attrs,defStyle);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas)
    {
        Path clipPath = new Path();
        RectF rect = new RectF(0,0,getWidth(), getHeight());
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}

【问题讨论】:

标签: android listview bitmap rounded-corners


【解决方案1】:

试试这个方法:创建rounded_corner.xml文件到drawable\rounded_corner.xml

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

<solid android:color="#101010" />

<stroke
    android:width="1dp"
    android:color="#808080" />

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

并将Background 设置为您的ImageView 喜欢

android:background="@drawable\rounded_corner"

还将您的RSS 图像设置为ImageViewSrcBitmap 之类的:

 imageview.setBitmap(yourrssimage);

【讨论】:

  • 我试过了,边角还是不圆。奇怪的是,使用我发布的代码,角在模拟器上是圆的,但在我的注释 II 中它们不是。
  • 更改 XML 文件中的半径并尝试。或发布您的所有布局和代码。你可能做错了什么。
  • 好的,我刚刚注意到一些 rss 项目没有图像源,默认图像具有圆角,但具有图像源的那些是矩形的。我正在使用 setImageURI 来设置图像源。如何将 uri 更改为位图以使用 setBitmap 方法?
【解决方案2】:

使用圆角图片或创建xml并在imageview上设置为背景图片。

【讨论】:

    【解决方案3】:

    在您的可绘制文件夹中创建一个如下所示的 XML。

    button_back.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@color/grey">
    
        <shape>
          <solid
              android:color="#ef4444" />
          <stroke
              android:width="1dp"
              android:color="#992f2f" />
          <corners
              android:radius="16dp" />
        </shape>
    </item>
    

    然后像这样应用到您的按钮。

    <Button
          android:text="@string/press_me"
          android:layout_margin="12dp"
          android:layout_width="fill_parent"
          android:layout_height="30dp"
    
          android:background="@drawable/button_back"
    />
    

    【讨论】:

      【解决方案4】:

      创建一个笔画并将其列为您的图像视图背景

      <?xml version="1.0" encoding="UTF-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
          <solid android:color="@android:color/transparent"/>
          <stroke android:width="5dip" android:color="@android:color/transparent" />
          <corners android:radius="5dip"/>
          <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
      </shape>
      

      在你的 imageview xml 中

      android:background="@drawable/your_stroke_xml"
      

      【讨论】:

      • 它适用于单个图像,而不是 ListView 中的图像。另外,现在当我上下滚动时,屏幕顶部会出现一个空白。
      • @nick28 我也有类似的问题。你找到解决方案了吗?
      • @Bresiu 我在我的问题中发布的课程非常适合我。问题出在我正在使用调用 MIUI 的自定义 ROM 上,我已经返回到 CM ROM 并且该类按预期工作:)
      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 2014-11-10
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多