android textview速度慢,甚至根本不起作用

llycmphe  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(203)

我做了一个非常简单的应用程序,它在一个窗口中显示当前日期 Textview 文本大小为120dp。我想增加和减少一天的日期,每刷右或刷左的手势。
我的代码工作,但它的工作非常缓慢,特别是刷左手势,以减少日期。有时它拒绝工作。
这是我的密码:

  1. Calendar c;
  2. Int day,month,year;
  3. public abstract class OnSwipeTouchListener implements View.OnTouchListener {
  4. public final GestureDetector gestureDetector;
  5. public OnSwipeTouchListener (Context ctx){
  6. gestureDetector = new GestureDetector(ctx, new GestureListener());
  7. }
  8. private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
  9. private static final int SWIPE_THRESHOLD = 100;
  10. private static final int SWIPE_VELOCITY_THRESHOLD = 100;
  11. @Override
  12. public boolean onDown(MotionEvent e) {
  13. return true;
  14. }
  15. @Override
  16. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  17. boolean result = false;
  18. try {
  19. float diffY = e2.getY() - e1.getY();
  20. float diffX = e2.getX() - e1.getX();
  21. if (Math.abs(diffX) > Math.abs(diffY)) {
  22. if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
  23. if (diffX > 0) {
  24. onSwipeRight();
  25. } else {
  26. onSwipeLeft();
  27. }
  28. }
  29. result = true;
  30. }
  31. else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
  32. if (diffY > 0) {
  33. onSwipeBottom();
  34. } else {
  35. onSwipeTop();
  36. }
  37. }
  38. result = true;
  39. } catch (Exception exception) {
  40. exception.printStackTrace();
  41. }
  42. return result;
  43. }
  44. }
  45. public void onSwipeRight() {
  46. }
  47. public void onSwipeLeft() {
  48. }
  49. public void onSwipeTop() {
  50. }
  51. public void onSwipeBottom() {
  52. }
  53. }
  54. textView.setOnTouchListener(new OnSwipeTouchListener(this) {
  55. public void onSwipeTop() {}
  56. public void onSwipeRight() {
  57. c.add(Calendar.DATE, 1);
  58. day = c.get(Calendar.DAY_OF_MONTH);
  59. month = c.get(Calendar.MONTH) +1;
  60. year = c.get(Calendar.YEAR);
  61. textView.setText(Integer.toString(day));
  62. }
  63. public void onSwipeLeft() {
  64. c.add(Calendar.DATE, -1);
  65. day = c.get(Calendar.DAY_OF_MONTH);
  66. month = c.get(Calendar.MONTH) +1;
  67. year = c.get(Calendar.YEAR);
  68. textView.setText(Integer.toString(day));
  69. }
  70. public void onSwipeBottom() {
  71. textView.setText("DOWN");
  72. }
  73. public boolean onTouch(View v, MotionEvent event) {
  74. return gestureDetector.onTouchEvent(event);
  75. }
  76. });

为什么这段代码的性能这么差?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题