Bitmap的像素操作, 用颜色填充Bitmap ,最后的效果图

Unnamed QQ Screenshot

1.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

2. TestBitmapPixelActivity

package com.zhai.test;

import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.widget.ImageView;

public class TestBitmapPixelActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView imageview = (ImageView) findViewById(R.id.imageView1);

        Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.RGB_565);
        int[] pixel = new int[300 * 300];
        Random rand = new Random();
        for (int i = 0; i < pixel.length; i++) {
            pixel[i] = rand.nextInt(65535);
            // pixel[i] = Color.RED;
        }
        bitmap.setPixels(pixel, 0, 300, 0, 0, 300, 300);
        // BitmapDrawable bmpDraw=new BitmapDrawable(bitmap);
        imageview.setImageBitmap(bitmap);

    }
}

 

源码本地下载: