我想在我的应用程序中左右滑动,但是当我使用PhotoView库时,不会触发Swipe事件。
如何使用PhotoView库实现滑动操作?
OnSwipeTouchListener.java
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class OnSwipeTouchListener implements View.OnTouchListener {
private GestureDetector gestureDetector;
public OnSwipeTouchListener(Context c) {
gestureDetector = new GestureDetector(c, new GestureListener());
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
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;
}
// Determines the fling velocity and then fires the appropriate swipe event accordingly
@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();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeDown();
} else {
onSwipeUp();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
}
My MainActivity我使用SwipeEvent和PhotoView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup Universial Image Loader
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config);
imageViewComic = (ImageView) findViewById(R.id.imageViewComic);
imageViewComic.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeDown() {
Toast.makeText(MainActivity.this, "Down", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "Left", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeUp() {
Toast.makeText(MainActivity.this, "Up", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwipeRight() {
Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
}
});
mAttacher = new PhotoViewAttacher(imageViewComic);
// Button for loading latest comic
btnLatestComic = (Button) findViewById(R.id.btnLoadComic);
btnLatestComic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute("http://xkcd.com/info.0.json");
}
});
// Button for loading previous comic
btnPrevComic = (Button) findViewById(R.id.btnPrevComic);
btnPrevComic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getPrevComic();
}
});
// Button for loading next comic
btnNextComic = (Button) findViewById(R.id.btnNextComic);
btnNextComic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getNextComic();
}
});
SharedPreferences Sharedsettings;
SharedPreferences.Editor editor;
Sharedsettings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean load_latest = Sharedsettings.getBoolean("load_latest_startup", true);
if( load_latest == true) {
//Load latest comic
new JSONParse().execute("http://xkcd.com/info.0.json");
}
else
{
getLastComic();
}
}
答案 0 :(得分:0)
使用此类创建fling函数,它将简单地扩展SimpleOnGestureListener,然后使用该fling函数来滑动图像。我在这里使用ViewPager。
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
&& selectedPg == (pageCount - 1)) {
shop_view_pager.setCurrentItem(0);
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
&& selectedPg == 0) {
shop_view_pager.setCurrentItem(pageCount - 1);
return true;
}
} catch (Exception e) {
// nothing
}
return false;
}
}
现在为MyGestureDetector创建一个对象,并使用OnTouchListener设置手势功能。
final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
view_pager.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
view_pager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int position) {
selectedPg = position;
}