0

enter image description here

I am developing an application on Android.

I have an issue with a Fragment, the code can be found below.

The idea is to have an Image View display a list of Picture in an infinite loop. In order to realize this, I have created a new Thread, so as not to block the UI Thread. With a while (0 < 5) statement I create an infinite loop. Then I run an if...else statement to check on which Picture we are to determine the next picture to go to.

A Handler is used to take care of the 10 seconds delay between switching pictures. And finally another runnable takes care of the posting to the UI Thread.

This seems like a very complicated way of getting things done, anyone used the simpler code?

On top of that, somewhere in my code, there is an error. I cannot spot it, anyone?

Here is my code.

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();
            }
        }
    });
}
MarGin
  • 2,078
  • 1
  • 17
  • 28
Niels Vanwingh
  • 604
  • 1
  • 6
  • 22
  • so you just want to change picture with some delay in loop? – NehaK Jun 25 '18 at 08:25
  • Yes, exactly.. A Handler seems like the best way to tackle the delay, and a separate thread is needed for posting to the UI Thread.. No? – Niels Vanwingh Jun 25 '18 at 08:27
  • yes but there are other ways too, let me share in post – NehaK Jun 25 '18 at 08:28
  • you can use Alaa M.'s answer, https://stackoverflow.com/questions/7161500/creating-animation-on-imageview-while-changing-image-resource – NehaK Jun 25 '18 at 08:33
  • this post will help you to add animation as well – NehaK Jun 25 '18 at 08:34
  • refer this link. which shows a simpler method https://stackoverflow.com/questions/46244161/how-to-implement-timer-into-automatically-image-slide-inside-the-fragment – MarGin Jun 25 '18 at 08:35
  • @NehaK, tried to implement your suggestion but I run on an error. Can you check my answer underneath? – Niels Vanwingh Jun 25 '18 at 09:55

3 Answers3

0
     private class AsyncQueryRun extends AsyncTask {
    @Override
            protected Object doInBackground(Object[] objects) {
                for (...){
                     ////do what you want


                    runOnUiThread(new Runnable() {
                            @Override
                               public void run() {
                           ///do what you want to be handled by UI thread
                           }});
                  SystemClock.sleep(60); ////wait as long as you want in mili sec.
               }
    }
    @Override
            protected void onPostExecute(Object o) {
                 }
    }
0

You can use Handler in following way :

        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
  • 2,639
  • 1
  • 15
  • 31
0

I have decided to try the solution of @NehaK and work with the ImageSwitcher View.

Added the following code in XML..

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

Then used it in my Fragment..

    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;
    }
}
Niels Vanwingh
  • 604
  • 1
  • 6
  • 22