2016-06-15 156 views
0

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

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

  1. 创建带圆角边框的drawable并将其设置为ImageView的背景。
  2. 将ImageView与另一个将作为框架工作的虚拟ImageView一起包装到FrameLayout中,并将边框设置为背景。
  3. 使用RoundedBitmapDrawableFactory创建RoundedBitmap并设置边框为背景。

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

在#3问题是边框和圆角位图的角落不完全匹配到彼此。我不知道该怎么做,以支持多种屏幕尺寸和密度see here

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

对于选项 代码:

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

任何人有任何想法如何实现无记忆问题所需的输出。

回答

0

您可以定义自定义ImageView以在图像上添加颜色边框;这是我多年前的项目之一,它是Android OS的一种圆形图像部件。

https://github.com/avenwu/IndexImageView 

Screenshot