Changes the partial transparency of the image to make the transparency Gradient based on the position.

Source: Internet
Author: User
Tags float number float range

Changes the partial transparency of the image to make the transparency Gradient based on the position.

Recently, when I was working on a project, I encountered a need to process an image to achieve the fuzzy transition of the edge.

After a long time, I finally used the following methods.

1. ARGB is used to create an image. We can directly retrieve the ARGB of the entire image and then change image A, that is, transparency.

 
Bitmap sourceImg;

int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
SourceImg. getPixels (argb, 0, sourceImg. getWidth (), 0, 0, sourceImg

. GetWidth (), sourceImg. getHeight ());

// Obtain the ARGB value of the image and put it into the argb Array
 
 

We have obtained the ARGB value of the image, and we only need to change Transparency.

2. We can use

// The range of number is 0-100. 0 indicates full transparency, and indicates opacity.
Float number = 100;
// Float alpha = number * 255/100;
argb[i] = ((int) alpha << 24) | (argb[i] & 0x00FFFFFF);

In the last sentence, only the Alpha value of the image is changed. (argb [I] & 0x00FFFFFF), all the values of A are set to 0, and then the (int) alpha <24) operation is performed or, then we can set our Alpha value. We shift (int) alpha) to the left 24 bits to avoid changing RGB.

 

3. Finally, use the following code to create a bitmap that changes the transparency.

sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg                .getHeight(), Bitmap.Config.ARGB_8888);

 

What I need to implement is the border fuzzy transition, so we need to make the transparency gradually change to 0 as the Y coordinate of the image, that is, the transition area is 0. My code is as follows:

/*** Sets the image transparency gradient from top to bottom to make the bottom edge smooth transition (note that it only follows the Y coordinate change) ** @ param sourceImg * @ return */public static Bitmap getTransAlphaBitmap (Bitmap sourceImg ){
Int [] argb = new int [sourceImg. getWidth () * sourceImg. getHeight ()]; sourceImg. getPixels (argb, 0, sourceImg. getWidth (), 0, 0, sourceImg. getWidth (), sourceImg. getHeight (); // obtain the ARGB value of the image // The range of number is 0-100, 0 is completely transparent, 100 is not transparent float number =;
// Float alpha = number * 255/100;
// The gradient range of the image (set the gradient from top to bottom only in the half of the image. The gradient is not displayed on the top, that is, the half that is close to the edge) float range = sourceImg. getHeight ()/2.0f;
// The gradient of transparency. The gradient changes with the Y coordinate each time, because the gradient must be 0 float pos = (number * 1.0f)/range at the edge;
// Subscript at the beginning of the loop, set the time from which to change int start = sourceImg. getWidth () * (sourceImg. getHeight ()-(int) range); for (int I = start; I <argb. length; I ++) {// The alpha value of the same row is not changed, because it is the if (I % sourceImg. getWidth () = 0) {number = number-pos; alpha = number * 255/100;} argb [I] = (int) alpha <24) | (argb [I] & 0x00FFFFFF);} sourceImg = Bitmap. createBitmap (argb, sourceImg. getWidth (), sourceImg. getHeight (), Bitmap. config. ARGB_8888); return sourceImg ;}

The above shows that the image edge is too large.

 

Reference: https://www.cnblogs.com/Anita9002/p/4207963.html

Reprinted please indicate everywhere: http://www.cnblogs.com/tangZH/p/8551632.html

Related Article

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.