Android set alpha value to make an instance of transparent and gradient effect

Source: Internet
Author: User

The colors supported by the Android system are made up of 4 values, the first 3 RGB, which is what we often call the three primary colors (red, green, blue), and the last value is a, which is alpha. These 4 values are between 0~255. A smaller color value indicates that the color is lighter, and the larger the color value, the darker the color. If RGB is 0, it is black, if all is 255, it is white. Alpha also needs to change between 0~255. The smaller the alpha value, the more transparent the color is, and the greater the alpha value, the more opaque the color is. When Alpha has a value of 0 o'clock, the color is completely transparent, and a fully transparent bitmap or graphic disappears from view. When Alpha has a value of 255, the color is opaque. From the Alpha feature, the transparency of setting the 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, full transparency is expressed, and FF is completely opaque. The expression order is "Aabbggrr", where Aa=alpha (00 to FF), Bb=blue (00 to FF), Gg=green (00 to FF), and rr=red (00 to FF). For example, if you want to apply a blue 50% opacity to an overlay, you should specify the following values: 7fff0000

The transparency of the set color can be done by the Setalpha method of the paint class. Because there are many times when you do not need to make a photo completely transparent, the experiment makes a program that adjusts the transparency of the image.

Example 1
This example changes the alpha value (transparency) of the color in the bitmap using a slider (SeekBar) component. The code for the MyView class that displays the bitmap is as follows:

Private classMyView extends view{PrivateBitmap Bitmap;  PublicMyView (Context context) {super (context); InputStream is=getresources (). Openrawresource (R.drawable.image); Bitmap= Bitmapfactory.decodestream ( is);  SetBackgroundColor (Color.White); } @Overrideprotected voidOnDraw (canvas canvas) {Paint Paint=NewPaint ();     Paint.setalpha (Alpha); Canvas.drawbitmap (Bitmap,NewRect (0,0, Bitmap.getwidth (), Bitmap. GetHeight ()),NewRect (Ten,Ten,310,235), paint); }}

The 2nd parameter of the Drawbitmap method in the preceding code represents the copy area of the in-place diagram, in this case the entire in-place map is copied. The 3rd parameter represents the target area of the drawing.
The code for the Onprogresschanged time method of the Seekbar component is as follows:

 Public void int Progress,      Boolean fromuser)  {    = progress;    Settitle ("Alpha:" + progress);    Myview.invalidate ();  }

To run this example, move the slider to the left and right positions, and you will see the effect of the following two graphs:

Example 2
Alpha Transparent gradient animation.

<alpha> tag as alpha transparency node
Android:fromalpha= "1.0" setting animation starting with a transparency of 1.0 means completely opaque
Android:toalpha= "0.0" Set animation end transparency to 0.0 for full transparency
That is, the alpha value range is between 0.0-1.0

This animation layout sets the animation from a completely opaque gradient to full transparency.

<?xmlversion="1.0"encoding="Utf-8"?><alphaxmlns:android="http://schemas.android.com/apk/res/android"Android:fromalpha="1.0"Android:toalpha="0.0"Android:repeatcount="Infinite"android:duration=" -"></alpha>

Code implementation

23456789Ten One A - - the - - - + - + A at - - - - - inImport android.app.Activity; import android.os.Bundle; import android.view.animation.Animation; import Android.view.animation.AnimationUtils;  Import Android.widget.ImageView;  PublicclassAlphaactivity extends Activity {/** Display the imageview* of the animation*/ImageView Mimageview=NULL; /** Transparent animation **/Animation manimation=NULL; @Override PublicvoidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (r.layout.translate); /** Get ImageView Object **/Mimageview=(ImageView) Findviewbyid (R.id.imageview); /** Load Transparent animation **/manimation= Animationutils.loadanimation ( This, R.anim.alpha); /** Play Transparent animation **/mimageview.startanimation (manimation); } }  

Android set alpha value to make an instance of transparent and gradient effect

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.