Sunday, March 8, 2015

Scale bitmap using Matrix

This example show how to create scaled bitmap using Matrix.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

 SeekBar xScaleBar, yScaleBar;
 ImageView image1, image2;

 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);

  bitmapOriginal = BitmapFactory.decodeResource(getResources(),
    R.drawable.ic_launcher);
  image1.setImageBitmap(bitmapOriginal);
  
  xScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);
  yScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);
  
  Toast.makeText(MainActivity.this, 
    bitmapOriginal.getWidth() + " x " + bitmapOriginal.getHeight(), 
    Toast.LENGTH_LONG).show();
  
  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 = xScaleBar.getProgress()/10.0f;
  float yScale = yScaleBar.getProgress()/10.0f;

  if(xScale<=0){
   xScale = 0.1f;
  }
  
  if(yScale<=0){
   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, true);

  image2.setImageBitmap(bitmapScaled);

 }

}

<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="30"
        android:progress="10" />

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

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

</LinearLayout>


No comments: