【问题标题】:How to change images in imageview every n number of seconds如何每n秒更改一次imageview中的图像
【发布时间】:2015-04-18 06:51:51
【问题描述】:

我创建了一个数组,其中包含 int 中的所有图像,我想在 imageView 中每 3 秒更改一次这些图像,我尝试了所有我能找到的解决方案,但显示了一些错误,我想不通出去 。

java 文件 (home.java)

/**
* Created by sukhvir on 17/04/2015.
*/
public class home extends android.support.v4.app.Fragment {

    ImageView MyImageView;
    int[] imageArray = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5 };

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * Inflate the layout for this fragment
        */
        return inflater.inflate(R.layout.home, container, false);
    }
}

xml 文件(home.xml)

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

【问题讨论】:

  • Yon 可以简单地使用 Thread / Timer / CountDownTimer 来改变每秒的图像。

标签: java android xml imageview


【解决方案1】:

实现您的要求的最佳选择是您应该使用Timer 以如下方式每 3 秒更改一次图像。

// Declare globally
private int position = -1;

/**
 * This timer will call each of the seconds.
 */
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
    @Override
    public void run() {
        // As timer is not a Main/UI thread need to do all UI task on runOnUiThread
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    // increase your position so new image will show
                position++;
                // check whether position increased to length then set it to 0
                // so it will show images in circuler
                if (position >= imageArray.length)
                    position = 0;
                // Set Image
                MyImageView.setImageResource(imageArray[position]);
            }
        });
    }
}, 0, 3000);
// where 0 is for start now and 3000 (3 second) is for interval to change image as you mentioned in question

【讨论】:

  • 红色出现的位置,无法解析符号'位置'
  • 你应该将 position 声明为全局变量。
  • 应用程序被强制关闭
【解决方案2】:

使用CountDownTimer 方法如下:How to make a countdown Timer in android? 并更改onFinish() 内的图像视图背景。 希望这对你有用

【讨论】:

    【解决方案3】:

    首先定义一个runnable

    private Runnable runnable = new Runnable() {
    
        @Override
        public void run() {
            changeImage(pos);
        }
    };
    

    比创建一个处理程序

    private Handler handler;
    handler = new Handler(Looper.getMainLooper());
    

    在你的 changeImage 方法中

    private void changeImage(int pos) {
        yourImageView.setImageResource(imageArray[pos]);
        handler.postDelayed(runnable, duration);
    }
    

    启动和停止可运行:

    handler.postDelayed(runnable, duration);
    handler.removeCallbacks(runnable);
    

    我希望这会对你有所帮助。祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-11
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      相关资源
      最近更新 更多