Dec 13, 2011

Copy bitmap using getPixels() and setPixels()

Example of copy bitmap using Bitmap.getPixels() and Bitmap.setPixels().

Copy bitmap using getPixels() and setPixels()

package com.AndroidCopyBitmap;

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

public class AndroidCopyBitmapActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image1 = (ImageView)findViewById(R.id.image1);
ImageView image2 = (ImageView)findViewById(R.id.image2);

Bitmap oldBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

int orgWidth = oldBitmap.getWidth();
int orgHeight = oldBitmap.getHeight();

Bitmap newBitmap = Bitmap.createBitmap(orgWidth, orgHeight, Bitmap.Config.ARGB_8888);
int[] pixels = new int[orgWidth * orgHeight];
oldBitmap.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);
newBitmap.setPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);

image1.setImageBitmap(oldBitmap);
image2.setImageBitmap(newBitmap);
}
}


<?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/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

next:
- Copy part of Bitmap



No comments:

Post a Comment

Infolinks In Text Ads