Android图片浏览器之实现显示图片的标题


这里有篇文章()只是单纯的显示一个图片,虽然我改进了,但是在使用的时候还是有一些地方不足。所以再次重写,在原来的基础上实现了以下小特点:

1、可以显示标题:

Android图片浏览器之实现显示图片的标题

2、原来的滑动速度非常快,这里给它的速度降低了。

主要是重写了Gallery。

废话少说,源码如下:

  1. package cn.yj3g.GalleryTest2;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import Android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.res.TypedArray;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.view.Window;  
  14. import android.view.animation.AnimationUtils;  
  15. import android.widget.AdapterView;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.Gallery;  
  18. import android.widget.Gallery.LayoutParams;  
  19. import android.widget.ImageSwitcher;  
  20. import android.widget.ImageView;  
  21. import android.widget.TextView;  
  22. import android.widget.ViewSwitcher;  
  23.   
  24. public class GalleryTestActivity extends Activity implements AdapterView.OnItemClickListener,  
  25.         ViewSwitcher.ViewFactory {  
  26.   
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  31.   
  32.           
  33.         setContentView(R.layout.image_switcher_1);  
  34.   
  35.         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);  
  36.         mSwitcher.setFactory(this);  
  37.         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));  
  38.         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));  
  39.   
  40.         Gallery g = (Gallery) findViewById(R.id.gallery);  
  41.         g.setAdapter(new ImageAdapter(this, mThumbIds.length));  
  42.         g.setOnItemClickListener(this);  
  43.     //  g.setOnItemSelectedListener(this);   
  44.         g.setSelection(1);  
  45.     }  
  46.   
  47.       
  48.     public void onItemSelected(AdapterView parent, View v, int position, long id) {  
  49.         mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);  
  50.     }  
  51.   
  52.     public void onNothingSelected(AdapterView parent) {  
  53.     }  
  54.       
  55.     @Override  
  56.     public View makeView() {  
  57.         Log.v("TAG""makeView()");  
  58.         ImageView i = new ImageView(this);  
  59.         i.setBackgroundColor(0xFF000000);  
  60.         i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  61.         i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,  
  62.                 LayoutParams.MATCH_PARENT));  
  63.         return i;  
  64.     }  
  65.   
  66.     private ImageSwitcher mSwitcher;  
  67.   
  68.     public class ImageAdapter extends BaseAdapter {  
  69.         private int mGalleryItemBackground;  
  70.         private HashMap<Integer, View> mViewMaps;  
  71.         private int mCount;  
  72.         private LayoutInflater mInflater;  
  73.   
  74.         public ImageAdapter(Context c, int count) {  
  75.             this.mCount = count;  
  76.             mViewMaps = new HashMap<Integer, View>(count);  
  77.             mInflater = LayoutInflater.from(GalleryTestActivity.this);  
  78.             // See res/values/attrs.xml for the <declare-styleable> that defines   
  79.             // Gallery1.   
  80.             TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);  
  81.             mGalleryItemBackground = a.getResourceId(  
  82.                     R.styleable.Gallery1_android_galleryItemBackground, 0);  
  83.             a.recycle();  
  84.         }  
  85.   
  86.         public int getCount() {  
  87.             // return mImageIds.length;   
  88.             return Integer.MAX_VALUE;  
  89.         }  
  90.   
  91.         public Object getItem(int position) {  
  92.             return position;  
  93.         }  
  94.   
  95.         public long getItemId(int position) {  
  96.             return position;  
  97.         }  
  98.   
  99.         public View getView(int position, View convertView, ViewGroup parent) {  
  100.             Log.v("TAG""getView() position=" + position + " convertView=" + convertView);  
  101.             View viewGroup = mViewMaps.get(position%mCount);  
  102.             ImageView imageView = null;  
  103.             TextView textView = null;  
  104.             if(viewGroup==null) {  
  105.                 viewGroup = mInflater.inflate(R.layout.gallery_item, null);  
  106.                 imageView = (ImageView) viewGroup.findViewById(R.id.item_gallery_image);  
  107.                 textView = (TextView) viewGroup.findViewById(R.id.item_gallery_text);  
  108.                 imageView.setBackgroundResource(mGalleryItemBackground);  
  109.                 mViewMaps.put(position%mCount, viewGroup);  
  110.                 imageView.setImageResource(mImageIds[position % mImageIds.length]);  
  111.                 textView.setText(titles[position % mImageIds.length]);  
  112.             }   
  113.               
  114.             return viewGroup;  
  115.         }  
  116.     }  
  117.       
  118.     private String[] titles = {"标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8",};  
  119.     private Integer[] mThumbIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,  
  120.             R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,  
  121.             R.drawable.sample_7 };  
  122.   
  123.     private Integer[] mImageIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,  
  124.             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,  
  125.             R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 };  
  126.   
  127.     @Override  
  128.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  129.         mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);  
  130.     }  
  131.   
  132. }  
  • 1
  • 2
  • 下一页

相关内容