?
? ????? PHP ??? ???? ??? ?? ??
Gallery
版本:Android 2.3 r1
?
結(jié)構(gòu)
繼承關(guān)系
???????? 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(畫廊)使用Theme_galleryItemBackground
作為Gallery(畫廊)適配器中的各視圖的默認(rèn)參數(shù)。如果你沒有設(shè)置,你就需要調(diào)整一些Gallery(畫廊)的屬性,比如間距。
Gallery(畫廊)中的視圖應(yīng)該使用Gallery.LayoutParams作為它們的布局參數(shù)類型。
參見Gallery tutorial。
?
內(nèi)部類
class?????????????????? Gallery.LayoutParams
Gallery(畫廊)擴(kuò)展了LayoutParams,以此提供可以容納當(dāng)前的轉(zhuǎn)換信息和先前的位置轉(zhuǎn)換信息的場所。
?
XML屬性
屬性名稱 |
描述 |
|||||||||||||||||||||||||||||||||||||||
android:animationDuration |
設(shè)置布局變化時動畫的轉(zhuǎn)換所需的時間(毫秒級)。僅在動畫開始時計(jì)時。該值必須是整數(shù),比如:100。 |
|||||||||||||||||||||||||||||||||||||||
android:gravity |
?指定在對象的X和Y軸上如何放置內(nèi)容。指定一下常量中的一個或多個(使用 “|”分割)
|
|||||||||||||||||||||||||||||||||||||||
android:spacing |
(譯者注:設(shè)置圖片之間的間距) |
|||||||||||||||||||||||||||||||||||||||
android:unselectedAlpha |
?設(shè)置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。 |
?
公共方法
public boolean dispatchKeyEvent (KeyEvent event)
???????? 在焦點(diǎn)路徑上分發(fā)按鈕事件到下一個視圖。該路徑從視圖樹的頂端遍歷到當(dāng)前獲得焦點(diǎn)的視圖。如果當(dāng)前視圖已獲得焦點(diǎn),就分發(fā)給自身。否則,就分發(fā)到下一個節(jié)點(diǎn)的焦點(diǎn)路徑上。該方法監(jiān)聽任何按鈕事件。
參數(shù)
??????????????????????????? event??????? 被分發(fā)的按鈕事件
?????????????????? 返回值
??????????????????????????? boolean??? 時間被處理返回true,否則false
?
public void dispatchSetSelected (boolean selected)
分發(fā)setSelected給視圖的子類。
參數(shù)
selected?? 新選中的狀態(tài)
?
public ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)
返回一個新的已設(shè)置屬性集合的布局參數(shù)。
參數(shù)
attrs?????????????????? 用于生成布局參數(shù)的屬性集合
返回值
一個ViewGroup.LayoutParams實(shí)例或者它的子類
????????
public boolean onDown (MotionEvent e)
當(dāng)輕擊和按下手勢事件發(fā)生時通知該方法。任何按下事件都會直接觸發(fā)該方法。所有其他的事件都要先于該方法。
參數(shù)
e?????? 按下動作事件
?
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
當(dāng)初始化的按下動作事件和松開動作事件匹配時通知fling(譯者注:快滑,用戶按下觸摸屏、快速移動后松開)事件。該動作的速度通過計(jì)算X和Y軸上每秒移動多少像素得來。
參數(shù)
e1???? 導(dǎo)致開始fling的按下動作事件。
e2???? 觸發(fā)當(dāng)前onFling方法的移動動作事件
velocityX? 測量fling沿X軸上的速度,像素/每秒
velocityY? 測量fling沿Y軸上的速度,像素/每秒
返回值
如果該事件被消耗返回true,否則false。
?
public boolean onKeyDown (int keyCode, KeyEvent event)
處理左,右和點(diǎn)擊事件
參數(shù)
keyCode?? 代表按下按鈕的按鍵碼,來自KeyEvent
。
event??????? 定義按鈕動作的KeyEvent對象。
返回值
如果已經(jīng)處理了按鈕事件,則返回true。如果你想讓下一個事件接收者處理,就返回false
參見
onKeyDown(int,
KeyEvent)
?
public boolean onKeyUp (int keyCode, KeyEvent event)
KeyEvent.Callback.onKeyMultiple()
方法的默認(rèn)實(shí)現(xiàn):當(dāng)KEYCODE_DPAD_CENTER
或者 KEYCODE_ENTER
被釋放時,執(zhí)行點(diǎn)擊視圖操作。
參數(shù)
keyCode?? 代表按下按鈕的按鍵碼,來自KeyEvent
。
event??????? 定義按鈕動作的KeyEvent對象。
返回值
如果已經(jīng)處理了按鈕事件,則返回true。如果你想讓下一個事件接收者處理,就返回false
?
public void onLongPress (MotionEvent e)
MotionEvent
初始化并按下觸發(fā)長按并通知本方法
。
參數(shù)
e?????? 導(dǎo)致開始長按的初始按下動作事件。
?
?
public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
當(dāng)初始按下動作事件和當(dāng)前移動動作事件導(dǎo)致滾動時通知本方法。為了方便提供了X和Y軸上的距離。
監(jiān)聽屏幕滾動事件。為了方便提供了X和Y軸上的距離。
參數(shù)
e1 ? 導(dǎo)致滾動開始按下的動作事件。
e2 ? 觸發(fā)當(dāng)前onScroll方法的移動動作事件。
distanceX ?????? 距離是自上一次調(diào)用onScroll方法在X軸上的距離。不是e1 和e2之間的距離。
distanceY ?????? 距離是自上一次調(diào)用onScroll方法在Y軸上的距離。不是e1 和e2之間的距離。
返回值
如果該事件被消耗返回true否則false
?
public void onShowPress (MotionEvent e)
用戶已經(jīng)執(zhí)行按下動作還沒有執(zhí)行移動或者彈起動作。該事件常通過高亮一個元素來向用戶提供一個視覺反饋即用戶的操作已經(jīng)被辨識了。
參數(shù)
e?????? 按下動作事件
?
public boolean onSingleTapUp (MotionEvent e)
在輕擊動作和up動作事件觸發(fā)時通知本方法。(譯者注:點(diǎn)擊屏幕上的某項(xiàng)的執(zhí)行流程? 有兩種情況,一種是時間很短,一種時間稍長:時間很短:onDown--->onSingleTapUp--->onSingleTapConfirmed,見 這里1, 這里2。)
參數(shù)
e?????? 完成開始輕擊的up動作事件
返回值
如果該事件被消耗返回true否則false
?
public boolean onTouchEvent (MotionEvent event)
實(shí)現(xiàn)該方法來處理觸摸屏動作事件
參數(shù)
event??????? 動作事件
返回值
如果該事件被消耗返回true否則false
?
public void setAnimationDuration (int animationDurationMillis)
設(shè)置當(dāng)子視圖改變位置時動畫轉(zhuǎn)換時間。僅限于動畫開始時生效。
參數(shù)
animationDurationMillis????????? 動畫轉(zhuǎn)換時間(毫秒級)
????????
public void setCallbackDuringFling (boolean shouldCallback)
當(dāng)flinged時是否回調(diào)每一個getOnItemSelectedListener()
。如果設(shè)為false,只回調(diào)最終選中的項(xiàng)。如果為true,則所有的項(xiàng)都會回調(diào)。
參數(shù)
shouldCallback???????? 設(shè)置拋滑的過程中是否回調(diào)
?
public void setGravity (int gravity)
描述子視圖的對齊方式。
?
public void setSpacing (int spacing)
設(shè)置Gallery中項(xiàng)的間距
參數(shù)
spacing??? Gallery中項(xiàng)的間距,以像素為單位
?
public void setUnselectedAlpha (float unselectedAlpha)
設(shè)置Gallery中未選中項(xiàng)的透明度(alpha)值。
參數(shù)
unselectedAlpha????? 未選中項(xiàng)的透明度(alpha)值
?
public boolean showContextMenu ()
顯示該視圖上下文菜單。
返回值
上下文菜單是否顯示。
?
public boolean showContextMenuForChild (View originalView)
為指定的視圖或者其父類顯示上下文菜單。
大部分情況下,子類不需要重寫該方法。但是,如果子類被直接添加到窗口管理器(例如:addView(View.android.view.ViewGroup.LayoutParams)),此時就需要重寫來顯示上下文菜單
參數(shù)
originalView??? 上下文菜單初次調(diào)用的源視圖
返回值
如果上下文菜單被顯示了 則返回true。
?
受保護(hù)方法
protected int computeHorizontalScrollExtent ()
在水平范圍內(nèi)計(jì)算滾動條滑塊的滾動范圍。該值用來計(jì)算滾動條滑塊的長度。
該范圍可以使用任意的單位但是必須跟computeHorizontalScrollRange()
和computeHorizontalScrollOffset()
的單位
保持一致。
默認(rèn)范圍是視圖的寬度。
返回值
滾動條滑塊的水平滾動范圍
?
protected int computeHorizontalScrollOffset ()
在水平范圍內(nèi)計(jì)算滾動條滑塊的偏移量。該值用來計(jì)算水平滑塊的位置。
該范圍可以使用任意的單位但是必須跟computeHorizontalScrollRange()
和computeHorizontalScrollExtent()
的單位
保持一致。
默認(rèn)偏移量是視圖的偏移量。
返回值
滾動條滑塊的水平偏移量。
?
protected int computeHorizontalScrollRange ()
計(jì)算滾動條水平方向上的滾動范圍。
該范圍可以使用任意的單位但是必須跟computeHorizontalScrollExtent()
和computeHorizontalScrollOffset()
的單位
保持一致。
返回值
水平滾動條代表的滑動總范圍。
?
protected void dispatchSetPressed (boolean pressed)
分發(fā) setPressed到View的子類。
參數(shù)
??????????????????????????? pressed??? 新按下的狀態(tài)
?
protected ViewGroup.LayoutParams generateDefaultLayoutParams ()
返回默認(rèn)的布局參數(shù)。當(dāng)View作為參數(shù)傳遞給addView(View)
而沒有布局參數(shù)時就會請求這些參數(shù)。如果返回null,則addView會拋出異常。
返回值
默認(rèn)的布局參數(shù)或null
?
protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)
返回一組合法的受支持的布局參數(shù)。當(dāng)把ViewGroup傳遞給View而該View的布局參數(shù)并沒有通過checkLayoutParams(android.view.ViewGroup.LayoutParams)的測試時,就會調(diào)用該方法。該方法應(yīng)該返回一組適合該ViewGroup的新的布局參數(shù),該過程可能需要從指定的一組布局參數(shù)中復(fù)制相關(guān)的屬性。
參數(shù)
p?????? 被轉(zhuǎn)換成適合該ViewGroup的一組參數(shù)。
返回值
返回一個ViewGroup.LayoutParams的實(shí)例或者一個它的子類。
?
protected int getChildDrawingOrder (int childCount, int i)
返回迭代的繪制子類索引。如果你想改變子類的繪制順序就要重寫該方法。默認(rèn)返回 i 值。
提示:為了能夠調(diào)用該方法,你必須首先調(diào)用setChildrenDrawingOrderEnabled(boolean)
來允許子類排序。
參數(shù)
???????? childCount??????? 子類個數(shù)
i??????? 當(dāng)前迭代順序
返回值
繪制該迭代子類的索引
?
protected boolean getChildStaticTransformation (View child, Transformation t)
(譯者注:setStaticTransformationsEnabled這個屬性設(shè)成true的時候每次viewGroup(看Gallery的源碼就可以看到它是從ViewGroup間接繼承過來的)在重新畫它的child的時候都會促發(fā)getChildStaticTransformation這個函數(shù)。 這里1、 這里2)
?
protected ContextMenu.ContextMenuInfo getContextMenuInfo ()
Views如果有額外的信息跟上下文菜單有聯(lián)系的話就需要實(shí)現(xiàn)該方法。返回的結(jié)果被用作回調(diào)方法onCreateContextMenu(ContextMenu, View, ContextMenuInfo)的參數(shù)。
返回值
顯示上下文菜單的條目的額外信息。這些信息將會改變View不同的子類
?
protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect)
當(dāng)該視圖的焦點(diǎn)狀態(tài)發(fā)生改變時將會調(diào)用視圖系統(tǒng)。當(dāng)導(dǎo)向的方向觸發(fā)焦點(diǎn)事件時,方向和先前獲得焦點(diǎn)的矩形提供焦點(diǎn)事件的來源。當(dāng)用戶重寫該方法,必須調(diào)用父類方法來觸發(fā)標(biāo)準(zhǔn)的焦點(diǎn)處理事件。
參數(shù)
gainFocus?????????????????? 如果View獲得焦點(diǎn)為true,否則false
direction? ?????? 當(dāng)調(diào)用requestFocus()方法來給該視圖焦點(diǎn)時焦點(diǎn)的移動方向。該值:FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT 或 FOCUS_RIGHT。該參數(shù)不常用,通常使用它的默認(rèn)值。
previouslyFocusedRect??? 該視圖坐標(biāo)系統(tǒng)中先前獲得焦點(diǎn)的視圖的矩形。如果適用,這將獲得焦點(diǎn)事件來源的更細(xì)致的信息(除了方向以外)。否則為null。
?
protected void onLayout (boolean changed, int l, int t, int r, int b)
當(dāng)視圖為每一個子類分配大小和位置時從布局中調(diào)用該方法。有子類的派生類應(yīng)該重寫該方法在子類中調(diào)用布局。
參數(shù)
changed?? 該視圖新的大小和位置。
l??????? 相對父容器的左側(cè)位置
t??????? 相對父容器的頂部位置
r??????? 相對父容器的右側(cè)位置
b?????? 相對父容器的底部位置
?
補(bǔ)充
文章精選
???????? Android開發(fā)——使用Gallery實(shí)現(xiàn)“多級聯(lián)動”
???????? android 圖片拖動效果(Gallery)
????????
示例代碼
???????? MyGallery.java
public class MyGallery extends Activity {
???
? 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));//設(shè)置圖片適配器
??????? //設(shè)置監(jiān)聽器
??????? gallery.setOnItemClickListener(new OnItemClickListener() {
???????? @Override
???????? public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,? long arg3) {
??????????? Toast.makeText(MyGallery.this, "點(diǎn)擊了第"+arg2+"張圖片", Toast.LENGTH_LONG).show();
???????? }
???? });
??? }
}
?
class ImageAdapter extends BaseAdapter{
? private Context context;
? //圖片源數(shù)組
? 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;
? }
? @Override
? public int getCount() {
???? return imageInteger.length;
? }
? @Override
? public Object getItem(int position) {
???? return position;
? }
? @Override
? public long getItemId(int position) {
???? // TODO Auto-generated method stub
???? return position;
? }
? @Override
? 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;
? }}
?????? main.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"
?? ???? />