TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

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

2013-7-10 19:58| 發佈者: woff| 查看: 1539| 評論: 0|原作者: woff

摘要: 完成圖 layout上建立ImageView Gallery 1.建立 attr.xml ?xml version=1.0 encoding=utf-8? resources declare-styleable name=GalleryCCTU attr name=android:galleryItemBackground /attr /declare-styleable ...
 
完成圖

4.jpg
layout上建立ImageView Gallery

1.建立 attr.xml







復制代碼


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, 2024-10-6 06:16 , Processed in 0.111728 second(s), 21 queries .

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

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部