ANDROID笔记:Gallery的简单使用
1 package com.example.adaptertest.gallery; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.os.Handler.Callback; 7 import android.os.Message; 8 import android.view.View; 9 import android.widget.AdapterView; 10 import android.widget.AdapterView.OnItemClickListener; 11 import android.widget.AdapterView.OnItemSelectedListener; 12 import android.widget.Gallery; 13 import android.widget.Toast; 14 15 import com.example.adaptertest.R; 16 17 @SuppressWarnings("deprecation") 18 public class GalleryActivity extends Activity { 19 int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4 }; 20 // 可以用AdapterViewFlipper替换 21 Gallery gallery; 22 int counter = 0; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.gallerytest); 28 29 gallery = (Gallery) findViewById(R.id.gallery_id); 30 gallery.setAdapter(new GalleryAdapter(GalleryActivity.this, images)); 31 handler.sendEmptyMessageDelayed(1, 5000); 32 // 得到gallery选中的item的ID 33 // gallery.getSelectedItemId(); 34 gallery.setOnItemClickListener(new OnItemClickListener() { 35 36 @Override 37 public void onItemClick(AdapterView<?> parent, View view, 38 int position, long id) { 39 Toast.makeText(GalleryActivity.this, position + "", 40 Toast.LENGTH_LONG).show(); 41 // 移除消息队列(点击图片事停止图片自动滚动) 42 handler.removeMessages(1); 43 } 44 }); 45 // 最前端的图片 46 gallery.setOnItemSelectedListener(new OnItemSelectedListener() { 47 48 @Override 49 public void onItemSelected(AdapterView<?> parent, View view, 50 int position, long id) { 51 Toast.makeText(GalleryActivity.this, position + "", 52 Toast.LENGTH_LONG).show(); 53 54 } 55 56 @Override 57 public void onNothingSelected(AdapterView<?> parent) { 58 Toast.makeText(GalleryActivity.this, "nothing", 59 Toast.LENGTH_LONG).show(); 60 } 61 }); 62 63 } 64 65 Handler handler = new Handler(new Callback() { 66 67 @Override 68 public boolean handleMessage(Message msg) { 69 if (msg.what == 1) { 70 counter++; 71 if (counter >= images.length) { 72 counter = 0; 73 } 74 // 更换图片 75 gallery.setSelection(counter); 76 // 继续发送消息到队列(类似递归) 77 handler.sendEmptyMessageDelayed(1, 5000); 78 } 79 return true; 80 } 81 }); 82 }
package com.example.adaptertest.gallery; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.ImageView.ScaleType; public class GalleryAdapter extends BaseAdapter { private int[] images; private Context context; public GalleryAdapter(Context convertView, int[] images) { this.context = convertView; this.images = images; } @Override public int getCount() { // TODO Auto-generated method stub return images.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return images[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @SuppressWarnings("deprecation") @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = new ImageView(context); imageView.setImageResource(images[position]); imageView.setScaleType(ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(500, 500)); return imageView; } }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Gallery 8 android:id="@+id/gallery_id" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:spacing="-20px" 12 android:unselectedAlpha="150" /> 13 14 </LinearLayout> 15 <!-- android:unselectedAlpha="150" 未被选择的图片的透明度 --> 16 <!-- android:spacing="-20px" 间隔 -->