Question

Below I am posting my code for the thread I am running to animate text in a RelativeLayout on top of the Page Curl activity by harism.

public void startProgress(final int index)
{
    runnable = new Runnable()
    {
        @Override
        public void run()
        {
            mArrWords.removeAll(mArrWords);
            mStart.removeAll(mStart);
            mEnd.removeAll(mEnd);

            words = sentence.split(" ");
            for(int i = 0; i < words.length; i++)
            {
                mArrWords.add(words[i]);
                if(i == 0)
                {
                    mStart.add(0);
                    mEnd.add(words[0].length());
                }
                else
                {
                    mStart.add(mEnd.get(i-1)+1);
                    mEnd.add(mStart.get(i)+words[i].length());
                }
                /*Log.e("words", "" + "" + words[i]);
                Log.e("mArrWords", "" + mArrWords);
                Log.e("mStart", "" + mStart);
                Log.e("mEnd", "" + mEnd);*/
            }
            for (int i = 0; i < mArrWords.size(); i++)
            {
                final int value = i;
                try
                {
                    Thread.sleep(500);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }

                mHandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        currIndex = index;
                        try
                        {

                            if(CurlView.ANIMATE)
                                tv1.setVisibility(View.VISIBLE);
                            else
                                tv1.setVisibility(View.GONE);

                            final Pattern p = Pattern.compile(mArrWords.get(value));
                            final Matcher matcher = p.matcher(sentence);
                            SpannableString spannableTxt = new SpannableString(sentence);
                            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
                            while(matcher.find())
                                spannableTxt.setSpan(span, mStart.get(value), mEnd.get(value), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv1.setText(spannableTxt);
                            mHandler.sendEmptyMessage(0);
                        }
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                            mHandler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }
    };
    final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
    worker.schedule(runnable, CurlView.ANIMTIME+50, TimeUnit.MILLISECONDS);
}

Here, I am animating the text over images. I need to change the text for each page I am changing. I am able to change the text, however, when I turn the page, the index values I store in ArrayLists are not getting cleared. I am storing a sentence in an ArrayList named mArrWords and the indexes to refer to each word of sentence are stored in mStart and mEnd.

The problem I am facing is when the text changes, the animation starts with the previous indexes stored in mStart and mEnd ArrayLists I use to store index of a particular word. What I need to know is how do I stop my thread when the page is turned or the index of the page changes. I am calling this function inside the updatePage(final CurlPage page, final int width, final int height, final int index) method of Curl activity. I hope I was able to explain my problem. Thanks!

EDIT: I would like to specify my question more clearly. How do I check if the thread is already running before starting another thread and stop the execution of the previous thread?

Était-ce utile?

La solution

removeCallbacks(..) only stops pending messages (Runnables).If runnable is started then u can not stop it in this way. See the following :

removecallbacks-not-stopping-runnable

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top