這裡有篇文章(http://www.linuxidc.com/Linux/2011-09/42550.htm)只是單純的顯示一個圖片,雖然我改進了,但是在使用的時候還是有一些地方不足。所以再次重寫,在原來的基礎上實現了以下小特點:
1、可以顯示標題:
2、原來的滑動速度非常快,這裡給它的速度降低了。
主要是重寫了Gallery。
廢話少說,源碼如下:
- package cn.yj3g.GalleryTest2;
-
- import java.util.HashMap;
-
- import Android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.Gallery.LayoutParams;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.ViewSwitcher;
-
- public class GalleryTestActivity extends Activity implements AdapterView.OnItemClickListener,
- ViewSwitcher.ViewFactory {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
-
- setContentView(R.layout.image_switcher_1);
-
- mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
- mSwitcher.setFactory(this);
- mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
- mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
-
- Gallery g = (Gallery) findViewById(R.id.gallery);
- g.setAdapter(new ImageAdapter(this, mThumbIds.length));
- g.setOnItemClickListener(this);
- // g.setOnItemSelectedListener(this);
- g.setSelection(1);
- }
-
-
- public void onItemSelected(AdapterView parent, View v, int position, long id) {
- mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);
- }
-
- public void onNothingSelected(AdapterView parent) {
- }
-
- @Override
- public View makeView() {
- Log.v("TAG", "makeView()");
- ImageView i = new ImageView(this);
- i.setBackgroundColor(0xFF000000);
- i.setScaleType(ImageView.ScaleType.FIT_CENTER);
- i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
- LayoutParams.MATCH_PARENT));
- return i;
- }
-
- private ImageSwitcher mSwitcher;
-
- public class ImageAdapter extends BaseAdapter {
- private int mGalleryItemBackground;
- private HashMap<Integer, View> mViewMaps;
- private int mCount;
- private LayoutInflater mInflater;
-
- public ImageAdapter(Context c, int count) {
- this.mCount = count;
- mViewMaps = new HashMap<Integer, View>(count);
- mInflater = LayoutInflater.from(GalleryTestActivity.this);
- // See res/values/attrs.xml for the <declare-styleable> that defines
- // Gallery1.
- TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
- mGalleryItemBackground = a.getResourceId(
- R.styleable.Gallery1_android_galleryItemBackground, 0);
- a.recycle();
- }
-
- public int getCount() {
- // return mImageIds.length;
- 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) {
- Log.v("TAG", "getView() position=" + position + " convertView=" + convertView);
- View viewGroup = mViewMaps.get(position%mCount);
- ImageView imageView = null;
- TextView textView = null;
- if(viewGroup==null) {
- viewGroup = mInflater.inflate(R.layout.gallery_item, null);
- imageView = (ImageView) viewGroup.findViewById(R.id.item_gallery_image);
- textView = (TextView) viewGroup.findViewById(R.id.item_gallery_text);
- imageView.setBackgroundResource(mGalleryItemBackground);
- mViewMaps.put(position%mCount, viewGroup);
- imageView.setImageResource(mImageIds[position % mImageIds.length]);
- textView.setText(titles[position % mImageIds.length]);
- }
-
- return viewGroup;
- }
- }
-
- private String[] titles = {"標題1","標題2","標題3","標題4","標題5","標題6","標題7","標題8",};
- private Integer[] mThumbIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
- R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
- R.drawable.sample_7 };
-
- private Integer[] mImageIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
- R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
- R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 };
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);
- }
-
- }