如何在安卓中实现每隔5秒更换图片。

huangapple 未分类评论48阅读模式
标题翻译

How can I change image after every 5 second in Android

问题

  1. public class MainActivity extends AppCompatActivity {
  2. private ImageView im_car;
  3. private Handler mHandler = new Handler();
  4. public static Integer[] mThumbeId = {R.drawable.first, R.drawable.second, R.drawable.thard};
  5. public int i = 0, id = 0;
  6. private String KEY_ID = "key_id";
  7. @SuppressLint("ClickableViewAccessibility")
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. im_car = findViewById(R.id.im_car);
  13. mThumbRun.run();
  14. if (savedInstanceState != null) {
  15. i = savedInstanceState.getInt(KEY_ID);
  16. }
  17. im_car.setOnTouchListener(new View.OnTouchListener() {
  18. @Override
  19. public boolean onTouch(View v, MotionEvent event) {
  20. switch (event.getAction()) {
  21. case MotionEvent.ACTION_DOWN:
  22. mHandler.removeCallbacks(mThumbRun);
  23. break;
  24. case MotionEvent.ACTION_UP:
  25. if (i!=0)
  26. {
  27. i = i-1;
  28. }
  29. else i=2;
  30. mThumbRun.run();
  31. break;
  32. }
  33. return false;
  34. }
  35. });
  36. }
  37. @Override
  38. protected void onRestart() {
  39. super.onRestart();
  40. if (i!=0)
  41. {
  42. i = i-1;
  43. }
  44. else i=2;
  45. mThumbRun.run();
  46. }
  47. Runnable mThumbRun = new Runnable() {
  48. @Override
  49. public void run() {
  50. im_car.setImageResource(mThumbeId[i]);
  51. i++;
  52. if (i >= mThumbeId.length) {
  53. i = 0;
  54. }
  55. mHandler.postDelayed(this, 5000);
  56. }
  57. };
  58. }
英文翻译

I need to change image every 5 seconds, then when we touch the ImageView it will stop changing until we release the touch.
When the application go to OnPause state it pauses the auto changing of the image.

I save the KEY_ID and stop the runnable in onSaveInstanceState

How can I make this change automatically ? Below is my code in java.


  1. public class MainActivity extends AppCompatActivity {
  2. private ImageView im_car;
  3. private Handler mHandler = new Handler();
  4. public static Integer[] mThumbeId = {R.drawable.first, R.drawable.second, R.drawable.thard};
  5. public int i = 0, id = 0;
  6. private String KEY_ID = "key_id";
  7. @SuppressLint("ClickableViewAccessibility")
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. im_car = findViewById(R.id.im_car);
  13. mThumbRun.run();
  14. if (savedInstanceState != null) {
  15. i = savedInstanceState.getInt(KEY_ID);
  16. }
  17. im_car.setOnTouchListener(new View.OnTouchListener() {
  18. @Override
  19. public boolean onTouch(View v, MotionEvent event) {
  20. switch (event.getAction()) {
  21. case MotionEvent.ACTION_DOWN:
  22. mHandler.removeCallbacks(mThumbRun);
  23. break;
  24. case MotionEvent.ACTION_UP:
  25. if (i!=0)
  26. {
  27. i = i-1;
  28. }
  29. else i=2;
  30. mThumbRun.run();
  31. break;
  32. }
  33. return false;
  34. }
  35. });
  36. }
  37. @Override
  38. protected void onRestart() {
  39. super.onRestart();
  40. if (i!=0)
  41. {
  42. i = i-1;
  43. }
  44. else i=2;
  45. mThumbRun.run();
  46. }
  47. Runnable mThumbRun = new Runnable() {
  48. @Override
  49. public void run() {
  50. im_car.setImageResource(mThumbeId[i]);
  51. i++;
  52. if (i >= mThumbeId.length) {
  53. i = 0;
  54. }
  55. mHandler.postDelayed(this, 5000);
  56. }
  57. };
  58. }

答案1

得分: 0

你可以在你的ImageView上创建一个触摸监听器。

保持一个布尔值来检查图像是否应该改变。

  1. boolean imageChangePermission = true;
  2. yourImageView.setOnTouchListener(new OnTouchListener () {
  3. public boolean onTouch(View view, MotionEvent event) {
  4. if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
  5. Log.d("TouchTest", "Touch down");
  6. }
  7. else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
  8. Log.d("TouchTest", "Touch up");
  9. imageChangePermission = false;
  10. }
  11. }
  12. }

ACTION_DOWN - 当你第一次触摸时

ACTION_MOVE - 当你在屏幕上移动手指时

ACTION_UP - 当你从屏幕上移开手指时

创建一个Handler来定期调用你的changeImage方法。

  1. Handler handler = new Handler();
  2. handler.postDelayed(new Runnable() {
  3. @Override
  4. public void run() {
  5. if(imageChangePermission) {
  6. changeImage();
  7. }
  8. }
  9. }, 5000); //每5秒后执行一次
英文翻译

You can create a touch listener in your imageview.

Keep a boolean to check wether image should change.

  1. boolean imageChangePermission = true;
  2. yourImageView.setOnTouchListener(new OnTouchListener () {
  3. public boolean onTouch(View view, MotionEvent event) {
  4. if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
  5. Log.d("TouchTest", "Touch down");
  6. }
  7. else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
  8. Log.d("TouchTest", "Touch up");
  9. imageChangePermission = false;
  10. }
  11. }
  12. }

ACTION_DOWN - when you first touch

ACTION_MOVE - when you are moving your finger on screen

ACTION_UP - when you remove your finger from screen

Create a Handler to periodically call the change Image method of yours.

  1. Handler handler = new Handler();
  2. handler.postDelayed(new Runnable() {
  3. @Override
  4. public void run() {
  5. if(imageChangePermission) {
  6. changeImage();
  7. }
  8. }
  9. },5000); //Run after every 5 second

huangapple
  • 本文由 发表于 2020年3月16日 20:17:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/60705873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定