Today, I wrote a method to handle the onDoubleTap event. Double-click the program to zoom in and out the screen event, double-click the screen image to zoom in, and then double-click the screen image to zoom in to the original effect. See the results!
Source image: double-click the screen to enlarge the effect:
Paste the code below:
I. Code in MainActivty. java class:
Package com.cn. android;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. GestureDetector;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. view. GestureDetector. OnGestureListener;
Public class mainActivity extends Activity implements OnGestureListener {
Private ImageShowView mImageShowView = null;
Private GestureDetector gesturetor;
Private double size = 1.0;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MImageShowView = new ImageShowView (this );
SetContentView (mImageShowView );
Gesturetor = new GestureDetector (this );
Gestureener. setOnDoubleTapListener (new GestureDetector. OnDoubleTapListener (){
@ Override
Public boolean onDoubleTap (MotionEvent e ){
// TODO Auto-generated method stub
// Double-click
If (Scale = 1.0 ){
Scale = Scale + 0.5;
MImageShowView. setScale (Scale );
}
Else if (Scale = 1.5 ){
Scale = Scale-0.5;
MImageShowView. setScale (Scale );
}
Log. v ("daming", "onDoubleTap ");
Return true;
}
@ Override
Public boolean onDoubleTapEvent (MotionEvent e ){
// TODO Auto-generated method stub
// Double-click is generated twice
Log. v ("daming", "onDoubleTapEvent ");
Return false;
}
@ Override
Public boolean onSingleTapConfirmed (MotionEvent e ){
// One click
Log. v ("daming", "onSingleTapConfirmed ");
Return false;
}
});
}
@ Override
Public boolean onTouchEvent (MotionEvent me ){
Return gesturetries. onTouchEvent (me );
}
@ Override
Public boolean onDown (MotionEvent e ){
// TODO Auto-generated method stub
Return false;
}
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
// TODO Auto-generated method stub
Return false;
}
@ Override
Public void onLongPress (MotionEvent e ){
// TODO Auto-generated method stub
}
@ Override
Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX,
Float distanceY ){
// TODO Auto-generated method stub
Return false;
}
@ Override
Public void onShowPress (MotionEvent e ){
// TODO Auto-generated method stub
}
@ Override
Public boolean onSingleTapUp (MotionEvent e ){
// TODO Auto-generated method stub
Return false;
}
}
Ii. Code in ImageShowView. java:
Package com.cn. android;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Matrix;
Import android. graphics. drawable. BitmapDrawable;
Import android. view. KeyEvent;
Import android. view. View;
Public class ImageShowView extends View implements Runnable {
Bitmap mBitmap = null;
Int mBitmapWidth = 0; // defines the image width.
Int mBitmapHeight = 0; // defines the Image Height.
Private double size = 1.0;
Matrix mMatrix = new Matrix ();
Public ImageShowView (Context context ){
Super (context );
MBitmap = (BitmapDrawable) getResources (). getDrawable (R. drawable. image1). getBitmap ();
MBitmapWidth = mBitmap. getWidth ();
MBitmapHeight = mBitmap. getHeight ();
// Enable the thread
New Thread (this). start ();
}
Public void setScale (double scale ){
This. Scale = scale;
}
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
MMatrix. reset ();
MMatrix. postScale (float) Scale, (float) Scale); // you can specify a Scale.
Bitmap mBitmap2 = Bitmap. createBitmap (mBitmap, 0, 0, mBitmapWidth, mBitmapHeight, mMatrix, true );
// Draw the rotated image
ImageShowView. drawImage (canvas, mBitmap2, (320-mBitmapWidth)/2,10 );
MBitmap2 = null;
}
@ Override
Public void run (){
While (! Thread. currentThread (). isInterrupted ()){
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
PostInvalidate ();
}
}
Private static void drawImage (Canvas canvas, Bitmap bitmap, int x, int y ){
// TODO Auto-generated method stub
Canvas. drawBitmap (bitmap, x, y, null );
}
}
Note: The above is all my code, and the images in the project will not be uploaded. You can upload an image by yourself. If you need the complete code, you can leave the email address, if you have any questions, please leave a message and I will reply to them! If you have good suggestions, you can come up with them, learn together, and make progress together!
From Daming zeroson's android learning history