0

如何在 a中show网格化视图,我在我的应用程序中使用GridView,现在我必须在 a 中显示每一个itemframegrid itemframe

我正在使用black颜色作为background,并且想使用frame一些other color(如:白色),那么我如何使用white作为网格项的框架

    <ImageView
        android:id="@+id/imageItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@null"
        android:contentDescription="@null"
        />
4

2 回答 2

3

为了在框架中显示图像,有两种方法可以做到这一点

  1. 设置背景图像或颜色以显示图像在框架中
  2. 设置遮罩

用于设置背景:

<ImageView
    android:id = "@+id/imageItem"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:adjustViewBounds = "true"
    android:scaleType = "center"
    android:padding = "5dp"
    android:background = "#FFFFFF"
    android:contentDescription = "@null" />

对于掩蔽检查以下代码:

ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.content_image);
Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setBackgroundResource(R.drawable.background_frame);

如果您想将颜色显示为背景,那么第一个选项也会更好、更有效。

于 2014-10-14T05:38:18.270 回答
0

使用以下代码将文件保存XML在您的Drawable文件夹中:

<?xml version="1.0" encoding="utf-8"?>

<item android:state_enabled="true"><shape>
        <solid android:color="#FFFFFF" />

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

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

 <!--       <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />-->
    </shape></item>

现在将其用作图像视图的背景并将图像设置为背景资源。 来源

<ImageView
        android:id="@+id/imageItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@drawable/your_xml_name"
        android:contentDescription="@null"
        />
于 2014-10-14T05:28:47.160 回答