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/1920183
Several ways Android uses Alpha to achieve gradient effects