Add a picture in the Android UI how to make a part of this picture transparent

Source: Internet
Author: User
Tags transparent color

You can take advantage of the canvas canvas class, which is a concrete way to see the official API.
Http://developer.android.com/reference/android/graphics/Canvas.html
Here's the idea: do some processing of your pictures, such as making the upper part fully visible, and getting the next part translucent.
1, first convert your original image into a bitmap bitmap;
2, first create canvas canvas, size is your original image size;
3, create Brush paint class, set brush properties transparency, color, etc.;
4. Using the Canvas class Drawbitmap (int[] colors, int offset, int stride, int x, int y, int width, int height, Boolean hasalpha, Paint Paint) function to create the. Specifically, look at the API.
5, Canvas.save ();

Summary: Canvas painting do not think how difficult it is, the theory is, how to hold the pen (paint property), how to (canvas properties, such as size, etc.) on the canvases (canvas), how to draw (canvas drawbitmap function) of the graph.

Change the transparent color of the view in Android

First, we have a preliminary understanding of the transparent color.

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); 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

View v = Findviewbyid (r.id.content);//Find the ID of the layout you want to set a transparent background for (you can set a background image for this view)

V.getbackground (). Setalpha (100);//0~255 Transparency Value

And there's a color.transparent that also means transparency.

Android Settings picture bitmap any transparencyCategory: Android development 2014-06-09 23:38 3122 people read reviews (6) favorite reports Bitmap Transparent

In two ways, the first idea is to get the bitmap pixel value into a int[] array by manipulating the bitmap, because bitmap is usually the ARGB8888 format in Android, so the highest bit is the value of a channel, After the alignment is changed, create a new bitmap. The second idea is to change the transparency of the view by setting the transparency of the canvas's paint and then Canvas.drawbitmap (). The specific code is as follows:

The first way of thinking:

 Public StaticBitmap Gettransparentbitmap (Bitmap sourceimg,intNumber ) {        int[] Argb =New int[Sourceimg.getwidth () *sourceimg.getheight ()]; Sourceimg.getpixels (ARGB,0, Sourceimg.getwidth (), 0, 0, sourceimg. GetWidth (), Sourceimg.getheight ());//get the ARGB value of the picture Number= number * 255/100;  for(inti = 0; i < argb.length; i++) {Argb[i]= (number << 24) | (Argb[i] & 0X00FFFFFF); } sourceimg=Bitmap.createbitmap (ARGB, Sourceimg.getwidth (), sourceimg. GetHeight (), config.argb_8888); returnsourceimg; }

The above code by my pro-test available, fixed the link two errors. The range of number is 0-100,0, which means that it is completely transparent and completely invisible. One of the most critical steps you can see is argb[i] = (number << 24) | (Argb[i] & 0X00FFFFFF); By using (Argb[i] & 0X00FFFFFF), the A channel of the pixel of point I is set to 0, and then (NUM<<24) is performed or manipulated. Shift See links

The second way of thinking:

Example code:

    classDrawcanvasextendsView { PublicDrawcanvas (Context context) {Super(context); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //get the bitmap of resource picturesBitmap Vbitmap = Bitmapfactory.decoderesource ( This. Getresources (), R.drawable.icon             ); //Creating Paint ObjectsPaint Vpaint =NewPaint ();   Vpaint. SetStyle (Paint.Style.STROKE); //HollowVpaint. Setalpha (75);// Canvas.drawbitmap (Vbitmap,50, 100,NULL);//No TransparencyCanvas.drawbitmap (Vbitmap, vpaint);//have transparent        }    }

Canvas using Drawbitmap to draw pictures in AndroidCategory: Getting Started with Android

1, the basic method of drawing pictures


Bitmap: Picture object, left: offset to position, top: offset at top
Drawbitmap (Bitmap Bitmap, float left, float top, paint paint)

2, to the picture splicing and limits the display area

Drawbitmap (Bitmap Bitmap, Rect src, RECTF DST, paint paint);

Rect src: The image is cropped, and if NULL, the entire picture is displayed

RECTF DST: Is the area that the picture displays in the canvas canvas,
Larger than SRC enlarges the SRC cut-off area,
Smaller than Src narrows the SRC cut-off area.

Add a picture in the Android UI how to make a part of this picture transparent

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.