Android獲取觸摸手勢實現(xiàn)左右滑動
本文實例為大家分享了Android獲取觸摸手勢實現(xiàn)左右滑動的具體代碼,供大家參考,具體內容如下
一、Android提供的兩種手勢:
①Android提供了手勢檢測,并為手勢提供了相應的監(jiān)聽器
②Android允許開發(fā)者添加手勢,并提供了相應的API識別用戶手勢
二、手勢檢測:手勢檢測器類:GestureDetector
監(jiān)聽器:OnGestureListener,負責對用戶的手勢行為提供響應
時間處理方法:boolean OnDraw(MotionEvent e):當觸摸事件按下時觸發(fā)該方法
boolean OnFing(MotionEvent e1,MotionEvent e2,float velocity X,float velocity Y):當用戶在觸摸屏上“拖過”時觸發(fā)該方法。其中 velocity X,float velocity Y代表“拖過”動作在橫向,縱向上的速度
abstract void onLongPress(MotionEvent e):當用戶在屏幕上長按時觸發(fā)該方法
onScroll(MotionEvent e,MotionEvent e2,float distanceX,float distanceY):當用戶在屏幕上“滾動”時觸發(fā)該方法
void onShowPress(MotionEvent e):當用戶在觸摸屏上按下,而還未移動和松開時觸發(fā)該方法
Android收拾檢測步驟:第一步:創(chuàng)建一個GestureDetector對象。創(chuàng)建該對象時必須實現(xiàn)一個GestureDetector.OnGestureListener監(jiān)聽器實例:例如:GestureDetector detector=new GestureDetector(this,this)
應用程序的Activity的TouchEvent事件綁定監(jiān)聽器,在事件處理中指定Activity上的TouchEvent事件交給GestureDetector處理。例如:detector.OnTouchEvent(event)
例子:①演示事件處理的方法
②通過手勢實現(xiàn)翻頁效果:
ViewFlipper組件,該組件可使用動畫控制多個組件之間的切換效果
flipper.setInAnimation()設置組件進入的動畫
flipper.setOutAnimation()設置組件出去的動畫
flipper.showPrevious()顯示上一個視圖
flipper.showNext()顯示下一個視圖
1.MXL如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" ? ? android:layout_height="match_parent" tools:context="com.example.android_gesture.MainActivity"> ? ? <ViewFlipper ? ? ? ? android:id="@+id/rs_ViewFlipper" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? > ? ? </ViewFlipper> </LinearLayout>
2.樣式
類名:left.in.xml //進
<?xml version="1.0" encoding="utf-8"?> ? ? <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:fillAfter="true" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="-100%p" ? ? ? ? android:toXDelta="0" ? ? ? ? ></translate> </set>
類名:left.out.xml //出
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:fillAfter="true" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="-100%p" ? ? ? ? ></translate> </set>
類名:fight.in.xml //進
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:fillAfter="true" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="100%p" ? ? ? ? android:toXDelta="0" ? ? ? ? ></translate> </set>
類名:fight.out.xml //出
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:fillAfter="true" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="100%p" ? ? ? ? ></translate> </set>
3.實現(xiàn)JAVA類
package com.example.android_gesture;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity {
? ? private ViewFlipper rs_viewFlipper;
? ? private int image[]={R.drawable.s2,R.drawable.s4,R.drawable.s9};
? ? private GestureDetector gd;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? rs_viewFlipper = (ViewFlipper) findViewById(R.id.rs_ViewFlipper);
? ? ? ? for (int i = 0; i <image.length ; i++) {
? ? ? ? ? ? ImageView images=new ImageView(this);
? ? ? ? ? ? images.setImageResource(image[i]);
? ? ? ? ? ? rs_viewFlipper.addView(images);
? ? ? ? }
? ? ? ? //實例化手勢檢測器類
? ? ? ? gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onDown(MotionEvent e) {//按下
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onShowPress(MotionEvent e) {//按下但是還未抬起
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onSingleTapUp(MotionEvent e) {//輕按,按一下,立刻抬起
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override//滾動
? ? ? ? ? ? public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onLongPress(MotionEvent e) {//長按
? ? ? ? ? ? }
? ? ? ? ? ? @Override//拖動
? ? ? ? ? ? public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
? ? ? ? ? ? ? ? if(e1.getX()-e2.getX()>100){//右滑下一張
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.showNext();
? ? ? ? ? ? ? ? ? ? //設置效果圖右滑下一張的樣式,一張圖片進,一張圖片出
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.setInAnimation(MainActivity.this,R.anim.right_in);
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.setOutAnimation(MainActivity.this,R.anim.left_out);
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "右滑下一張", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(e2.getX()-e1.getX()>100){//左滑上一張
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.showPrevious();
? ? ? ? ? ? ? ? ? ? //設置效果圖左滑上一張的樣式,一張圖片進,一張圖片出
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.setInAnimation(MainActivity.this,R.anim.left_in);
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.setOutAnimation(MainActivity.this,R.anim.right_out);
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "左滑上一張", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? @Override//觸摸事件
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? return gd.onTouchEvent(event);
? ? }
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android 高仿微信朋友圈動態(tài)支持雙擊手勢放大并滑動查看圖片效果
- Android實現(xiàn)圖片自動輪播并且支持手勢左右無限滑動
- Android實現(xiàn)手勢滑動和簡單動畫效果
- Android實現(xiàn)手勢滑動多點觸摸放大縮小圖片效果
- Android手勢滑動實現(xiàn)ImageView縮放圖片大小
- Android GestureDetector手勢滑動使用實例講解
- android中view手勢滑動沖突的解決方法
- Android自定義View實現(xiàn)隨手勢滑動控件
- Android實現(xiàn)手勢滑動多點觸摸縮放平移圖片效果
- Android實現(xiàn)手勢滑動多點觸摸縮放平移圖片效果(二)
相關文章
Android RecyclerView的刷新分頁的實現(xiàn)
這篇文章主要介紹了Android RecyclerView的刷新分頁的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android Material Design 陰影實現(xiàn)示例
這篇文章主要介紹了Android Material Design 陰影實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
詳解用RxJava實現(xiàn)事件總線(Event Bus)
本篇文章主要介紹了用RxJava實現(xiàn)事件總線(Event Bus),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

