The Android system supports colors that are made up of 4 values, the first 3 RGB, the three primary colors we often say (red, green, and blue), and the last value is a, which is alpha. These 4 values are 0~255 between the two. The smaller the color value, the lighter the color, the greater the color value, which indicates the darker the color. If RGB is 0, is black, if all is 255, is white. Alpha also needs to change between 0~255. The smaller the value of alpha, the more transparent the color, and the greater the alpha value, the less transparent the color. When Alpha has a value of 0 o'clock, the color is completely transparent, and a completely transparent bitmap or graphic disappears from view. When Alpha has a value of 255, the color is opaque. From the characteristics of alpha, setting the transparency of a color is actually setting the alpha value.
Translucent: <button android:background= "#e0000000" .../>
Transparent: <button android:background= "#00000000" .../>
The color and opacity (alpha) values are represented by hexadecimal notation. The range of values for any one color is 0 to 255 (00 to FF). For alpha,00 representations are fully transparent, FF represents completely opaque. The expression order is "Aabbggrr", where Aa=alpha (00 to FF), Bb=blue (00 to FF), Gg=green (00 to FF), rr=red (00 to FF). For example, if you want to apply a blue with an opacity of 50% to an overlay, you should specify the following values: 7fff0000
Setting the transparency of a color can be done through the Setalpha method of the paint class. Because the project, many times do not need to make the photo completely transparent, so the experiment made a can adjust the transparency of the image of the program.
Example 1
This example uses a slider (SeekBar) component to change the alpha value (transparency) of the color in the bitmap. The code for the MyView class that displays the bitmap is as follows:
Private class MyView extends View
{
private Bitmap Bitmap;
Public MyView
{
super (context);
InputStream is =getresources (). Openrawresource (r.drawable.image);
Bitmap = Bitmapfactory.decodestream (is);
SetBackgroundColor (Color.White);
}
@Override
protected void OnDraw (Canvas Canvas)
{
Paint Paint = new Paint ();
Paint.setalpha (Alpha);
Canvas.drawbitmap (Bitmap, new Rect (0, 0, bitmap.getwidth (), Bitmap
. GetHeight ()), New Rect (Ten, 310, 235), paint) ;
}
}
The 2nd parameter of the Drawbitmap method in the preceding code represents the copied region of the in-place diagram, in this case, the entire in-situ diagram. The 3rd parameter represents the target area of the drawing.
The code for the Onprogresschanged time method for the Seekbar component is as follows:
public void onprogresschanged (SeekBar SeekBar, int progress,
Boolean Fromuser)
{
alpha = progress;
Settitle ("Alpha:" + progress);
Myview.invalidate ();
}
To run this example, move the slider to the left and right position, and you will see the effect as shown in the following two graphs:
Example 2
Alpha Transparent gradient animation.
<alpha> label is alpha transparency node
Android:fromalpha= "1.0" sets the animation starting transparency to 1.0 means completely opaque
Android:toalpha= "0.0" sets the animation end transparency to 0.0 to indicate full transparency
Which means Alpha's range is between 0.0-1.0.
This animated layout sets the animation from a completely opaque gradient to full transparency.
<?xml
version= "1.0" encoding= "Utf-8"?> <alpha xmlns:android=
"http://
" Schemas.android.com/apk/res/android "
android:fromalpha=" 1.0 "
android:toalpha=" 0.0 "
android: Repeatcount= "Infinite"
android:duration= "> </alpha>"
Code implementation
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationUtils;
Import Android.widget.ImageView;
the public class Alphaactivity extends activity {
/** shows the imageview**/
imageview Mimageview = null of the animation;
/** Transparent animation **/
Animation manimation = null;
@Override
public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.translate);
/** get ImageView object **/
Mimageview = (imageview) Findviewbyid (R.id.imageview);
/** loaded transparent animation **/
manimation = Animationutils.loadanimation (this, r.anim.alpha);
/** Play transparent animation **/
mimageview.startanimation (manimation);
}