Undo Shadow Android ImageView

I am trying to add a shadow to ImageView. Another Stackoverflow answer seems to be using canvas and bitmaps, etc., which is much more complicated than it should be.

In 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 will display the shadow, regardless of whether the shadow is applied to the view, image or text.

I 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 do not see the expected red shadow for my image of a bird at all.

Am I doing something wrong?

Example

Let's say I want the shadow to be like this:

shadow example

Update

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

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

T_T

+7
android imageview
source share
1 answer

Android Studio has a built-in drawable that you can use to apply shadow to any View . It looks like a shadow.

 android:background="@drawable/abc_menu_dropdown_panel_holo_light" 

Using this, you cannot change the background color in the view and its border color. If you want to create your own custom option, 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, here i preferred solid white --> <shape android:shape="rectangle"> <solid android:color="@android:color/red" /> </shape> </item> </layer-list> 

and apply to your view as below

 android:background="@drawable/custom_drop_shadow_drawable" 

Hope this helps you!

+8
source share

All Articles