歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android圖片浏覽器之實現顯示圖片的標題

Android圖片浏覽器之實現顯示圖片的標題

日期:2017/3/1 11:11:30   编辑:Linux編程

這裡有篇文章(http://www.linuxidc.com/Linux/2011-09/42550.htm)只是單純的顯示一個圖片,雖然我改進了,但是在使用的時候還是有一些地方不足。所以再次重寫,在原來的基礎上實現了以下小特點:

1、可以顯示標題:

2、原來的滑動速度非常快,這裡給它的速度降低了。

主要是重寫了Gallery。

廢話少說,源碼如下:

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