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

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

How can I change image after every 5 second in Android

问题

public class MainActivity extends AppCompatActivity {

    private ImageView im_car;
    private Handler mHandler = new Handler();
    public static Integer[] mThumbeId = {R.drawable.first, R.drawable.second, R.drawable.thard};
    public int i = 0, id = 0;
    private String KEY_ID = "key_id";

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        im_car = findViewById(R.id.im_car);

        mThumbRun.run();
        if (savedInstanceState != null) {
            i = savedInstanceState.getInt(KEY_ID);
        }
        im_car.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mHandler.removeCallbacks(mThumbRun);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (i!=0)
                        {
                            i = i-1;
                        }
                        else i=2;
                        mThumbRun.run();
                        break;
                }
                return false;
            }
        });

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (i!=0)
        {
            i = i-1;
        }
        else i=2;
        mThumbRun.run();
    }

    Runnable mThumbRun = new Runnable() {
        @Override
        public void run() {
            im_car.setImageResource(mThumbeId[i]);
            i++;
            if (i >= mThumbeId.length) {
                i = 0;
            }
            mHandler.postDelayed(this, 5000);
        }
    };
}
英文翻译

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.


public class MainActivity extends AppCompatActivity {

    private ImageView im_car;
    private Handler mHandler = new Handler();
    public static Integer[] mThumbeId = {R.drawable.first, R.drawable.second, R.drawable.thard};
    public int i = 0, id = 0;
    private String KEY_ID = "key_id";

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        im_car = findViewById(R.id.im_car);

        mThumbRun.run();
        if (savedInstanceState != null) {
            i = savedInstanceState.getInt(KEY_ID);
        }
        im_car.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mHandler.removeCallbacks(mThumbRun);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (i!=0)
                        {
                            i = i-1;
                        }
                        else i=2;
                        mThumbRun.run();
                        break;
                }
                return false;
            }
        });

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (i!=0)
        {
            i = i-1;
        }
        else i=2;
        mThumbRun.run();
    }

    Runnable mThumbRun = new Runnable() {
        @Override
        public void run() {
            im_car.setImageResource(mThumbeId[i]);
            i++;
            if (i >= mThumbeId.length) {
                i = 0;
            }
            mHandler.postDelayed(this, 5000);
        }
    };
}

答案1

得分: 0

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

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

boolean imageChangePermission = true;

yourImageView.setOnTouchListener(new OnTouchListener () {
  public boolean onTouch(View view, MotionEvent event) {

    if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
      Log.d("TouchTest", "Touch down");
    } 
    else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
      Log.d("TouchTest", "Touch up");
      imageChangePermission = false;
    }
  }
}

ACTION_DOWN - 当你第一次触摸时

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

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

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

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

You can create a touch listener in your imageview.

Keep a boolean to check wether image should change.

boolean imageChangePermission = true;


yourImageView.setOnTouchListener(new OnTouchListener () {
  public boolean onTouch(View view, MotionEvent event) {

    if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
      Log.d("TouchTest", "Touch down");
    } 
    else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
      Log.d("TouchTest", "Touch up");
      imageChangePermission = false;
    }
  }
}

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.

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if(imageChangePermission) {
                changeImage();
            }
        }
    },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:

确定