Friday, January 25, 2013

Copy bitmap

Hi Guys!
Today I ma sharing the code how to copy the same bitmap image to new bitmap image.
Example of copy bitmap using Bitmap.getPixels() and Bitmap.setPixels().

activity_main.xml










MainAcitivity.java

package com.sunil.copybitmap;

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 MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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);
}
}

 You can download the source code Copy Bitmap

Cheers Guys!

Rocky

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Tipss.