编辑:现在发布整个班级,而不仅仅是内部自定义监听器
几个星期以来,我一直把这个问题记在脑海里,仍然没有答案。我想制作一个“照片视图活动”,用户可以在整个屏幕上看到照片。用户必须能够双击缩放,捏合缩放以及能够在向左/向右滑动时退出活动。现在我能够做到以下几点: 滑动退出 捏/双击缩放(Photo attacher)
但是,如果我尝试同时使用两者,系统会认为我正在尝试缩放并且不会触发滑动的侦听器。因为照片附件很简单,所以这里有一些滑动部分的代码:内在的类
public class PhotoShowActivity extends Activity{
// Toolbar bar;
PhotoView photoToShow;
PhotoViewAttacher attacher = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_show);
//set up bar
// bar = (Toolbar) findViewById(R.id.toolbar_with_back_key);
// bar.setTitle("");
//setSupportActionBar(bar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setDisplayShowHomeEnabled(true);
photoToShow = (PhotoView) findViewById(R.id.photo_show);
Intent i = getIntent();
try
{
File receive = (File) i.getSerializableExtra("image");
// getSupportActionBar().setTitle(i.getStringExtra("title"));
Glide.with(this).load(receive).into(photoToShow);
}catch(Exception e){finish();}
attacher = new PhotoViewAttacher(photoToShow);
photoToShow.setOnTouchListener(new OnSwipeTouchListener(PhotoShowActivity.this)
{
public void onSwipeRight() {
PhotoShowActivity.this.finish();
}
public void onSwipeLeft() {
PhotoShowActivity.this.finish();
}
public void onSwipeTop() {
// PhotoShowActivity.this.finish();
}
public void onSwipeBottom() {
// PhotoShowActivity.this.finish();
}
});
}
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (attacher.getScale() == 1.0) {
gestureDetector.onTouchEvent(event);
}
return true;
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
}
这里我们将侦听器绑定到图像视图
imageView.setOnTouchListener(new OnSwipeTouchListener(PhotoShowActivity.this){
public void onSwipeRight() {
PhotoShowActivity.this.finish();
}
public void onSwipeLeft() {
PhotoShowActivity.this.finish();
}
public void onSwipeTop() {
// PhotoShowActivity.this.finish();
}
public void onSwipeBottom() {
// PhotoShowActivity.this.finish();
}
});
关于如何组合缩放和滑动的任何想法?
答案 0 :(得分:0)
您需要做的就是检查ImageView是否被缩放。如果它已放大,则滑动左右功能不起作用。 PhotoView库提供了检查ImageView缩放级别的功能。我遇到了同样的问题,我已经想通了。您可以在下面找到以下代码段作为参考,
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (imgPdfView.getCurrentZoom() == 1) {
mGestureDetector.onTouchEvent(motionEvent);
}
return true;
}
如果imgPdfView.getCurrentZoom()==1
,则表示您的ImageView处于正常状态。因此,现在您可以向左滑动或向右滑动以获得更多功能。