Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply shadow to ImageView?

I want to apply shadow to the ImageView. When I'm applying shadow to a TextView I'm getting it but same it's not getting to ImageView. How can I solve this problem?

like image 975
New to android development Avatar asked Dec 02 '11 07:12

New to android development


People also ask

How to give shadow in ImageView?

Create a drawable file for shadow view and assign the name image_shadow and add the below code in this file. Now, open the XML file and add the below code into it, and set this drawable file as view background. That's it. You should be ready to go.

How to give shadow on top in android?

Height Adjustment: The height of the shadow is controlled by the view's, "v_shadowTop", height (currently 3dp). Gradient Adjustment: The gradient's color is controlled by the last six characters (hex code) of the startColor (currently 000000) and endColor (currently ffffff) in the "shadow_top. xml" drawable.

What are android shadows?

Shadows are drawn by the parent of the elevated view, and thus subject to standard view clipping, clipped by the parent by default. Elevation is also useful to create animations where widgets temporarily rise above the view plane when performing some action.


2 Answers

We can also use CardView which provides a rounded corner background and shadow. To use that you need to add the v7 CardView library as a dependency to the project in the build.gradle like below.

dependencies {     compile 'com.android.support:cardview-v7:23.0.1'     ------- } 

Note: Change 23.0.1 in the above line with the respected version.

So I surrounded the ImageView with CardView to make shadow like below.

<android.support.v7.widget.CardView     android:layout_width="match_parent"     android:layout_height="wrap_content"     card_view:cardBackgroundColor="@android:color/white">      <ImageView         android:id="@+id/dish_image"         android:layout_width="match_parent"         android:layout_height="120dp"         android:adjustViewBounds="true" />  </android.support.v7.widget.CardView> 

It'll add a shadow around the image.

Note: I don't know whether it is a good workaround. I am a beginner. I tried to implement the CardView which gives an idea to implement the same for this. If it is not good please update me with the reason.

like image 109
Mahendran Sakkarai Avatar answered Sep 26 '22 01:09

Mahendran Sakkarai


This is taken from Romain Guy's presentation at Devoxx, pdf found here.

Paint mShadow = new Paint();  // radius=10, y-offset=2, color=black  mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);  // in onDraw(Canvas)  canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow); 

Hope this helps.

NOTES

Don't forget for Honeycomb and above you need to invoke setLayerType(LAYER_TYPE_SOFTWARE, mShadow), otherwise you will not see your shadow! (@Dmitriy_Boichenko)

SetShadowLayer does not work with hardware acceleration unfortunately so it greatly reduces performances (@Matt Wear)

Answer Taken from Here

For Api greater than 21. You can try in xml in imageview or Button : Read here at developer site

android:elevation="5dp" 
like image 21
Avinash Verma Avatar answered Sep 23 '22 01:09

Avinash Verma