Several ways Android uses Alpha to achieve gradient effects

Source: Internet
Author: User


Principle: The gradient effect is achieved by changing the alpha value of the control transparency


Method One: Using XML

Step one: First, we want to create an. xml file in the Anim folder under the Res folder (here I created Alpha.xml)

Set start transparency at 0.0 (transparent): android:fromalpha= "0.0"

Set End Transparency 1.0 (opaque): android:toalpha= "1.0"


<alpha

Xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:fromalpha= "0.0"

Android:toalpha= "1.0"

Android:duration= ">"

</alpha>


Step two: Next you will use the animation class to achieve the effect


@Override

protected void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);


ImageView but= (ImageView) Findviewbyid (R.id.mainbutton);


Animation manimation = Animationutils.loadanimation (this, r.anim.alpha);

But.startanimation (manimation);

}


Method Two: Use the Paint class

Method: Change the alpha value of the graph by Paint.setalpha ();


public class Mainactivity extends Activity implements ontouchlistener{

Paint paint=new paint ();

Set initial alpha to 100 (opaque)

int al=100;


@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (New Renderview (this));

}


Class Renderview extends View {

protected void OnDraw (canvas canvas) {

Get the screen length, width

int Height=canvas.getheight ();

int Width=canvas.getwidth ();

Draw a fully populated red rectangle and change the alpha value

Paint.setstyle (Paint.Style.STROKE);

Paint.setcolor (color.red);

Paint.setalpha (al--); Change the alpha (transparency) value

Canvas.drawrect (Width/2,2,width/3*2,height/2,paint);

Invalidate ();

}

}

}

This article is from the "12608835" blog, please be sure to keep this source http://12618835.blog.51cto.com/12608835/1920181

Several ways Android uses Alpha to achieve gradient effects

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.