Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView drop shadow

I'm trying to add a drop shadow to an ImageView. The other Stackoverflow answer seems to be using canvas and bitmaps etc, way more complicated than it needs to be.

On iOS I would do something like this:

myImageView.layer.shadowColor = [UIColor redColor].CGColor;
myImageView.layer.shadowRadius = 5;
myImageView.layer.shadowOffset = CGRectMake(0, 5);

and it would render a drop shadow, regardless of whether the shadow is being applied on a view, an image or text.

I've tried to do the same on Android but it just refuses to work:

birdImageView = new ImageView(context);
birdImageView.setImageResource(R.drawable.yellow_bird);

Paint paint = new Paint();
paint.setAntiAlias(true);


birdImageView.setLayerType(LAYER_TYPE_SOFTWARE, null);
paint.setShadowLayer(5, 0, 5, Color.argb(255, 255, 0, 0));
birdImageView.setLayerPaint(paint);

I don't see any expected red drop shadow on my bird image at all.

Am I doing something wrong?

Example

Let's say I want the drop shadow like this:

drop shadow example

Update

Do I have to resort to Android 5.0 and the new Elevation api (http://developer.android.com/training/material/shadows-clipping.html) ?

But if one was to use new API, then according to current demographics (http://www.droid-life.com/2016/02/02/android-distribution-february-2016/) more than 50% of users will not be able to use the app.

T_T

like image 664
Zhang Avatar asked Feb 03 '16 06:02

Zhang


People also ask

How to add shadow to a view in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above tags indicates about shadow color, Axis's(X, Y) and shadow radius for text view.

How to give elevation to shape in Android?

To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.

What is Android elevation?

Elevation (Android) Elevation is the relative depth, or distance, between two surfaces along the z-axis. Specifications: Elevation is measured in the same units as the x and y axes, typically in density-independent pixels (dp).


1 Answers

In android studio there is in build drawable that you can use for apply shadow to any View. That is similar like a drop shadow.

android:background="@drawable/abc_menu_dropdown_panel_holo_light"

Using this you can not change the background color of the view & its border color. If you want your own custom drawable then use layer-list

custom_drop_shadow_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--the shadow comes from here-->
    <item
        android:bottom="0dp"
        android:drawable="@android:drawable/dialog_holo_light_frame"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">

    </item>

    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <!--whatever you want in the background -->
        <shape android:shape="rectangle">
            <solid android:color="@android:color/red" />

        </shape>
    </item>
</layer-list>

and apply to your view like below

android:background="@drawable/custom_drop_shadow_drawable"

Hope it will help you! Thanks Android View shadow

like image 167
Maheshwar Ligade Avatar answered Oct 31 '22 07:10

Maheshwar Ligade