Archive for the ‘Android’ Category

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

Just like that , I needed to make a call to an AsynTask in Android, after reading differents threads in stackoverflow about the solutions I found mine doing this:

 
new GetAsyncTask(this).execute().get(10000,TimeUnit.MILLISECONDS);

 

This waits for 10 sec doing the request, if time is over just finishes and I catch an Exception to continue with the flow.
Seems that AsyncTask must be used only for Task that won’t consume too much time.