android Gesture 簡單Demo 源碼 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

今天想學習一下anddroid Gesture.在網上找瞭些資料,自己寫瞭個小Demo.

 

實現瞭手動拖動圖片

 

用戶可以從左往右、從右往左、從上到下、從下到上拖動圖片。

 

圖片進入時添加瞭簡單的Animation動畫.

 

有需要的同學可以下載下來看看。

 

 擦擦,發現不能上傳rar文件

那隻能貼出來瞭。。

Java類:

 

 

package com.covics.zfh; 

 

import android.app.Activity; 

import android.content.Context; 

import android.os.Bundle; 

import android.view.GestureDetector; 

import android.view.GestureDetector.OnGestureListener; 

import android.view.MotionEvent; 

import android.view.View; 

import android.view.View.OnTouchListener; 

import android.view.animation.Animation; 

import android.view.animation.AnimationUtils; 

import android.widget.ImageView; 

import android.widget.TextView; 

import android.widget.Toast; 

 

public class HelloGestureActivity extends Activity implements OnTouchListener, OnGestureListener{ 

     

    private static final String TAG = "GestureActivity——->"; 

     

    private GestureDetector mGestureDetector = new GestureDetector(this); 

    private TextView tv; 

    private ImageView ivImage; 

    private int[] imageId = {R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, 

                             R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7}; 

    private int mCurrentIndex = 0; 

    private Animation leftInAnimation; 

    private Animation upInAnimation; 

    private Animation rightInAnimation; 

    private Animation downInAnimation; 

    private Context mContext; 

    /** Called when the activity is first created. */ 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        mContext = getApplicationContext(); 

        init(); 

         

        ivImage.setOnTouchListener(this); 

        ivImage.setFocusable(true); 

        ivImage.setClickable(true); 

        ivImage.setLongClickable(true); 

        ivImage.setImageResource(imageId[mCurrentIndex]); 

        mGestureDetector.setIsLongpressEnabled(true); 

    } 

     

    private void init(){ 

        ivImage = (ImageView) findViewById(R.id.image); 

        leftInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.left_in); 

        upInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.up_in); 

        rightInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.right_in); 

        downInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.down_in); 

    } 

 

    // 在onTouch()方法中我們調用GestureDetector的onTouchEvent方法,將捕捉到 的 

    // MotionEvent交給GestureDetector 來分析是否有合適的callBack函數來處理手勢 

    @Override 

    public boolean onTouch(View v, MotionEvent event) { 

        // TODO Auto-generated method stub 

        return mGestureDetector.onTouchEvent(event); 

    } 

     

    // 用戶輕點觸摸屏,由1個MotionEvent ACTION_DOWN觸發 

    @Override 

    public boolean onDown(MotionEvent e) { 

        return false; 

    } 

 

    // 用戶輕觸觸摸屏後松開,由1個MotionEvent ACTION_UP觸發 

    @Override 

    public boolean onSingleTapUp(MotionEvent e) { 

        // TODO Auto-generated method stub 

        return false; 

    } 

 

    // 用戶輕點觸摸屏,尚末松開或者拖動,由1個MotionEvent ACTION_DOWN觸發 

    // 註意和onDown()的區別,是沒有松開或者拖動的狀態 

    @Override 

    public void onShowPress(MotionEvent e) { 

        // TODO Auto-generated method stub 

    } 

 

    // 用戶按下觸摸屏並拖動 由1個MotionEvent EVENT_DOWN 多個EVENT_MOVE觸發 

    @Override 

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 

            float distanceY) { 

        // TODO Auto-generated method stub 

        return false; 

    } 

 

    // 用戶長按觸摸屏,由多個MotionEvent EVENT_DOWN觸發 

    @Override 

    public void onLongPress(MotionEvent e) { 

        // TODO Auto-generated method stub 

         

    } 

 

    // 用戶按下觸摸屏,快速移動後松開,由1個MotionEvent ACTION_DOWN  

    // 多個EVENT_MOVE 和1個EVENT_UP觸發 

    @Override 

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 

            float velocityY) { 

        // TODO Auto-generated method stub 

        // 參數解釋:    

        // e1:第1個ACTION_DOWN MotionEvent    

        // e2:最後一個ACTION_MOVE MotionEvent    

        // velocityX:X軸上的移動速度,像素/秒    

        // velocityY:Y軸上的移動速度,像素/秒    

       

        // 觸發條件 :    

        // X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒 

        int FLING_MIN_DISTANCE = 100;  

        int FLING_MIN_VELOCITY = 200; 

        if ((e1.getX() – e2.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 

 

            System.out.println(TAG + "onFling left"); 

            if (mCurrentIndex == imageId.length – 1) { 

                Toast.makeText(mContext, "當前已經是最後一張瞭", Toast.LENGTH_LONG).show(); 

            } else { 

                mCurrentIndex++; 

                leftInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.left_in); 

                ivImage.setAnimation(leftInAnimation); 

                ivImage.setImageResource(imageId[mCurrentIndex]); 

            } 

        } else if ((e2.getX() – e1.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 

             

            System.out.println(TAG + "onFling right"); 

            if (mCurrentIndex == 0) { 

                 

                Toast.makeText(mContext, "當前已經是第一張瞭", Toast.LENGTH_LONG).show(); 

            } else { 

                mCurrentIndex–; 

                rightInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.right_in); 

                ivImage.setAnimation(rightInAnimation); 

                ivImage.setImageResource(imageId[mCurrentIndex]); 

            } 

        } else if ((e1.getY() – e2.getY()) > FLING_MIN_DISTANCE && Math.abs(velocityY) > FLING_MIN_VELOCITY) { 

            System.out.println(TAG + "onFling up"); 

            if (mCurrentIndex == imageId.length – 1) { 

                Toast.makeText(mContext, "當前已經是最後一張瞭", Toast.LENGTH_LONG).show(); 

            } else { 

                mCurrentIndex++; 

//              ivImage.setAnimation(rightOutAnimation); 

//              leftInAnimation.setDuration(1000); 

                upInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.up_in); 

                ivImage.setAnimation(upInAnimation); 

                ivImage.setImageResource(imageId[mCurrentIndex]); 

            } 

        } else if ((e2.getY() – e1.getY()) > FLING_MIN_DISTANCE && Math.abs(velocityY) > FLING_MIN_VELOCITY) { 

            System.out.println(TAG + "onFling down"); 

            if (mCurrentIndex == 0) { 

                Toast.makeText(mContext, "當前已經是第一張瞭", Toast.LENGTH_LONG).show(); 

            } else { 

                mCurrentIndex–; 

                downInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.down_in); 

                ivImage.setAnimation(downInAnimation); 

                ivImage.setImageResource(imageId[mCurrentIndex]); 

            } 

        } 

        return false; 

    } 

 

 

 

下面是動畫效果XML實現

down_in.xml

 

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="https://schemas.android.com/apk/res/android" > 

    <translate 

        android:fromYDelta="-100%p" 

        android:toYDelta="0" 

        android:duration="900" 

     /> 

</set> 

left_in.xml

 

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="https://schemas.android.com/apk/res/android" > 

    <translate 

        android:fromXDelta="100%p" 

        android:toXDelta="0" 

        android:duration="800" 

     /> 

</set> 

     

     

 

right_in.xml

 

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="https://schemas.android.com/apk/res/android" > 

    <translate 

        android:fromXDelta="-100%p" 

        android:toXDelta="0" 

        android:duration="800" 

     /> 

</set> 

     

      

 

up_in.xml

 

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="https://schemas.android.com/apk/res/android" > 

    <translate 

        android:fromYDelta="100%p" 

        android:toYDelta="0" 

        android:duration="900" 

     /> 

</set> 

     

     

 

佈局文件:

main.xml:   

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

 

    <TextView 

        android:id="@+id/tv" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="@string/hello" /> 

    <ImageView  

        android:id="@+id/image" 

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent" 

        /> 

</LinearLayout> 

 

記得添加圖片哈

 

摘自 wanzdhan的專欄

發佈留言