Android學習筆記之Gallery

openkk 12年前發布 | 41K 次閱讀 Android Android開發 移動開發

<1>簡介

這是一個非常炫的效果,可以用手指直接拖動圖片移動。iPhone曾經憑借這一特效吸引了無數的蘋果粉絲,在Android平臺上也可以實現這一效果。

要實現這一效果,就必須有一個容器來存放Gallery索要顯示的圖片。這里使用一個繼承自BaseAdapter的派生類來裝這些圖片。我們需要監聽其事件setOnItemClickListener,從而確定當前用戶選擇了那一張圖片。

首先,我們需要將顯示的圖片的索引存放在一個int數組中。然后通過setImageResource方法來設置ImageView要顯示的圖片資源。最后將每張圖片的ImageView顯示在屏幕上。

 

<2>XML屬性

 

  • 屬性名稱

    </td>

    描述

    </td> </tr>

    android:animationDuration

    </td>

    設置布局變化時動畫的轉換所需的時間(毫秒級)。僅在動畫開始時計時。該值必須是整數,比如:100

    </td> </tr>

    android:gravity

    </td>

    指定在對象的X和Y軸上如何放置內容。指定一下常量中的一個或多個(使用 “|”分割)

    Constant

    </td>

    Value

    </td>

    Description

    </td> </tr>

    top

    </td>

    0x30

    </td>

    緊靠容器頂端,不改變其大小

    </td> </tr>

    bottom

    </td>

    0x50

    </td>

    緊靠容器底部,不改變其大小

    </td> </tr>

    left

    </td>

    0x03

    </td>

    緊靠容器左側,不改變其大小

    </td> </tr>

    right

    </td>

    0x05

    </td>

    緊靠容器右側,不改變其大小

    </td> </tr>

    center_vertical

    </td>

    0x10

    </td>

    垂直居中,不改變其大小

    </td> </tr>

    fill_vertical

    </td>

    0x70

    </td>

    垂直方向上拉伸至充滿容器

    </td> </tr>

    center_horizontal

    </td>

    0x01

    </td>

    水平居中,不改變其大小

    </td> </tr>

    Fill_horizontal

    </td>

    0x07

    </td>

    水平方向上拉伸使其充滿容器

    </td> </tr>

    center

    </td>

    0x11

    </td>

    居中對齊,不改變其大小

    </td> </tr>

    fill

    </td>

    0x77

    </td>

    在水平和垂直方向上拉伸,使其充滿容器

    </td> </tr>

    clip_vertical

    </td>

    0x80

    </td>

    垂直剪切(當對象邊緣超出容器的時候,將上下邊緣超出的部分剪切掉)

    </td> </tr>

    clip_horizontal

    </td>

    0x08

    </td>

    水平剪切(當對象邊緣超出容器的時候,將左右邊緣超出的部分剪切掉)

    </td> </tr> </tbody> </table> </td> </tr>

    android:spacing

    </td>

    (譯者注:設置圖片之間的間距)

    </td> </tr>

    android:unselectedAlpha

    </td>

    設置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。

    </td> </tr> </tbody> </table>

     

    <3>范例

    Android學習筆記之Gallery

    package xiaosi.gallery;

    import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast;

    public class GalleryActivity extends Activity { /* Called when the activity is first created. / private Gallery gallery =null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

        gallery = (Gallery)findViewById(R.id.gallery);
        //設置圖片適配器
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setSpacing(10);
    
        //設置監聽器
        gallery.setOnItemClickListener(new OnItemClickListener() {
    
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,    long arg3) {
                Toast.makeText(GalleryActivity.this, "點擊了第"+arg2+"張圖片", Toast.LENGTH_LONG).show();
            }
        });
    }
    

    }

    class ImageAdapter extends BaseAdapter{ private Context context; //圖片源數組 private Integer[] imageInteger={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, }; public ImageAdapter(Context c){ context = c; } // 獲取圖片的個數 public int getCount() { return imageInteger.length; } // 獲取圖片在庫中的位置
    public Object getItem(int position) { return position; } // 獲取圖片ID
    public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = new ImageView(context);
        // 給ImageView設置資源
        imageView.setImageResource(imageInteger[position]);
        // 設置顯示比例類型  
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        // 設置布局 圖片120*80 
        imageView.setLayoutParams(new Gallery.LayoutParams(120, 80));
        return imageView;
    }
    

    }</pre>

    main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="
    
    
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:background="?android:galleryItemBackground"/>
    

    </LinearLayout></pre>
    轉自:
    http://blog.csdn.net/sjf0115/article/details/7253332

     本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
  • sesese色