0

I want to apply fade in/out effect on an ImageView via AnimationListener from the separate thread. The following piece of code is working, if it's called from the main thread:

public static void setImage(final ImageView imageView, final int image)
{
    final Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setInterpolator(new DecelerateInterpolator());
    fadeIn.setDuration(2000);

    final Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator());
    fadeOut.setDuration(2000);

    AnimationSet animation = new AnimationSet(false);
    animation.addAnimation(fadeOut);
    animation.setRepeatCount(1);
    imageView.setAnimation(animation);

    animation.setAnimationListener(new Animation.AnimationListener()
    {
        public void onAnimationEnd(Animation animation)
        {
            // TODO Auto-generated method stub
            imageView.setImageResource(image);
            imageView.startAnimation(fadeIn);
        }
        public void onAnimationRepeat(Animation animation)
        {
            // TODO Auto-generated method stub
        }
        public void onAnimationStart(Animation animation)
        {
            // TODO Auto-generated method stub
        }
    });
}

Somewhere in MainActivity:

protected void onCreate(Bundle savedInstanceState)
{
     ...
     Animator.setImage(imageView, R.drawable.ic_check_circle);
     ...
}

However, it doesn't seem to work properly if executed inside asynctask:

private class JSONParse extends AsyncTask<String, String, JSONObject>
{
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected JSONObject doInBackground(String... args)
    {
        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(ins_url);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json)
    {
        try
        {
            if( json.getString( "status" ).equals( "true" ) )
            {
                System.out.println("Ok!");
            //  Nothing happens when setImage method is invoked
                Animator.setImage(imageView, R.drawable.ic_check_circle);

            //  setImageResource works fine
            //  imageView.setImageResource(R.drawable.ic_check_circle);

            }

        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

    }
}

I came up with an idea to run it in the main thread, but failed to make it work either:

Handler mainHandler = new Handler(getBaseContext().getMainLooper());
Runnable r = new Runnable()
{
     @Override
     public void run() {
          Animator.setImage(imageView, R.drawable.ic_check_circle);
     }
};
mainHandler.post(r);

Any suggestions?

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
WrathOfFlame
  • 83
  • 1
  • 7
  • Possible duplicate of [Fade In Fade Out Android Animation in Java](http://stackoverflow.com/questions/6796139/fade-in-fade-out-android-animation-in-java) – Ayoub Falah May 17 '16 at 17:36
  • not at all. I've checked it out!:) – WrathOfFlame May 17 '16 at 17:41
  • 1
    You shouldn't have to specify which Thread to run `onPostExecute()` as it [always runs on the UI Thread](https://developer.android.com/reference/android/os/AsyncTask.html) – Ed Holloway-George May 17 '16 at 17:53
  • @WrathOfFlame I see `wrath of not RTFM` here :) – Marcin Orlowski May 17 '16 at 17:55
  • @Ed George, And if it does, why setImage doesn't work as it's supposed to? – WrathOfFlame May 17 '16 at 18:04
  • Not sure, but you should start by calling `setAnimation()` on the view _after_ you have set the `AnimationListener`. Have you attempted to debug in Android Studio? – Ed Holloway-George May 17 '16 at 19:15
  • @Ed George I did, nothing unusual so far. I tried to call `setAnimation()` after setting the `AnimationListener`. Nothing's changed. – WrathOfFlame May 18 '16 at 04:57
  • wow, finally i did it. I should've added `startAnimation()` after setting it to an `ImageView`. The funny thing that i still don't know why it does work without `startAnimation()` from the main thread. whatever...sorry for inconvenience – WrathOfFlame May 18 '16 at 05:07

0 Answers0