Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无限循环-延迟-分离线程_Android - Fatal编程技术网

Android 无限循环-延迟-分离线程

Android 无限循环-延迟-分离线程,android,Android,我正在Android上开发一个应用程序 我有一个片段的问题,代码可以在下面找到 其思想是让图像视图以无限循环的方式显示图片列表。为了实现这一点,我创建了一个新线程,以避免阻塞UI线程。通过while(0

我正在Android上开发一个应用程序

我有一个片段的问题,代码可以在下面找到

其思想是让图像视图以无限循环的方式显示图片列表。为了实现这一点,我创建了一个新线程,以避免阻塞UI线程。通过while(0<5)语句,我创建了一个无限循环。然后,我运行if…else语句来检查我们要确定下一张图片的图片

处理程序用于处理切换图片之间的10秒延迟。最后,另一个runnable负责发布到UI线程

这似乎是一种非常复杂的完成任务的方法,有人使用过更简单的代码吗

最重要的是,在我的代码中,有一个错误。我看不出来,有人吗

这是我的密码

public class SecAct_Foto_Fragment extends Fragment {

    int counter = 0;
    View rootView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);

        return rootView;
    }

    Thread myThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (0 < 5) {

                //so far it loops only once
                //you start with run_rocks and but_left
                final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);
                final ImageView three_but = (ImageView) rootView.findViewById(R.id.knoppen);

                //create a runnable for the picture view
                pic_view.post(new Runnable() {
                    @Override
                    public void run() {
                        //every 10 seconds, switch picture and button fragment
                        if (counter == 0) {
                            final Handler handler0 = new Handler();
                            handler0.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    pic_view.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            pic_view.setImageResource(R.drawable.run_mount);
                                        }
                                    });
                                    counter = 1;
                                }
                            }, 10000L);
                        } else if (counter == 1) {
                            final Handler handler1 = new Handler();
                            handler1.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    pic_view.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            pic_view.setImageResource(R.drawable.run_away);
                                        }
                                    });
                                    counter = 2;
                                }
                            }, 10000L);
                        } else {
                            final Handler handler2 = new Handler();
                            handler2.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    pic_view.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            pic_view.setImageResource(R.drawable.run_rocks);
                                        }
                                    });
                                    counter = 0;
                                }
                            }, 10000L);
                        }
                    }
                });

                myThread.start();
            }
        }
    });
}
公共类SecAct\u Foto\u片段扩展片段{
int计数器=0;
视图根视图;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
rootView=充气机。充气(R.layout.sec\u act\u photo\u布局,容器,错误);
返回rootView;
}
Thread myThread=新线程(new Runnable()){
@凌驾
公开募捐{
而(0<5){
//到目前为止,它只循环了一次
//你从奔跑的岩石开始,但你离开了
最终ImageView pic_view=(ImageView)rootView.findViewById(R.id.foto_groot);
final ImageView three_but=(ImageView)rootView.findviewbyd(R.id.knoppen);
//为图片视图创建可运行的
pic_view.post(新的Runnable(){
@凌驾
公开募捐{
//每10秒,切换图片和按钮片段
如果(计数器==0){
最终处理程序handler0=新处理程序();
handler0.postDelayed(新的Runnable(){
@凌驾
公开募捐{
pic_view.post(新的Runnable(){
@凌驾
公开募捐{
pic_view.setImageResource(R.drawable.run_mount);
}
});
计数器=1;
}
},10000升);
}else if(计数器==1){
最终处理程序handler1=新处理程序();
handler1.postDelayed(新的Runnable(){
@凌驾
公开募捐{
pic_view.post(新的Runnable(){
@凌驾
公开募捐{
pic_view.setImageResource(R.drawable.run_-away);
}
});
计数器=2;
}
},10000升);
}否则{
最终处理程序handler2=新处理程序();
handler2.postDelayed(新的Runnable(){
@凌驾
公开募捐{
pic_view.post(新的Runnable(){
@凌驾
公开募捐{
pic_view.setImageResource(R.drawable.run_rocks);
}
});
计数器=0;
}
},10000升);
}
}
});
myThread.start();
}
}
});
}

您可以通过以下方式使用处理程序:

        final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);

        private int animationCounter = 1;
        private Handler imageSwitcherHandler;
        imageSwitcherHandler = new Handler(Looper.getMainLooper());
        imageSwitcherHandler.post(new Runnable() {
            @Override
            public void run() {
                switch (animationCounter++) {
                    case 1:
                        pic_view.setImageResource(R.drawable.run_mount);

                        break;
                    case 2:
                        pic_view.setImageResource(R.drawable.run_mount2);

                        break;
                    case 3:
                        pic_view.setImageResource(R.drawable.run_mount3);
                        break;
                }
                animationCounter %= 4;
                if(animationCounter == 0 ) animationCounter = 1;

                imageSwitcherHandler.postDelayed(this, 3000);
            }
        });

我决定尝试@NehaK的解决方案,并使用ImageSwitcher视图

在XML中添加了以下代码

   <ImageSwitcher
        android:id="@+id/foto_groot_imageswitch"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        app:srcCompat="@drawable/run_rocks"
    />

所以你们只是想在循环中稍微延迟一下改变画面?是的,没错。。处理程序似乎是解决延迟的最佳方法,需要一个单独的线程来发布到UI线程。。不是吗?是的,但是还有其他的方法,让我在帖子中分享你可以使用Alaa M.的答案,这篇帖子将帮助你添加动画以及用你想要的图像更改图像
   <ImageSwitcher
        android:id="@+id/foto_groot_imageswitch"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        app:srcCompat="@drawable/run_rocks"
    />
    public class SecAct_Foto_Fragment extends Fragment {

    int counter = 0;
    View rootView;
    private ImageSwitcher pic_image_switch;
    private Handler pic_image_switch_handler;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);

        /*Animation anim_in = AnimationUtils.loadAnimation(getActivity(), R.anim.enter_from_left);
        pic_image_switch.setInAnimation(anim_in);*/

        //pic_image_switch = new ImageSwitcher(getActivity());
        pic_image_switch = (ImageSwitcher) rootView.findViewById(R.id.foto_groot_imageswitch);

        pic_image_switch.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getActivity());
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
                return imageView;
            }
        });

        pic_image_switch_handler = new Handler(Looper.getMainLooper());

        pic_image_switch_handler.post(new Runnable() {
            @Override
            public void run() {
                switch (counter) {
                    case 0:
                        pic_image_switch.setImageResource(R.drawable.run_mount);
                        break;
                    case 1:
                        pic_image_switch.setImageResource(R.drawable.run_away);
                        break;
                    case 2:
                        pic_image_switch.setImageResource(R.drawable.run_rocks);
                        break;
                }
                counter += 1;
                if (counter == 3) {
                    counter = 0;
                }
                pic_image_switch.postDelayed(this, 1000);
            }
        });

        return rootView;
    }
}