Android Gallery 画廊Demo
源代码:http://yuncode.net/code/c_5039963a841a379
- public class Sample extends Activity {
- int[] p_w_picpathIDs={
- R.drawable.bbta,R.drawable.bbtb,R.drawable.bbtc,
- R.drawable.bbtd,R.drawable.bbte,R.drawable.bbtf,
- R.drawable.bbtg
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Gallery gl=(Gallery)this.findViewById(R.id.Gallery01);
- BaseAdapter ba=new BaseAdapter(){
- @Override
- public int getCount() {
- return p_w_picpathIDs.length;
- }
- @Override
- public Object getItem(int arg0) {
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- return 0;
- }
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- ImageView iv = new ImageView(Sample_5_8.this);
- iv.setImageResource(p_w_picpathIDs[arg0]);
- iv.setScaleType(ImageView.ScaleType.FIT_XY);
- iv.setLayoutParams(new Gallery.LayoutParams(188,250));
- return iv;
- }
- };
- gl.setAdapter(ba);
- gl.setOnItemClickListener(
- new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- Gallery gl=(Gallery)findViewById(R.id.Gallery01);
- gl.setSelection(arg2);
- }
- }
- );
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_vertical"
- >
- <Gallery
- android:id="@+id/Gallery01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:spacing="10dip"
- android:unselectedAlpha="1" />
- </LinearLayout>