Scale listener : ListActivity « UI « Android






Scale listener

   

package app.test;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class Test extends Activity {
  private ImageView image;
  private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1f;
  private Matrix mMatrix = new Matrix();
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView)findViewById(R.id.image);
        mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());
    }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
        mScaleDetector.onTouchEvent(ev);
        return true;
  }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
        public boolean onScale(ScaleGestureDetector detector) {
      mScaleFactor *= detector.getScaleFactor();
      mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
            mMatrix.setScale(mScaleFactor, mScaleFactor);
            image.setImageMatrix(mMatrix);
            image.invalidate();
            return true;
        }
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"  android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="fill_parent" >

  <TextView  android:text="Use the pinch gesture to change the image size"
    android:layout_width="fill_parent"  android:layout_height="wrap_content" />

  <ImageView android:id="@+id/image"  android:src="@drawable/icon"
    android:layout_width="match_parent"  android:layout_height="match_parent"
    android:scaleType="matrix" />

</LinearLayout>

   
    
    
  








Related examples in the same category

1.A list view example where the data for the list comes from an array of strings.
2.A list view example where the data comes from a cursor.
3.Using ListActivity
4.SimpleCursorAdapter and ListActivity
5.Get ListActivity selected index
6.Multi-column ListActivity
7.A list view example with separators.
8.List item click event
9.Dynamic List item
10.Self Wrapper for List
11.Static text for List view
12.List selection event
13.Simple List single choice
14.To do list app
15.Weather List Widget
16.Demonstrates expandable lists backed by Cursors
17.Demonstrates expandable lists backed by a Simple Map-based adapter
18.Demonstrates the using a list view in transcript mode
19.Demonstrates how a list can avoid expensive operations during scrolls or flings.
20.Demonstrates how to write an efficient list adapter.
21.A list view where the last item the user clicked is placed in the "activated" state, causing its background to highlight.
22.Calculate the minimum and maximum values out of a list of doubles.
23.Bounded Linked List
24.Adapter that simply returns row views from a list.
25.Diary app