|
主要讲的就是一个循环播放图片的实现,我们这里主要还是用到了arraylisy,利用arraylist来控制里面的图片位置,这样就相对简单和容易控制循环了,我们只要牵扯到自定义的arrayadapter还是要注意继承getview在里面设置一个变量参数,就可以来实现变化,这才是最关键的。我们来看看代码吧:
java代码:
public class Splash extends Activity {
ArrayList objects = new ArrayList();
Gallery g;
int i = 0;
@Override
public void onCreate(Bundle icicle) { super.onCreate(icicle);
setContentView(R.layout.photos);
g = (Gallery) findViewById(R.id.gallery);
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
g.setAdapter(new CustomAdapter(this, objects));
g.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
Log.i(“”, “selected ” + arg2);
}
@Override
public void onNothingSelected(AdapterView arg0) {}
});
}
@Override
public void onBackPressed() {
g.setSelection(i++);
}
private class CustomAdapter extends BaseAdapter {
private Context mCtx;
private List objects;
public int getCount() {
return this.objects.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public CustomAdapter(Context context, ArrayList objects) {
super();
mCtx = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView row = (ImageView) convertView;
if (row == null) {
row = new ImageView(mCtx);
row.setBackgroundDrawable(objects.get(position));
}
return row;
}
}
} |
|