read

This view make transitions between images with fade in and fade out animations:

<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"> import android.content.Context;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
* A transition view provides animated switching of
* a predefined set of image resources.
* @author Nathan Scandella
*/
public class TransitionView extends RelativeLayout {

/** One of the two in-memory art images */
private ImageView _artView1;

/** The other of the two in-memory art images */
private ImageView _artView2;

/** Length of art view transition animation (msec) */
private final int ANIMATION_DURATION_MSEC = 1000;

/** The underlying ImageSwitcher that animates */
private ImageSwitcher _imageSwitcher;

/** Index into _imageIds array */
private int _currentImage = 0;

/** All available art image resource ids */
private final Integer[] _imageIds = {
R.drawable.pic01,
R.drawable.pic02,
R.drawable.pic03,
R.drawable.pic04,
R.drawable.pic05,
R.drawable.pic06,
R.drawable.pic07,
R.drawable.pic08,
R.drawable.pic09,
R.drawable.pic10
};

/**
* Create a new instance.
* @param context The parent context
*/
public TransitionView(Context context) {
super(context);

Animation fadeIn =
AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
fadeIn.setDuration(ANIMATION_DURATION_MSEC);
Animation fadeOut =
AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
fadeOut.setDuration(ANIMATION_DURATION_MSEC);

_imageSwitcher = new ImageSwitcher(context);
_imageSwitcher.setInAnimation(fadeIn);
_imageSwitcher.setOutAnimation(fadeOut);

_artView1 = new ImageView(context);
_artView1.setImageResource(_imageIds[_currentImage]);

_artView2 = new ImageView(context);
_artView2.setImageResource(_imageIds[_currentImage + 1]);

LayoutParams fullScreenLayout =
new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
_imageSwitcher.addView(_artView1, 0, fullScreenLayout);
_imageSwitcher.addView(_artView2, 1, fullScreenLayout);
_imageSwitcher.setDisplayedChild(0);
addView(_imageSwitcher, fullScreenLayout);
}

/**
* Change the currently displayed image
* @param pageRight if true, the next image will be shown,
* else the previous image will appear
*/
public void changePage(boolean pageRight) {
_currentImage = (pageRight) ? (_currentImage + 1) :
(_currentImage - 1);

if (_currentImage < 0) {
_currentImage = _imageIds.length - 1;
} else if (_currentImage >= _imageIds.length) {
_currentImage = 0;
}

if (_imageSwitcher.getCurrentView() == _artView1) {
_artView2.setImageResource(_imageIds[_currentImage]);
_imageSwitcher.showNext();
} else {
_artView1.setImageResource(_imageIds[_currentImage]);
_imageSwitcher.showPrevious();
}
}
}
</pre>
Source

Blog Logo

Daniel Gomez Rico


Published

Image

MakinGIANTS

The findings and tips records of an Android-iOS-TheWholeShabang group

Back to Overview