【问题标题】:Xamarin Android round corner border with color ImageViewXamarin Android 圆角边框与彩色 ImageView
【发布时间】:2016-06-15 06:41:35
【问题描述】:

我正在使用 BaseAdapter 以自定义布局呈现 GridView。我的视图在 ImageView 下包含 ImageView 和 TextView。对于 ImageView 我想用黑色设置圆角边框。

我尝试了一些来自社区答案的建议。

  1. 创建带有圆形边框的可绘制对象并将其设置为 ImageView 的背景。
  2. 使用另一个虚拟 ImageView 将 ImageView 包装到 FrameLayout 中,该虚拟 ImageView 将用作框架,并将可绘制的边框设置为它的背景。
  3. 使用 RoundedBitmapDrawableFactory 创建 RoundedBitmap 并将可绘制的边框设置为背景。

在上述所有情况下,我都没有得到我想得到的效果。 图像边框仅与图像重叠。

在 #3 中,问题是边框和圆角位图角彼此不完全匹配。我不确定如何支持多种屏幕尺寸和密度see here

如果我使用代码创建圆角位图,则存在严重的内存问题: more details, see comments. Further details

对于选项 3 代码:

internal void SetImageWithRoundCorners(int imageResID, Context context)
{
    Resources res = context.Resources;
    Bitmap src = BitmapFactory.DecodeResource(res, imageResID);
    RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.Create(res, src);
    dr.CornerRadius = 50.0f;
    ImgTopicIcon.SetImageDrawable(dr);
}

round_border_corner.xml

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
     <solid android:color="#00ffffff" />
     <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
     <corners android:radius="@dimen/GVImgRoundCornerRad" />
     <stroke android:width="2dp" android:color="#ff000000" />
</shape> 

Grid_Custom_Item.xml

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="48dp"
    android:gravity="center"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ImgCategoryIcon" />
        <ImageView
            android:src="@drawable/round_corner_border"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:id="@+id/TxtCategoryName"
    android:gravity="center" />
</LinearLayout>

预期结果: http://screencast.com/t/6hd8moeTgqAQ

输出: http://screencast.com/t/WVgdlyq87IU

任何人都知道如何在没有内存问题的情况下实现所需的输出。

【问题讨论】:

    标签: android xamarin.android android-imageview


    【解决方案1】:

    您可以定义一个自定义的 ImageView 来在图像上添加颜色边框;这是我多年前的一个项目,它是一种适用于 Android 操作系统的圆形图像小部件。

    https://github.com/avenwu/IndexImageView
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2016-10-29
      相关资源
      最近更新 更多