Home > DeveloperSection > Articles > How to set shape of Image view in Android

How to set shape of Image view in Android


Android Android  Android Styles  Image View 
Ratings:
0 Comment(s)
 1189  View(s)
Rate this:

How to set shape of Image view in Android

In Android, you can use android.widget.ImageView class to display an image file. Image file is easy to use but hard to master, because of the various screen and dpi in Android devices.

Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

Here I am telling you to add shape of imageview in background.

Follow below steps to add shape in imageview

1.      Create an android projected in eclipse or Android studio

2.      Add image view in activity_main.xml layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <ImageView

        android:id="@+id/myimage4"

        android:layout_width="190sp"

        android:layout_height="190sp"

        android:background="@drawable/myshape"

        android:contentDescription="@string/app_name"

        android:padding="5sp"

        android:scaleType="fitXY"

        android:src="@drawable/ic_launcher"

        android:visibility="visible" />

 

</RelativeLayout>

 

3.      Now right click on drawable folder and add new Android xml file and select shape Root Element.

       

 

4.      Now add shape code in myshpae.xml file

 

 

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >

 

    <stroke

        android:width="2dp"

        android:color="#FFFFFFFF" />

 

    <gradient

        android:angle="225"

        android:endColor="#DD2ECCFA"

        android:startColor="#DD000000" />

 

    <corners

        android:bottomLeftRadius="7dp"

        android:bottomRightRadius="7dp"

        android:topLeftRadius="7dp"

        android:topRightRadius="7dp" />

 

</shape>

 

5.      You have no need to change in MainActivity.class

Now run your application

 


Don't want to miss updates? Please click the below button!

Follow MindStick