How to modify the pixel information of a bitmap image in Android r g color

Source: Internet
Author: User

To modify the r g B information of a bitmap image, first obtain the color value of each vertex of the image and then calculate the corresponding r g B value based on the color value. loading an image is actually writing the RGB information of each image point into the memory. If the color information is modified dynamically, the image will be changed.

Modifying the color value of an image is actually useful in many places, I remember that when I used to develop a game based on a cell phone with a low memory, I couldn't load too many images in the memory at the same time. For example, when playing a strange game, players certainly don't want to see the same monsters every time. without increasing the memory, you can modify the r g B information of the image to give players a fresh feeling. This is the principle of the game palette.

Next I will introduce the code. The color of the microphone image in the two pictures below is white. Here I dynamically modify the color value in the middle of the image to make it work.

// Start the activity

package cn.m15.demo;import android.app.Activity;import android.os.Bundle;import android.view.Window;public class demoActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);    }}

// The layout file customizes a view to draw an image.

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:background="#888888"    android:layout_height="150dip" android:layout_width="120dip" >  <cn.m15.demo.RecordingViewandroid:id="@+id/uvMeter" android:layout_height="wrap_content" android:layout_width="wrap_content"android:gravity="center"/></RelativeLayout>

// Custom View

Package CN. m15.demo; import Java. util. random; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. drawable. bitmapdrawable; import android. util. attributeset; import android. view. view; public class recordingview extends view {paint mpaint; bitmap Mbitmap; int mbitmapwidth = 0; int mbitmapheight = 0; int marraycolor [] = NULL; int marraycolorlengh = 0; long starttime = 0; int mbackvolume = 0; Public recordingview (context) {super (context); Init (context);} public recordingview (context, attributeset attrs) {super (context, attrs); Init (context );} void Init (context) {mpaint = new paint (paint. anti_alias_flag); // a bitmap MB is created here. Itmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. ic_vd_mic_on); // set this bitmap as the background image setbackgrounddrawable (New bitmapdrawable (mbitmap); mbitmapwidth = mbitmap. getwidth (); mbitmapheight = mbitmap. getheight (); marraycolorlengh = mbitmapwidth * mbitmapheight; marraycolor = new int [marraycolorlengh]; int COUNT = 0; For (INT I = 0; I <mbitmapheight; I ++) {for (Int J = 0; j <Mbit Mapwidth; j ++) {// obtain the color value of each vertex in the bitmap image. Int color = mbitmap. getpixel (J, I); // Add the color value to an array to facilitate the subsequent modification of marraycolor [count] = color; // if you want to do more detail, you can take the r g B of the color value for response processing. I will not explain it more here. Int r = color. red (color); int G = color. green (color); int B = color. blue (color); count ++ ;}} starttime = system. currenttimemillis ();}/*** returns a random number ** @ Param botton * @ Param top * @ return */INT utilrandom (INT botton, int Top) {return (math. ABS (new random (). nextint () % (top + 1-botton) + botton);} @ overrideprotected void ondraw (canvas) {super. ondraw (canvas); // set the filled color area every 100 milliseconds if (system. currenttimemillis ()-starttime> = 100) {starttime = system. currenttimemillis (); setvolume (utilrandom (0,100);} // used to refresh the screen invalidate ();} public void setvolume (INT volume) {int starty = 0; int Endy = 0; Boolean isadd = False; // determine whether to fill the new region or restore the old region. If (mbackvolume> volume) {isadd = false; starty = getvalue (mbackvolume ); endy = getvalue (volume);} else {isadd = true; starty = getvalue (volume); Endy = getvalue (mbackvolume );} // There is no need to loop all vertices in the image every time, because it takes a lot of time. Int COUNT = starty * mbitmapwidth; // starts from the start point of the image to be filled or restored to the end point (INT I = starty; I <Endy; I ++) {for (Int J = 0; j <mbitmapwidth; j ++) {If (isadd) {// specify the color value to be filled. If not, it indicates that if the color is completely transparent or the return value is 0. // getpixel () without transparent channel getpixel32 () the transparent part is 0x00000000 // while the transparent black part is 0xff000000. If the transparent part is not calculated, it will be 0. Int color = mbitmap. getpixel (J, I); If (color! = 0) {mbitmap. setpixel (J, I, color. black) ;}} else {// if it is a restored color, assign the color of the current vertex to the array mbitmap that saves the color before. setpixel (J, I, marraycolor [count]) ;}count ++ ;}} mbackvolume = Volume ;} // calculate the actual filling height by percentage based on the image width and height. Public int getvalue (INT volume) {return mbitmapheight-(mbitmapheight * volume/100 );}}
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.