Gif animation in Android

Posted: May 22, 2015 in Android

After searching some post about how to implement a gif animation in Android my solution goes here:

Seems that my template doesn’t fix well with gist, so here’s an alternative for the code



public void enableLoadingImage() {
 
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
 
		ImageView image = (ImageView) findViewById(R.id.connectionImage);
		image.setVisibility(View.GONE);
 
		imageSwitcher.setFactory(new ViewFactory() {
 
			@Override
			public View makeView() {
				ImageView myView = new ImageView(getApplicationContext());
				myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				myView.setLayoutParams(new ImageSwitcher.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				return myView;
			}
 
		});
 
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
 
		imageSwitcher.postDelayed(new Runnable() {
			int i = -1;
 
			public void run() {
 
				i++;
 
				switch (i) {
				case 0:
					imageSwitcher.setImageResource(R.drawable.loadingscore1);
					break;
				case 1:
					imageSwitcher.setImageResource(R.drawable.loadingscore2);
					break;
				case 2:
					imageSwitcher.setImageResource(R.drawable.loadingscore3);
					break;
				case 3:
					imageSwitcher.setImageResource(R.drawable.loadingscore4);
					i = -1;
					break;
 
				}
 
				imageSwitcher.postDelayed(this, 1000);
			}
		}, 1000);
 
	}
 
}

Hope this can help someone else.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s