When you create a gallery application, you will allow the user to zoom the image in and out through pinch (moving a finger and thumb a part or bringing them together). In this Android tip, I am going show you how to zoom an ImageView when the user makes a pinch on the screen.
<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:orientation="vertical"
tools:context="com.example.inputdialog.MainActivity" >
<ImageView
android:id="@+id/imgview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher"
/>
</RelativeLayout>
public class MainActivity extends Activity{
ImageView imgView;
ScaleGestureDetector scaleGDetector;
float scale=1f;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView=(ImageView)findViewById(R.id.imgview);
scaleGDetector=new ScaleGestureDetector(this, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
scaleGDetector.onTouchEvent(ev);
return true;
}
private class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener{
public boolean onScaleBegin(ScaleGestureDetector sgd){
return true;
}
public void onScaleEnd(ScaleGestureDetector sgd){
}
public boolean onScale(ScaleGestureDetector sgd){
// Multiply scale factor
scale*= sgd.getScaleFactor();
// Scale or zoom the imageview
imgView.setScaleX(scale);
imgView.setScaleY(scale);
Log.i("Main",String.valueOf(scale));
return true;
}
}
}
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.