Create Shadow For Image In Android Using Android Studio

This article will show you how to create a shadow for any type of image in an Android application using Android Studio.

Introduction

This article will show you how to create a shadow for any type of image in an Android application using Android Studio.The shadow will appear with the given image.This will provide a different look to the image.

Applications
  • Any Type of Views
    Long-Shadows can render shadow for any type of View, even custom Views.

  • Heavy calculation in Native
    Long Shadows shifts a load of heavy calculations to native, to reduce the overhead in Java. Thus, rendering is very fast and efficient.

  • Precise control over the shadow of every View
    The library supports fine control over shadows of a View, so two Views in the same ViewGroup can have different shadow parameters.

  • Multiple Shadows for a View
    The library supports rendering multiple long shadows for a single View, simulating multiple light sources.

  • Async Calculations
    The library allows the operations to be asynchronous to avoid blocking the UI thread for a long calculation.

Step 1

Create a new project in Android Studio.

 

Give a name to the project and click "Next". 

 

Select the "Phone and Tablet" and click "Next". 

 

Select an empty activity and click "Next". 

 

At last, give the activity name and click on "Finish".

 

Step 2

Next, copy the image in which you want to create the shadow and go to Shadow>>res>>drawable.

 

Then, paste the copied image into the drawable folder.

 

Step 3

Next, go to app >> res >> layout >> activity_main.xml. Select activity page and just type the code as follows.

This code will get the image width and height as input and give the shadow as output.
  1. <com.sdsmdg.harjot.longshadows.LongShadowsWrapper xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F5362C">  
  2.     <com.sdsmdg.harjot.longshadows.LongShadowsImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:src="@drawable/image" />   
  3. </com.sdsmdg.harjot.longshadows.LongShadowsWrapper>  
Step 4

Setup the gradle by just adding the following dependency in your app's build.gradle.
  1. dependencies  
  2. {  
  3.    implementation 'com.sdsmdg.harjot:longshadows:1.0.1'  
  4. }  
Step 5

To set up the angle for the shadow(optional), just add the following code at last.
  1. app:shadow_angle="90"
Step 6

To set up the length of the shadow(optional), add the following code.

Where 100=1cm,
  1. app:shadow_length="100"

Step 7 

To set up the shadow color (optional), add the following code.
  1. app:shadow_endColor="#00000000"
  2. app:shadow_startColor="#88000000" />
Step 8

Verify the preview.

->After the code is applied, the preview will appear like this (without angle, length, and color).

 

Step 9

Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.



Run the application in your desired emulator (Shift + F10).

 

Finally, we have successfully created the shadow for the image.

Explanation of Source Code 

The LongShadowsWrapper is an extension of Relative Layout. The LongShadowsFrameLayoutWrapper is an extension of FrameLayout. Use any one of them as per your convenience.

Firstly, eligible child Views (LongShadowsImageView, LongShadowsTextView or extensions of LongShadowsView) are found using a recursive strategy. Once an eligible View is found, it goes through the process to generate the long shadow.

*Support and share.  Thank you!*