Gallery控制項使用方法詳解

來源:互聯網
上載者:User

    Android手機作業系統中有許多控制項的應用可以協助我們輕鬆實現一些功能。在這裡我們就為大家詳細介紹一下有關Android Gallery控制項的具體使用方法,希望能協助大家對控制項的使用有一個深入的認識。

    Android Gallery控制項:即圖片瀏覽控制項

    layout部分:

 
  1. < Gallery xmlns:android=
    "http://schemas.android.com/apk/res/android"   
  2. android:id="@+id/gallery" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent"/> 

Android Gallery控制項代碼部分:

主類部分代碼:

 
  1. Gallery g = (Gallery) findViewById(R.id.gallery);
    //定義 Gallery 控制項  
  2. g.setAdapter(new ImageAdapter(this));
    //設定 Gallery 控制項的圖片源  
  3. g.setOnItemClickListener(new OnItemClickListener() { 
    //點擊監聽事件  
  4. public void onItemClick(AdapterView parent, View v, 
    int position, long id) {//點擊事件  
  5. Toast.makeText(galleryMain.this, "" + position, 
    Toast.LENGTH_SHORT).show(); //Toast顯示圖片位置  
  6. }  
  7. }); 

ImageAdapter 類

 
  1. private Context mContext; 
  2. //定義Context   
  3. private Integer[] mImageIds = { 
  4. //定義整型數組 即圖片源  
  5. R.drawable.sample_1,  
  6. R.drawable.sample_2,  
  7. R.drawable.sample_3,  
  8. R.drawable.sample_4,  
  9. R.drawable.sample_5,  
  10. R.drawable.sample_6,  
  11. R.drawable.sample_7  
  12. };  
  13. public ImageAdapter(Context c) { 
  14. //聲明 ImageAdapter  
  15. mContext = c;  
  16. }  
  17. public int getCount() { //擷取圖片的個數  
  18. return mImageIds.length;  
  19. }  
  20. public Object getItem(int position) {
  21. //擷取圖片在庫中的位置  
  22. return position;  
  23. }  
  24. public long getItemId(int position) {
  25. //擷取圖片在庫中的位置  
  26. return position;  
  27. }  
  28. public View getView(int position, View convertView,
     ViewGroup parent) {  
  29. ImageView i = new ImageView(mContext);  
  30. i.setImageResource(mImageIds[position]);
  31. //給ImageView設定資源  
  32. i.setLayoutParams(new Gallery.LayoutParams(200, 200));
  33. //設定布局 圖片200×200顯示  
  34. i.setScaleType(ImageView.ScaleType.FIT_XY);
  35. //設定比例類型  
  36. return i;  

Android Gallery控制項的相關使用方法就為大家介紹到這裡。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.