Saturday, October 6, 2012

Create scaled bitmap using Matrix

Example to create scaled bitmap using Matrix.postScale() method. Refer to scaleBitmap() method in the code.

Create scaled bitmap using Matrix

package com.example.androidmatrix;

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

public class MainActivity extends Activity {
 
 ImageView imageSource, imageTarget;
 Bitmap bitmapSource;
 
 SeekBar xBar, yBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageSource = (ImageView)findViewById(R.id.source);
        imageTarget = (ImageView)findViewById(R.id.target);
        xBar = (SeekBar)findViewById(R.id.xbar);
        yBar = (SeekBar)findViewById(R.id.ybar);
        
        bitmapSource = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        imageSource.setImageBitmap(bitmapSource);
        
        xBar.setOnSeekBarChangeListener(xyBarChangeListener);
        yBar.setOnSeekBarChangeListener(xyBarChangeListener);
        
        Bitmap nBM = scaleBitmap(bitmapSource, 1.0f, 1.0f);
        imageTarget.setImageBitmap(nBM);
    }
    
    OnSeekBarChangeListener xyBarChangeListener
    = new OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
   float xscale = (float)(xBar.getProgress())/200;
   float yscale = (float)(yBar.getProgress())/200;
         imageTarget.setImageBitmap(scaleBitmap(bitmapSource, xscale, yscale));
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }};

 private Bitmap scaleBitmap(Bitmap src, float xScale, float yScale){
  Matrix matrix = new Matrix();
  matrix.postScale(xScale, yScale);
     Bitmap scaledBitmap = Bitmap.createBitmap(
       bitmapSource, 
       0, 
       0, 
       src.getWidth(), 
       src.getHeight(), 
       matrix, 
       true);
     
     return scaledBitmap;
 }

}


<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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <ImageView
        android:id="@+id/source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <SeekBar
        android:id="@+id/xbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="400"
        android:progress="100"/>
    <SeekBar
        android:id="@+id/ybar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="400"
        android:progress="100"/>

</LinearLayout>


download filesDownload the files.

No comments: