Monday, March 9, 2015

Flip bitmap using Matrix

Example to create flipped 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.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

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

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

  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);
  image3.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="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="match_parent"
            android:layout_height="match_parent"
            android:background="#B0B0B0" />
    </LinearLayout>

</LinearLayout>


No comments: