Mar 7, 2015

Create mirror bitmap using Matrix



package com.example.androidmirrorimage;

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

public class MainActivity extends ActionBarActivity {

 ImageView image1, image2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  image1 = (ImageView) findViewById(R.id.image1);
  image2 = (ImageView) findViewById(R.id.image2);

  Bitmap bm = BitmapFactory.decodeResource(getResources(),
    R.drawable.ic_launcher);
  
  image1.setImageBitmap(bm);
  image2.setImageBitmap(getMirrorBitmap(bm));

 }

 private Bitmap getMirrorBitmap(Bitmap src) {

  Matrix matrix = new Matrix();
  matrix.preScale(1, -1);
  Bitmap result = Bitmap.createBitmap(
    src, 0, 0, src.getWidth(), src.getHeight(), matrix, false);
  return result;
 }
}

<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.androidmirrorimage.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="28dp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads