(Android) 單擊屏幕事件和滑動屏幕事件共存的解決方案

     實現功能:佈局文件中有兩個控件,分別是ImageSwitcher和Gallery控件,Gallery默認的情況下是不可見的。當進入程序的界面時,如果滑動屏幕,會切換到下一張或者前一張圖片,根據你滑動屏幕的方向來判斷,當單擊屏幕時,會顯示Gallery控件,再單擊屏幕時,會隱藏Gallery控件,這個問題不是我自己解決的,通過論壇提問,AMinfo幫我解決的,感謝他。
        心得:我解決這個問題的方案是:要實現兩個監聽器,分別是:OnTouchListener和OnGestureListener,怎麼嘗試都無法區分單擊屏幕和滑動屏幕動作。AMinfo的解決方案是在OnTouchListener的重寫onTouch方法中添加如下的代碼,如果我在一個人這樣做下去,我都不知道什麼時候完成這麼一個小功能呢?讓我明白需要大傢幫助的時候,不可一意孤行,不但浪費時間,而且傷身啊,當然,自己也要先想想怎麼去解決這個問題嘍。
[java]
// 數字4和數字0分別表示的是不可見和可見, 
// 也可以使用View.INVISIBLE和View.VISIBLE來代替數字. 
else 

    if (gallery.getVisibility() == 4) 
        gallery.setVisibility(0); 
    else 
        gallery.setVisibility(4); 

        下面,貼上完整的小應用代碼:
Album.java,項目中要用到的圖片自己提供哦
[java] 
package com.treasure.ui; 
 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Gallery.LayoutParams; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher.ViewFactory; 
 
/**
 * 實現相冊瀏覽功能
 */ 
public class Album extends Activity implements OnItemSelectedListener, ViewFactory, OnTouchListener 

    private ImageSwitcher imageSwitcher; 
    private Gallery gallery; 
     
    private int selectedTag = 0; 
    private int downX, upX; 
     
    private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,  
            R.drawable.f, R.drawable.g}; 
    private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,  
            R.drawable.f, R.drawable.g}; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState)  
    { 
        super.onCreate(savedInstanceState); 
         
        setContentView(R.layout.main); 
         
        imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher); 
        imageSwitcher.setFactory(this); 
        //設置圖片切換時的動畫效果 
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 
        imageSwitcher.setOnTouchListener(this); 
         
        gallery = (Gallery)findViewById(R.id.gallery); 
        //自定義ImageAdapter繼承於BaseAdapter,是一個內部類 
        gallery.setAdapter(new ImageAdapter(this)); 
        gallery.setOnItemSelectedListener(this); 
    } 
    @Override 
    public View makeView() 
    { 
        ImageView image = new ImageView(this); 
        image.setScaleType(ImageView.ScaleType.FIT_XY); 
        image.setLayoutParams(new ImageSwitcher.LayoutParams( 
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        return image; 
    } 
 
    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
            long arg3) 
    { 
        selectedTag = arg2; 
        imageSwitcher.setImageResource(imagesId[arg2]); 
    } 
 
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) 
    {} 
    public class ImageAdapter extends BaseAdapter 
    { 
        private Context context; 
        int galleryItemBackground; 
        public ImageAdapter (Context c) 
        { 
            context = c; 
            TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery); 
            galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); 
            typeArray.recycle(); 
        } 
        @Override 
        public int getCount()  
        { 
            //返回selectId[]的長度 
            return selectId.length; 
        } 
 
        @Override 
        public Object getItem(int position) 
        { 
            return position; 
        } 
 
        @Override 
        public long getItemId(int position)  
        { 
            return position; 
        } 
 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent)  
        { 
            ImageView imageView = new ImageView(context); 
            //設置資源圖片 
            imageView.setImageResource(selectId[position]); 
            imageView.setAdjustViewBounds(true);  //允許調整邊框 
            //設定底部畫廊,自適應大小 
            imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
            //設置畫廊背景 
            imageView.setBackgroundResource(galleryItemBackground); 
            return imageView; 
        } 
         
    } 
     
    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
         
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        { 
            downX = (int)event.getX();  // 取得按下時的坐標x 
            return true; 
        } 
        else if (event.getAction() == MotionEvent.ACTION_UP) 
        { 
            upX = (int)event.getX();    // 取得松開時的坐標x; 
            if (upX – downX > 100) 
            { 
                // 從左拖到右,即看前一張 
                // 如果是第一,則去到尾部 
                if (gallery.getSelectedItemPosition() == 0) 
                    selectedTag = gallery.getCount() – 1; 
                else 
                    selectedTag = gallery.getSelectedItemPosition() – 1; 
                 
                imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
                        R.anim.push_right_in)); 
                imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
                        R.anim.push_right_out)); 
            } 
            else if (downX – upX > 100) 
            { 
                // 從右拖到左,即看後一張 
                // 如果是最後,則去到第一 
                if (gallery.getSelectedItemPosition()  
                        == (gallery.getCount() – 1)) 
                    selectedTag = 0; 
                else 
                    selectedTag = gallery.getSelectedItemPosition() + 1; 
                imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
                        R.anim.push_left_in)); 
                imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
                        R.anim.push_left_out)); 
            } 
             
            else 
            { 
                if (gallery.getVisibility() == 4) 
                    gallery.setVisibility(0); 
                else 
                    gallery.setVisibility(4); 
            } 
                 
             
            // 改變gallery圖片所選,自動觸發ImageSwitcher的setOnItemSelectedListener 
            gallery.setSelection(selectedTag, true); 
            return true; 
        } 
         
        return false; 
    } 

res\layout\main.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <ImageSwitcher android:id="@+id/switcher" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true"/> 
     
    <Gallery android:id="@+id/gallery" 
        android:layout_width="fill_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_height="150dp" 
        android:spacing="10dp" 
        android:layout_alignParentBottom="true" 
        android:gravity="center_vertical" 
        android:visibility="invisible"/> 
     
</RelativeLayout> 

res\values\attrs.xml
[html]
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Gallery"> 
        <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources> 

res\anim\push_left_in.xml
[html]
<?xml version="1.0" encoding="utf-8"?> 
<!– 左進漸變效果 –> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="1500" 
        android:fromXDelta="100%p" 
        android:toXDelta="0" /> 
 
    <alpha 
        android:duration="1500" 
        android:fromAlpha="0.1" 
        android:toAlpha="1.0" /> 
</set> 

res\anim\push_left_out.xml
[html]
<?xml version="1.0" encoding="utf-8"?> 
<!– 右出漸變效果 –> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="1500" 
        android:fromXDelta="0" 
        android:toXDelta="-100%p" /> 
 
    <alpha 
        android:duration="1500" 
        android:fromAlpha="1.0" 
        android:toAlpha="0.1" /> 
</set> 

res\anim\push_right_in.xml
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?> 
<!– 右進漸變效果 –> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="1500" 
        android:fromXDelta="-100%p" 
        android:toXDelta="0" /> 
 
    <alpha 
        android:duration="1500" 
        android:fromAlpha="0.1" 
        android:toAlpha="1.0" /> 
</set> 

res\anim\push_right_out.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<!– 右出漸變效果 –> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="1500" 
        android:fromXDelta="0" 
        android:toXDelta="100%p" /> 
 
    <alpha 
        android:duration="1500" 
        android:fromAlpha="1.0" 
        android:toAlpha="0.1" /> 
</set> 

效果 圖:
進入界面沒有單擊屏幕,默認是隱藏Gallery

進入界面,當單擊屏幕,會顯示Gallery

向右滑動時,顯示下一張圖片

向左滑動時,顯示上一張圖片

發佈留言