`

ImageView淡入淡出效果

 
阅读更多

 

public class FadeInAndOut extends Activity {

	private int intCounter = 0;
	private Handler mHandler = new Handler();

	private ImageView mImageView01;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fade_in_out);

		mImageView01 = (ImageView) findViewById(R.id.imageView1);
		mImageView01.setImageResource(R.drawable.img1);
		
		mImageView01.setAlpha(0);
		mHandler.post(fadeInTask);

	}

	/* 设定ImageView的透明度渐显出来 */
	private Runnable fadeInTask = new Runnable() {
		public void run() {
			intCounter = intCounter + 1;
			mImageView01.setAlpha(intCounter * 15);
			if(intCounter == 17)mHandler.postDelayed(fadeOutTask, 50);
			else mHandler.postDelayed(fadeInTask, 50);
		}
	};

	/* 设定ImageView的透明度渐暗下去 */
	private Runnable fadeOutTask = new Runnable() {
		public void run() {
			intCounter = intCounter - 1;
			mImageView01.setAlpha(intCounter * 15);
			if(intCounter == 0)mHandler.postDelayed(fadeInTask, 50);
			else mHandler.postDelayed(fadeOutTask, 50);
		}
	};

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics