[android]代码库
package com.example.testandroid;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/** * gallery 图片循环播放 * * @author maqf * */
@SuppressWarnings("deprecation")
public class CycleImageActivity extends Activity {
private int[] picture = { R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5,
R.drawable.gallery6, };
private Gallery pictureGallery = null;
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tiem);
this.pictureGallery = (Gallery) findViewById(R.id.gallery);
ImageAdapter adapter = new ImageAdapter(this);
this.pictureGallery.setAdapter(adapter);
Timer timer = new Timer();
timer.schedule(task, 2000, 2000);
}
/** * 定时器 */
private TimerTask task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 2;
index = pictureGallery.getSelectedItemPosition();
index++;
handler.sendMessage(message);
}
};
/** * Handler */
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 2:
pictureGallery.setSelection(index);
break;
default:
break;
}
}
};
/** * 自定义一个图片显示适配器 * * @author TanRuixiang * */
class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(picture[position % picture.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(
Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
return imageView;
}
}
}
初级程序员
by: 小蘑菇 发表于:2013-05-05 23:57:20 顶(0) | 踩(0) 回复
收藏,备用
回复评论