代码之家  ›  专栏  ›  技术社区  ›  nick28

ListView中带有圆角的图像

  •  1
  • nick28  · 技术社区  · 11 年前

    我开发了一个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);
        }
    }
    
    4 回复  |  直到 11 年前
        1
  •  3
  •   M D    11 年前

    尝试以下方式:创建 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 映像到 图片框 作为一个 Src Bitmap 如:

     imageview.setBitmap(yourrssimage);
    
        2
  •  0
  •   user8938    11 年前

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

        3
  •  0
  •   InnocentKiller    11 年前

    在可绘制文件夹中创建一个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
  •  0
  •   Ahmed Zayed    11 年前

    创建笔划并将其列为图像视图背景

    <?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"