android Gallery控制項與BaseAdapter適配器

來源:互聯網
上載者:User

GalleyView 
 
Galley是畫廊的意思,一般只在圖片顯示中使用,而且也不常用。

1)Android XML檔案

Java代碼:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:id="@+id/gallery" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="wrap_content"> 
  6. </Gallery> 

由於Galley使用者處理圖片,因此處理item可採用ImageView,在設定adapter中,我們可以參見《Android 學習筆記(十三):Activity-GridView 》中對BaseAdapter進行繼承。

2)Java原始碼

Java代碼:

 
  1. public class Chapter7Test8 extends Activity {  
  2. @Override   
  3. protected void onCreate(Bundle savedInstanceState) {  
  4. super.onCreate(savedInstanceState);  
  5. setContentView(R.layout.chapter_7_test8);   
  6. //步驟1:和之前學習的一樣,設定adapter來描述item的內容以及設定item的格式;通過setOnItemClickListener()設定點擊觸發的操作。   
  7. Gallery gallery = (Gallery)findViewById(R.id.gallery);  
  8. gallery.setAdapter (new ImageAdapter (this));  
  9. gallery.setOnItemClickListener (new OnItemClickListener(){  
  10. public void onItemClick(AdapterView<?> parent,View v,int position,long id){  
  11. Toast.makeText(Chapter7Test8.this,""+position,Toast.LENGTH_SHORT).show();  
  12. }  
  13. } );  
  14. }  
  15.  
  16. //步驟2:adapter繼承BaseAdapter,具體描述item。需要建立建構函式,具體化getCount(), getItem(), getItemId(), getView()。   
  17. private class ImageAdapter extends BaseAdapter{  
  18. private Context mContext;  
  19. private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,  
  20. R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,  
  21. R.drawable.sample_7 }; //我們將名稱為drawable_sample_1的圖片檔案,拷貝到drawable/下面。   
  22. public ImageAdapter(Context context){  
  23. mContext = context;  
  24. }  
  25. public int getCount() {  
  26. return mImageIds.length;  
  27. }  
  28. public Object getItem(int position) {  
  29. return position;  
  30. }  
  31. public long getItemId(int position) {  
  32. return position;  
  33. }  
  34. //步驟3:每個item都是ImageView,通過setImageResource將圖片呈現,設定每個item的大小,以及顯示比例,這裡,我們採用FIT_XY,根據X:Y將整個圖片顯示出來,如果X:Y和圖片長:寬不一樣,圖片可能有些變形。   
  35. public View getView(int position, View convertView, ViewGroup parent) {  
  36. ImageView image = new ImageView(mContext);  
  37. image.setImageResource(mImageIds[position]);  
  38. image.setLayoutParams(new Gallery.LayoutParams(150,100));  
  39. image.setScaleType(ImageView.ScaleType.FIT_XY);  
  40. return image;  
  41. }   
  42. }  
  43. }  
  44. 3)通過xml檔案對item的格式進行設定   
  45. 我們在res/values/下面增加一個xml檔案,用於描述自訂widget的屬性格式為  
  46. Java代碼:  
  47. <resources> 
  48. <declare-styleable name="XXXX"> 
  49. <attr name="AAAAA" format="BBBB" /> 
  50. <attr name="aaaaa" format="bbbb" /> 
  51. </declare-styleable> 
  52. </resources> 

在R.java中將增加int R.styleable.XXXX[]來表示此定義,如果裡面有2個屬性,則有兩個元素。在本例,設定style的屬性,我們設定一個android已定義的屬性galleryItembackground,它定義一個具有一個邊框的gallery的item。如下:

Java代碼:

 
  1. <resources> 
  2. <declare-styleable name="HelloGallery"> 
  3. <attr name="android:galleryItemBackground" /> 
  4. </declare-styleable> 
  5. </resources> 

獲得自訂屬性方式:

Java代碼:

 
  1. TypedArray a= obtainStyledAttributes (R.styleable.XXX /*int[]*/ );  
  2. aattrId = a.getResourceId (R.styleable.XXXX_AAAA,defaultId);//獲得該屬性的ID, 如果沒有發現該屬性,則返回defaultId的值。   
  3. a.recyle (),//在使用obtainStyledAttributes()後應調用,是的可以被系統重用。  

在這個例子中:

Java代碼:

 
  1. public ImageAdapter(Context context){  
  2. TypedArray a= obtainStyledAttributes(R.styleable.HelloGallery);  
  3. mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground , 0);  
  4. a.recycle();  
  5. }  
  6. public View getView(int position, View convertView, ViewGroup parent) {  
  7. image.setBackgroundResource (mGalleryItemBackground);  
  8. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.