Android Course---How Android uses the slider (SeekBar) component to set the transparency of a picture's color (go)

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.

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.

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 classMyViewextendsView {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 (10, 10, 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:

This article transferred from: http://blog.csdn.net/pku_android/article/details/7625850

Android Course---How Android uses the slider (SeekBar) component to set the transparency of a picture's color (go)

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.