我做了一个非常简单的应用程序,它在一个窗口中显示当前日期 Textview
文本大小为120dp。我想增加和减少一天的日期,每刷右或刷左的手势。
我的代码工作,但它的工作非常缓慢,特别是刷左手势,以减少日期。有时它拒绝工作。
这是我的密码:
Calendar c;
Int day,month,year;
public abstract class OnSwipeTouchListener implements View.OnTouchListener {
public final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
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() {
}
}
textView.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeTop() {}
public void onSwipeRight() {
c.add(Calendar.DATE, 1);
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH) +1;
year = c.get(Calendar.YEAR);
textView.setText(Integer.toString(day));
}
public void onSwipeLeft() {
c.add(Calendar.DATE, -1);
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH) +1;
year = c.get(Calendar.YEAR);
textView.setText(Integer.toString(day));
}
public void onSwipeBottom() {
textView.setText("DOWN");
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
为什么这段代码的性能这么差?
暂无答案!
目前还没有任何答案,快来回答吧!