Sophie
Sophie

Reputation: 2634

Show GridView Items in a Frame

How to show grid view item in a frame, I am using GridView in my application and now i have to show each and every grid item within a frame

I am using black color as background, and want to use frame of some other color (like:white), so how can i use white as frame of griditems

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

Upvotes: 0

Views: 769

Answers (2)

SilentKiller
SilentKiller

Reputation: 6942

For showing image in frame there are two ways to do so

  1. Set Background Image or Color to show image is in Frame
  2. Set Masking

For setting BackGround :

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

For Masking check following code :

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);

If you want to show color as background then first option will be much better and efficient too.

Upvotes: 3

Manish Dubey
Manish Dubey

Reputation: 4306

Save a XML file in your Drawable folder with this code :

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

Now use this as a background of your image view and set your images as background resources. Source

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

Upvotes: 0

Related Questions