一、結構
public class Gallery extends AbsSpinner
implements GestureDetector.OnGestureListener
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsSpinner
android.widget.Gallery
二、概述
一個鎖定中心條目并且擁有水準滾動清單的視圖。
Gallery(畫廊)中的視圖應該使用Gallery.LayoutParams作為它們的布局參數類型。
三、内部類
class Gallery.LayoutParams
Gallery(畫廊)擴充了LayoutParams,以此提供可以容納目前的轉換資訊和先前的位置轉換資訊的場所。
四、XML屬性
屬性名稱
描述
android:animationDuration
設定布局變化時動畫的轉換所需的時間(毫秒級)。僅在動畫開始時計時。該值必須是整數,比如:100。
android:gravity
指定在對象的X和Y軸上如何放置内容。指定一下常量中的一個或多個(使用 “|”分割)
Constant
Value
Description
top
0x30
緊靠容器頂端,不改變其大小
bottom
0x50
緊靠容器底部,不改變其大小
left
0x03
緊靠容器左側,不改變其大小
right<code></code>
0x05
緊靠容器右側,不改變其大小
center_vertical<code></code>
0x10
垂直居中,不改變其大小
fill_vertical<code></code>
0x70
垂直方向上拉伸至充滿容器
center_horizontal<code></code>
0x01
水準居中,不改變其大小
Fill_horizontal<code></code>
0x07
水準方向上拉伸使其充滿容器
center<code></code>
0x11
居中對齊,不改變其大小
fill<code></code>
0x77
在水準和垂直方向上拉伸,使其充滿容器
clip_vertical<code></code>
0x80
垂直剪切(當對象邊緣超出容器的時候,将上下邊緣超出的部分剪切掉)
clip_horizontal<code></code>
0x08
水準剪切(當對象邊緣超出容器的時候,将左右邊緣超出的部分剪切掉)
android:spacing
(譯者注:設定圖檔之間的間距)
android:unselectedAlpha
設定未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。
五、公共方法
public boolean dispatchKeyEvent (KeyEvent event)
在焦點路徑上分發按鈕事件到下一個視圖。該路徑從視圖樹的頂端周遊到目前獲得焦點的視圖。如果目前視圖已獲得焦點,就分發給自身。否則,就分發到下一個節點的焦點路徑上。該方法監聽任何按鈕事件。
參數
event 被分發的按鈕事件
傳回值
時間被處理傳回true,否則false
public void dispatchSetSelected (boolean selected)
分發setSelected給視圖的子類。
selected 新選中的狀态
public ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)
傳回一個新的已設定屬性集合的布局參數。
attrs 用于生成布局參數的屬性集合
傳回值
一個ViewGroup.LayoutParams執行個體或者它的子類
public boolean onDown (MotionEvent e)
當輕擊和按下手勢事件發生時通知該方法。任何按下事件都會直接觸發該方法。所有其他的事件都要先于該方法。
e 按下動作事件
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
當初始化的按下動作事件和松開動作事件比對時通知fling(譯者注:快滑,使用者按下觸摸屏、快速移動後松開)事件。該動作的速度通過計算X和Y軸上每秒移動多少像素得來。
e1 導緻開始fling的按下動作事件。
e2 觸發目前onFling方法的移動動作事件
velocityX 測量fling沿X軸上的速度,像素/每秒
velocityY 測量fling沿Y軸上的速度,像素/每秒
如果該事件被消耗傳回true,否則false。
public boolean onKeyDown (int keyCode, KeyEvent event)
處理左,右和點選事件
event 定義按鈕動作的KeyEvent對象。
如果已經處理了按鈕事件,則傳回true。如果你想讓下一個事件接收者處理,就傳回false
參見
public boolean onKeyUp (int keyCode, KeyEvent event)
public void onLongPress (MotionEvent e)
e 導緻開始長按的初始按下動作事件。
public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
當初始按下動作事件和目前移動動作事件導緻滾動時通知本方法。為了友善提供了X和Y軸上的距離。
監聽螢幕滾動事件。為了友善提供了X和Y軸上的距離。
e1 導緻滾動開始按下的動作事件。
e2 觸發目前onScroll方法的移動動作事件。
distanceX 距離是自上一次調用onScroll方法在X軸上的距離。不是e1 和e2之間的距離。
distanceY 距離是自上一次調用onScroll方法在Y軸上的距離。不是e1 和e2之間的距離。
如果該事件被消耗傳回true否則false
public void onShowPress (MotionEvent e)
使用者已經執行按下動作還沒有執行移動或者彈起動作。該事件常通過高亮一個元素來向使用者提供一個視覺回報即使用者的操作已經被辨識了。
public boolean onSingleTapUp (MotionEvent e)
e 完成開始輕擊的up動作事件
public boolean onTouchEvent (MotionEvent event)
實作該方法來處理觸摸屏動作事件
event 動作事件
public void setAnimationDuration (int animationDurationMillis)
設定當子視圖改變位置時動畫轉換時間。僅限于動畫開始時生效。
animationDurationMillis 動畫轉換時間(毫秒級)
public void setCallbackDuringFling (boolean shouldCallback)
shouldCallback 設定抛滑的過程中是否回調
public void setGravity (int gravity)
描述子視圖的對齊方式。
public void setSpacing (int spacing)
設定Gallery中項的間距
spacing Gallery中項的間距,以像素為機關
public void setUnselectedAlpha (float unselectedAlpha)
設定Gallery中未選中項的透明度(alpha)值。
unselectedAlpha 未選中項的透明度(alpha)值
public boolean showContextMenu ()
顯示該視圖上下文菜單。
上下文菜單是否顯示。
public boolean showContextMenuForChild (View originalView)
為指定的視圖或者其父類顯示上下文菜單。
大部分情況下,子類不需要重寫該方法。但是,如果子類被直接添加到視窗管理器(例如:addView(View.android.view.ViewGroup.LayoutParams)),此時就需要重寫來顯示上下文菜單
originalView 上下文菜單初次調用的源視圖
如果上下文菜單被顯示了 則傳回true。
六、受保護方法
protected int computeHorizontalScrollExtent ()
在水準範圍内計算滾動條滑塊的滾動範圍。該值用來計算滾動條滑塊的長度。
預設範圍是視圖的寬度。
滾動條滑塊的水準滾動範圍
protected int computeHorizontalScrollOffset ()
在水準範圍内計算滾動條滑塊的偏移量。該值用來計算水準滑塊的位置。
預設偏移量是視圖的偏移量。
滾動條滑塊的水準偏移量。
protected int computeHorizontalScrollRange ()
計算滾動條水準方向上的滾動範圍。
水準滾動條代表的滑動總範圍。
protected void dispatchSetPressed (boolean pressed)
分發 setPressed到View的子類。
pressed 新按下的狀态
protected ViewGroup.LayoutParams generateDefaultLayoutParams ()
預設的布局參數或null
protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)
傳回一組合法的受支援的布局參數。當把ViewGroup傳遞給View而該View的布局參數并沒有通過checkLayoutParams(android.view.ViewGroup.LayoutParams)的測試時,就會調用該方法。該方法應該傳回一組适合該ViewGroup的新的布局參數,該過程可能需要從指定的一組布局參數中複制相關的屬性。
p 被轉換成适合該ViewGroup的一組參數。
傳回一個ViewGroup.LayoutParams的執行個體或者一個它的子類。
protected int getChildDrawingOrder (int childCount, int i)
傳回疊代的繪制子類索引。如果你想改變子類的繪制順序就要重寫該方法。預設傳回 i 值。
childCount 子類個數
i 目前疊代順序
繪制該疊代子類的索引
protected boolean getChildStaticTransformation (View child, Transformation t)
protected ContextMenu.ContextMenuInfo getContextMenuInfo ()
Views如果有額外的資訊跟上下文菜單有聯系的話就需要實作該方法。傳回的結果被用作回調方法onCreateContextMenu(ContextMenu, View, ContextMenuInfo)的參數。
顯示上下文菜單的條目的額外資訊。這些資訊将會改變View不同的子類
protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect)
當該視圖的焦點狀态發生改變時将會調用視圖系統。當導向的方向觸發焦點事件時,方向和先前獲得焦點的矩形提供焦點事件的來源。當使用者重寫該方法,必須調用父類方法來觸發标準的焦點處理事件。
gainFocus 如果View獲得焦點為true,否則false
direction 當調用requestFocus()方法來給該視圖焦點時焦點的移動方向。該值:FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT 或 FOCUS_RIGHT。該參數不常用,通常使用它的預設值。
previouslyFocusedRect 該視圖坐标系統中先前獲得焦點的視圖的矩形。如果适用,這将獲得焦點事件來源的更細緻的資訊(除了方向以外)。否則為null。
protected void onLayout (boolean changed, int l, int t, int r, int b)
當視圖為每一個子類配置設定大小和位置時從布局中調用該方法。有子類的派生類應該重寫該方法在子類中調用布局。
changed 該視圖新的大小和位置。
l 相對父容器的左側位置
t 相對父容器的頂部位置
r 相對父容器的右側位置
b 相對父容器的底部位置
七、補充
文章精選
示例代碼
Java檔案
public class MyGallery extends Activity {
/** Called when the activity is first created. */
private Gallery gallery;
@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.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(MyGallery.this, "點選了第"+arg2+"張圖檔", Toast.LENGTH_LONG).show();
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context context;
//圖檔源數組
private Integer[] imageInteger={
R.drawable.gallery_photo_1,
R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,
R.drawable.gallery_photo_4,
R.drawable.gallery_photo_5,
R.drawable.gallery_photo_6,
R.drawable.gallery_photo_7,
R.drawable.gallery_photo_8
};
public ImageAdapter(Context c){
context = c;
public int getCount() {
return imageInteger.length;
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
public long getItemId(int position) {
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageInteger[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
return imageView;
XML檔案
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="?android:galleryItemBackground"
/>