package com.droidikin.samples.imageswitcher;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class CodeActivity extends Activity implements ViewFactory {
// A reference to the images contained into the drawable folder
private Integer[] m_ImageIds = {
R.drawable.img_01,
R.drawable.img_02,
R.drawable.img_03
};
// The ImageSwitcher
ImageSwitcher m_ImageSwitcher;
// The Handler used for manage the Runnable that switch the images
Handler m_Handler = new Handler();
// The index of the current image
int m_imageIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.use_code_activity);
// assign the ImageSwitcher
m_ImageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitcher);
m_ImageSwitcher.setFactory(this);
// Create the Fade in animation
Animation fadeIn = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
fadeIn.setDuration(3000);
// Create the Fade out animation
Animation fadeOut = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
fadeOut.setDuration(3000);
// Assign the two animations to the ImageSwitcher
m_ImageSwitcher.setInAnimation(fadeIn);
m_ImageSwitcher.setOutAnimation(fadeOut);
// Start the Runnable
m_Handler.post(m_UpdateTimeTask);
}
@Override
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return i;
}
/*
* Set the image to show into the ImageSwitcher, then post his execution after 5 seconds
*/
Runnable m_UpdateTimeTask = new Runnable() {
public void run()
{
Log.e("------run()--", " ");
// Set the Image to show
m_ImageSwitcher.setImageResource(m_ImageIds[m_imageIndex]);
// Increment the index
m_imageIndex++;
// if necessary restart from the first image
if(m_imageIndex > (m_ImageIds.length-1)){
m_imageIndex = 0;
}
// Set the execution after 5 seconds
// m_Handler.postDelayed(this, (5 * 1000));
m_Handler.postDelayed(this, (500));
}
};
}
No comments:
Post a Comment