Shadow for Android)

Source: Internet
Author: User

Shadow: shadows can be set for various shapes (rectangle, circle, etc.) and text.


What is the principle of shadow?

In fact, it is very simple. You need to set shadow things as a main layer. Then draw a shadow layer under the main layer.

Shadow creation involves an important function:

Public void setShadowLayer (float radius, float dx, float dy, int color)
Radius: Shadow radius
Dx: offset of the X axis
Dy: the offset in the Y axis.
Color: shadow color


Note: If the radius is set to 0, the shadow is removed.


Specific implementation: [java] package xiaosi. textShadow;
 
Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. OS. Bundle;
Import android. view. View;
 
Public class TextShadowActivity extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (new drawCanvas (this ));
}
 
Class drawCanvas extends View
{
Private Bitmap bitmap = null;
Public drawCanvas (Context context)
{
Super (context );
Bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. I _skinprocess );
}
 
@ Override
Protected void onDraw (Canvas canvas)
{
Super. onDraw (canvas );
// Create a Paint object
Paint paint1 = new Paint ();
// Set the color
Paint1.setColor (0xFFFFFF00 );
// Set the shadow (Soft Edge, X axis displacement, Y axis displacement, and shadow color)
Paint1.setShadowLayer (5, 3, 3, 0xFFFF00FF );
// Solid rectangle & Its shadow
Canvas. drawText ("I love you", 20, 40, paint1 );
Paint paint2 = new Paint ();
Paint2.setColor (Color. GREEN );
Paint2.setShadowLayer (10, 5, 2, Color. YELLOW );
Canvas. drawText ("You are stupid", paint2 );

Paint paint3 = new Paint ();
Paint3.setColor (Color. RED );
Paint3.setShadowLayer (30, 5, 2, Color. GREEN );
Canvas. drawCircle (50,130, 30, paint3 );

Paint paint4 = new Paint ();
Paint4.setShadowLayer (5, 8, 7, Color. DKGRAY );
Canvas. drawBitmap (bitmap, 50,200, paint4 );
}
}
}


From agods-footprint
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.