Android uses the 2D method to implement reflection effects

Source: Internet
Author: User
Tags image flip

Each image pixel is displayed in a 4-byte integer. The maximum byte is used as the alpha channel. In other words, it is used for transparent/opaque control. 255 indicates full opacity; 0 indicates full transparency. The next byte is the red channel; 255 indicates that it is completely red. And so on, the next two bytes implement the green and blue channels accordingly.

Now you can process individual pixels. By using {
Tagshow (event)
} "> Android. graphics. Bitmap {
Tagshow (event)
} "> GetPixels In the API, which can be {
Tagshow (event)
} "> Loads pixels into an integer array. In this article {
Tagshow (event)
} "> In the example, you will color each pixel according to certain rules. After this processing, all pixels will be converted into a byte code ranging from 0 to 255. SetPixels In the android. graphics. Bitmap API is used to load this integer array into an image. The last step is through Image {
Tagshow (event)
} "> View variable mIV to update {
Tagshow (event)
} "> Screen. The following are the processes for implementing this dyeing process {
Tagshow (event)
} "> Code snippet.

Private void TintThePicture (int deg ){

Int [] pix = new int [picw * pich];

MBitmap. getPixels (pix, 0, picw, 0, 0, picw, pich );

Int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;

Double angle = (3.14159d * (double) deg)/180.0d;

Int S = (int) (256.0d * Math. sin (angle ));

Int C = (int) (256.0d * Math. cos (angle ));

For (int y = 0; y <pich; y ++)

For (int x = 0; x <picw; x ++)

{

Int index = y * picw + x;

Int r = (pix [index]> 16) & 0xff;

Int g = (pix [index]> 8) & 0xff;

Int B = pix [index] & 0xff;

RY = (70 * r-59 * g-11 * B)/100;

GY = (-30 * r + 41 * g-11 * B)/100;

BY = (-30 * r-59 * g + 89 * B)/100;

Y = (30 * r + 59 * g + 11 * B)/100;

RYY = (S * BY + C * RY)/256;

BYY = (C * BY-S * RY)/256;

GYY = (-51 * RYY-19 * BYY)/100;

R = Y + RYY;

R = (R <0 )? 0: (R> 255 )? 255: R );

G = Y + GYY;

G = (G <0 )? 0: (G> 255 )? 255: G );

B = Y + BYY;

B = (B <0 )? 0: (B> 255 )? 255: B );

Pix [index] = 0xff000000 | (R <16) | (G <8) | B;

}

Bitmap bm = Bitmap. createBitmap (picw, pich, false );

Bm. setPixels (pix, 0, picw, 0, 0, picw, pich );

// Put the updated bitmap into the main view

MIV. setImageBitmap (bm );

MIV. invalidate ();

MBitmap = bm;

Pix = null;

}

 

 

Implement reflection effects using 2D Methods

Reflection can be UI {
Tagshow (event)
} "> The interface brings a stereoscopy, which is a common UI special effect. Is An Image Browsing {
Tagshow (event)
} "> In the program's GridView, the reflection effect of this View adds a lot of colors to the UI interface.


Implementation Principle

To implement reflection, you can use 3D interface methods such as OpenGL or 2D method {
Tagshow (event)
} "> Simulation.

To implement reflection using the 2D method, we need to consider the following two aspects:

1. The reflection is an image that is flipped up or down;

2. The transparency increases from top to bottom.


Image flip implementation

In principle, the image flip is actually to convert the image {
Tagshow (event)
} "> Data is swapped between upper and lower rows.

Bitmap bm = Bitmap. createBitmap (w, h, Bitmap. Config. ARGB_8888 );

 

For (int y = 0; y

{

Bm. setPixels (srcPix, y * w, w, 0, h-y-1, w, 1 );

}

 

 

Transparency gradient

There are two ways to achieve transparency gradient: one is to use the mask; the other is to directly modify the pixel data and modify each
The alpha value of the pixel.

For the mask method, make a transparency gradient image in advance. This is our mask. After the image is drawn, draw the mask image. This produces the transparency gradient {
Tagshow (event)
} "> Effect.

For the second method, we need to first read the image data and then modify the alpha value of each pixel. The following code snippet {
Tagshow (event)
} "> The function is to add the alpha value row by row, and the effect is from top to bottom from the dark
Brightness.

Int alpha = 0x00000000;

 

MBitmap. getPixels (pix, 0, w, 0, 0, w, h );

 

For (int y = 0; y

For (int x = 0; x <w; x ++ ){

Int index = y * w + x;

Int r = (pix [index]> 16) & 0xff;

Int g = (pix [index]> 8) & 0xff;

Int B = pix [index] & 0xff;

 

Pix [index] = alpha | (r <16) | (g <8) | B;

}

Alpha = alpha + 0x01000000;

 

}

{
ShowMenu ({'ctrlid': this. id, 'pos': '13 '})
} ">

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.