基本视图——Gallery

作者:追风剑情 发布于:2015-9-16 22:49 分类:Android

一、准备图片

g1.png

二、配置attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="Gallery1">
  4. <attr name="android:galleryItemBackground" />
  5. </declare-styleable>
  6. </resources>

三、创建活动

视图

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Image of San Francisco" />
  12. <Gallery
  13. android:id="@+id/gallery1"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"/>
  16. <ImageView
  17. android:id="@+id/image1"
  18. android:layout_width="320dp"
  19. android:layout_height="250dp"
  20. android:scaleType="fitXY"/>
  21. </LinearLayout>

代码

  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.AdapterView;
  10. import android.widget.AdapterView.OnItemClickListener;
  11. import android.widget.BaseAdapter;
  12. import android.widget.Gallery;
  13. import android.widget.ImageView;
  14. import android.widget.Toast;
  15.  
  16. public class GalleryActivity extends Activity {
  17.  
  18. Integer[] imageIDs = {
  19. R.drawable.pic1,
  20. R.drawable.pic2,
  21. R.drawable.pic3,
  22. R.drawable.pic4,
  23. R.drawable.pic5,
  24. R.drawable.pic6,
  25. R.drawable.pic7
  26. };
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_gallery);
  31. Gallery gallery = (Gallery) findViewById(R.id.gallery1);
  32. gallery.setAdapter(new ImageAdapter(this));
  33. gallery.setOnItemClickListener(new OnItemClickListener(){
  34.  
  35. @Override
  36. public void onItemClick(AdapterView<?> parent, View view,
  37. int position, long id) {
  38. Toast.makeText(getBaseContext(), "pic"+(position+1)+" selected",
  39. Toast.LENGTH_SHORT).show();
  40. ImageView imageView = (ImageView) findViewById(R.id.image1);
  41. imageView.setImageResource(imageIDs[position]);
  42. }
  43. });
  44. }
  45. public class ImageAdapter extends BaseAdapter
  46. {
  47. Context context;
  48. int itemBackground;
  49. public ImageAdapter(Context c)
  50. {
  51. context = c;
  52. //设置样式
  53. TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
  54. itemBackground = a.getResourceId(
  55. R.styleable.Gallery1_android_galleryItemBackground, 0);
  56. a.recycle();//回收a对象
  57. }
  58.  
  59. @Override
  60. public int getCount() {
  61. return imageIDs.length;
  62. }
  63.  
  64. @Override
  65. public Object getItem(int position) {
  66. return position;
  67. }
  68.  
  69. @Override
  70. public long getItemId(int position) {
  71. return position;
  72. }
  73.  
  74. @Override
  75. public View getView(int position, View convertView, ViewGroup parent) {
  76. ImageView imageView;
  77. if(convertView == null) {
  78. imageView = new ImageView(context);
  79. imageView.setImageResource(imageIDs[position]);
  80. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  81. imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
  82. }else{
  83. imageView = (ImageView) convertView;
  84. }
  85. imageView.setBackgroundResource(itemBackground);
  86. return imageView;
  87. }
  88. }
  89. }

运行效果

g2.png


示例说明:

      创建的ImageAdapter类扩展了BaseAdapter类,可以用来将一系列ImageView视图绑定到Gallery视图上。BaseAdapter类作为联系AdapterView和为它提供数据的数据源之间的桥梁。下面列出了AdapterView的一些示例:

  • ListView
  • GridView
  • Spinner
  • Gallery

在Android中,BaseAdapter类有以下几个子类:

  • ListAdapter
  • ArrayAdapter
  • CursorAdapter
  • SpinnerAdapter

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号