we are going to create pinch to zoom IN/OUT in Android.
Step 1. For this we have created xml File as Follow.
Step 1. For this we have created xml File as Follow.
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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: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.gc.myapplication.MainActivity">
<TextView android:id="@+id/pinchZoom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pinch_title" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/pinchZoom" android:scaleType="matrix" android:src="@mipmap/amrit" android:maxHeight="100dp" android:id="@+id/pinchimage" android:maxWidth="50dp" android:contentDescription="@string/content_description" /></RelativeLayout>
Step 2. we have created Activity for Pinch Zoom IN/OUT.
package com.example.gc.myapplication; import android.graphics.Matrix; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { // ImageView imageView; ImageView imageView; Matrix matrix= new Matrix(); Float scale=1f; ScaleGestureDetector sgd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.pinchimage); sgd = new ScaleGestureDetector(this,new scaleListener()); } private class scaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{ @Override public boolean onScale(ScaleGestureDetector detector) { scale =scale*detector.getScaleFactor(); scale=Math.max(0.1f,Math.min(scale,5f)); matrix.setScale(scale,scale); imageView.setImageMatrix(matrix); return true; } } public boolean onTouchEvent(MotionEvent event){ sgd.onTouchEvent(event); return true; } }
No comments:
Post a Comment