articles

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

How to set shape of Image view in Android

How to set shape of Image view in Android

Manoj Pandey 26105 24-Jun-2015

In Android, you can use android.widget.ImageView class for displaying an image file.

Image file is easy to use but hard to master, because of the various screen and dpi that we have in Android devices.

It displays an arbitrary image like an icon. The ImageView class is capable of loading images from various sources (such as resources or content providers),

takes care of computing its measurement from the image so that it is usable in any layout manager, and provides various display options such as scaling and tinting.

Here I am telling you to add the shape of the image view in background.

Follow the below steps to add shape in image views :

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 the drawable folder and add a new Android XML file and Select

shape Root Element.

How to set shape of Image view in Android

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

How to set shape of Image view in Android


Updated 06-Apr-2020

Leave Comment


Please enter subject.
Please enter comments message.

Comments

Liked By