Merge bitmaps

Example show how to combin two bitmaps to one.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

SeekBar xScaleBar, yScaleBar;
ImageView image1, image2, image3, image4;

Bitmap bitmapOriginal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

xScaleBar = (SeekBar) findViewById(R.id.xscale);
yScaleBar = (SeekBar) findViewById(R.id.yscale);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image4 = (ImageView) findViewById(R.id.image4);

bitmapOriginal = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
image1.setImageBitmap(bitmapOriginal);

xScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);
yScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);

ReloadImage();

}

OnSeekBarChangeListener OnScaleChangeListener = new OnSeekBarChangeListener() {

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
ReloadImage();
}
};

private void ReloadImage() {

float xScale = (float)(xScaleBar.getProgress()-10) / 10.0f;
float yScale = (float)(yScaleBar.getProgress()-10) / 10.0f;

//between +1 and -1,
//cannot be 0
if (xScale >= 0 && xScale < 0.1f) {
xScale = 0.1f;
}else if(xScale < 0 && xScale > -0.1f){
xScale = -0.1f;
}

if (yScale >= 0 && yScale < 0.1f) {
yScale = 0.1f;
}else if(yScale < 0 && yScale > -0.1f){
yScale = -0.1f;
}

// create scaled bitmap using Matrix
Matrix matrix = new Matrix();
matrix.postScale(xScale, yScale);

Bitmap bitmapScaled = Bitmap.createBitmap(bitmapOriginal, 0, 0,
bitmapOriginal.getWidth(), bitmapOriginal.getHeight(), matrix,
false);

image2.setImageBitmap(bitmapScaled);

//Merge two bitmaps to one
Bitmap bitmapMerged = Bitmap.createBitmap(
bitmapOriginal.getWidth(),
bitmapOriginal.getHeight(),
bitmapOriginal.getConfig());
Canvas canvasMerged = new Canvas(bitmapMerged);
canvasMerged.drawBitmap(bitmapOriginal, 0, 0, null);
canvasMerged.drawBitmap(bitmapScaled, 0, 0, null);

image3.setImageBitmap(bitmapMerged);
image4.setImageBitmap(bitmapMerged);

}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/xscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="20" />

<SeekBar
android:id="@+id/yscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="20" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<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"
android:background="#D0D0D0" />

<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B0B0B0" />

<ImageView
android:id="@+id/image4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#909090" />
</LinearLayout>

</LinearLayout>