Android SubsamplingScaleImageView(subsampling-scale-image-view)单双击长按事件【系列2】

Android SubsamplingScaleImageView(subsampling-scale-image-view)单双击长按事件【系列2】


附录参考文章1介绍了Android SubsamplingScaleImageView(subsampling-scale-image-view)的简单使用方法。
在处理大图的浏览查看动作过程中,往往还有其他额外的事情需要处理,典型的以微信为例,在微信中,用户可以在消息列表中查看单击/双击放大/缩小消息列表中的图片,但还有一个事件:用户长按图片,弹出对话框,提示用户针对此图片的其他动作如转发。这项功能需要在SubsamplingScaleImageView(subsampling-scale-image-view)给当前的SubsamplingScaleImageView注册一个OnTouchListener事件侦听类,进而用Android的GestureDetector侦听处理有关SubsamplingScaleImageView的单击、双击、长按事件。
在附录文章1的基础上,增加上述‘功能,给出一个小demo。
测试的主Activity MainActivity.java:

package zhangphil.demo;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	private SubsamplingScaleImageView mSubsamplingScaleImageView;
	private GestureDetector gestureDetector;

	private final String fail_tips = "SubsamplingScaleImageView not ready !!!";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mSubsamplingScaleImageView = (SubsamplingScaleImageView) findViewById(R.id.subsamplingScaleImageView);
		mSubsamplingScaleImageView.setImage(ImageSource.asset("china.jpg"));

		gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				if (mSubsamplingScaleImageView.isReady()) {
					PointF sCoord = mSubsamplingScaleImageView.viewToSourceCoord(e.getX(), e.getY());
					Toast.makeText(getApplicationContext(), "单击: " + ((int) sCoord.x) + ", " + ((int) sCoord.y),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), fail_tips, Toast.LENGTH_SHORT).show();
				}

				return false;
			}

			@Override
			public void onLongPress(MotionEvent e) {
				if (mSubsamplingScaleImageView.isReady()) {
					PointF sCoord = mSubsamplingScaleImageView.viewToSourceCoord(e.getX(), e.getY());
					Toast.makeText(getApplicationContext(), "长按: " + ((int) sCoord.x) + ", " + ((int) sCoord.y),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), fail_tips, Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public boolean onDoubleTap(MotionEvent e) {
				if (mSubsamplingScaleImageView.isReady()) {
					PointF sCoord = mSubsamplingScaleImageView.viewToSourceCoord(e.getX(), e.getY());
					Toast.makeText(getApplicationContext(), "双击: " + ((int) sCoord.x) + ", " + ((int) sCoord.y),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), fail_tips, Toast.LENGTH_SHORT).show();
				}

				return false;
			}
		});

		mSubsamplingScaleImageView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				return gestureDetector.onTouchEvent(motionEvent);
			}
		});
	}
}


在此简单的说一下Android的事件传导派发流程。在Android的OnTouchListener中,用GestureDetector屏幕事件时候,在个别系统回调函数中的返回值:true,false。通常,
(1)如果返回true,是告知Android事件系统当前事件已经被消费(事件被拦截了),不要再继续传导派发此事件了。
(2)如果返回false,是告知Android事件系统,当前此事件没有被消费(事件没有被拦截,只是监听而已),要求Android系统继续传导派发此事件。
简言之,如果是拦截事件,就返回true;如果只是监听事件,则返回false。
在本例中,虽然我拦截了GestureDetector的onSingleTapConfirmed,onDoubleTap,但返回了false,返回false,是为了继续支持SubsamplingScaleImageView的单击、双击、长按事件。
运行结果如图所示:


附录参考文章:
【文章1】《Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】》链接地址:http://blog.csdn.net/zhangphil/article/details/49557549


你可能感兴趣的:(Android)