TShopping

 找回密碼
 註冊
搜索
查看: 512|回復: 0

[教學] [Android智慧型手機設計_Gallery元件]

[複製鏈接]
發表於 2013-5-31 21:35:27 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
完成圖

4.jpg
layout上建立ImageView Gallery

1.建立 attr.xml

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


2.主程式 MainActivity.java


  1. package org.galleryEx;
  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. public class MainActivity extends Activity {
  16. //宣告資料型態
  17. ImageView imageView;
  18. Gallery gallery;
  19. //宣告圖片陣列,檔案必須匯入到圖片資料夾
  20. private Integer[] images = {
  21. R.drawable.baby0, R.drawable.baby1, R.drawable.baby2, R.drawable.baby3,
  22. R.drawable.baby4, R.drawable.baby5, R.drawable.baby6
  23. };

  24. //建立Adapter
  25. public class ImageAdapter extends BaseAdapter {
  26. int Background;
  27. private Context context;


  28. //建構元
  29. public ImageAdapter(Context c) {
  30. context = c;
  31. //獲取陣列
  32. TypedArray attr = context.obtainStyledAttributes(R.styleable.GalleryCCTU);

  33. //變更背景
  34. Background = attr.getResourceId(
  35. R.styleable.GalleryEx_android_galleryItemBackground, 0);
  36. //回收記憶體資源
  37. attr.recycle();
  38. }


  39. @Override
  40. //回傳幾張圖片
  41. public int getCount() {
  42. return images.length;
  43. }


  44. @Override
  45. public Object getItem(int position) {
  46. return position;
  47. }


  48. @Override
  49. public long getItemId(int position) {
  50. return position;
  51. }


  52. @Override
  53. public View getView(int position, View convertView, ViewGroup parent) {


  54. ImageView imageView;
  55. //如果convertView為空,設定預設值到imageView,不然秀出點選圖
  56. if (convertView == null) {
  57. imageView = new ImageView(context);
  58. imageView.setLayoutParams(new Gallery.LayoutParams(150, 150));
  59. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  60. imageView.setBackgroundResource(Background);
  61. } else {
  62. imageView = (ImageView)convertView;
  63. }
  64. //獲取點選而改變圖片並回傳
  65. imageView.setImageResource(images[position]);
  66. return imageView;
  67. }
  68. }

  69. @Override
  70. public void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. setContentView(R.layout.main);
  73. findViews();
  74. }

  75. private void findViews() {

  76. imageView = (ImageView)findViewById(R.id.imageView);
  77. gallery = (Gallery) findViewById(R.id.gallery);

  78. //設定gallery Adapter
  79. gallery.setAdapter(new ImageAdapter(this));
  80. //監聽gallery點擊圖片 即改變圖片到ImageView
  81. gallery.setOnItemClickListener(new OnItemClickListener() {
  82. public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
  83. //秀出訊息
  84. Toast.makeText(MainActivity.this, "第 " + position+" 張", Toast.LENGTH_SHORT).show();
  85. //改變圖片到ImageView

  86. imageView.setImageResource(images[position]);
  87. }
  88. });

  89. }
  90. }
複製代碼



 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-11 06:55 , Processed in 0.062466 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表