最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android獲取觸摸手勢(shì)實(shí)現(xiàn)左右滑動(dòng)

 更新時(shí)間:2022年05月18日 09:02:21   作者:殤丶  
這篇文章主要為大家詳細(xì)介紹了Android獲取觸摸手勢(shì)實(shí)現(xiàn)左右滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android獲取觸摸手勢(shì)實(shí)現(xiàn)左右滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下

一、Android提供的兩種手勢(shì):

①Android提供了手勢(shì)檢測(cè),并為手勢(shì)提供了相應(yīng)的監(jiān)聽器
②Android允許開發(fā)者添加手勢(shì),并提供了相應(yīng)的API識(shí)別用戶手勢(shì)

二、手勢(shì)檢測(cè):手勢(shì)檢測(cè)器類:GestureDetector

監(jiān)聽器:OnGestureListener,負(fù)責(zé)對(duì)用戶的手勢(shì)行為提供響應(yīng)
時(shí)間處理方法:boolean OnDraw(MotionEvent e):當(dāng)觸摸事件按下時(shí)觸發(fā)該方法
boolean OnFing(MotionEvent e1,MotionEvent e2,float velocity X,float velocity Y):當(dāng)用戶在觸摸屏上“拖過”時(shí)觸發(fā)該方法。其中 velocity X,float velocity Y代表“拖過”動(dòng)作在橫向,縱向上的速度
abstract void onLongPress(MotionEvent e):當(dāng)用戶在屏幕上長(zhǎng)按時(shí)觸發(fā)該方法
onScroll(MotionEvent e,MotionEvent e2,float distanceX,float distanceY):當(dāng)用戶在屏幕上“滾動(dòng)”時(shí)觸發(fā)該方法
void onShowPress(MotionEvent e):當(dāng)用戶在觸摸屏上按下,而還未移動(dòng)和松開時(shí)觸發(fā)該方法

Android收拾檢測(cè)步驟:第一步:創(chuàng)建一個(gè)GestureDetector對(duì)象。創(chuàng)建該對(duì)象時(shí)必須實(shí)現(xiàn)一個(gè)GestureDetector.OnGestureListener監(jiān)聽器實(shí)例:例如:GestureDetector detector=new GestureDetector(this,this)

應(yīng)用程序的Activity的TouchEvent事件綁定監(jiān)聽器,在事件處理中指定Activity上的TouchEvent事件交給GestureDetector處理。例如:detector.OnTouchEvent(event)

例子:①演示事件處理的方法
②通過手勢(shì)實(shí)現(xiàn)翻頁(yè)效果:
ViewFlipper組件,該組件可使用動(dòng)畫控制多個(gè)組件之間的切換效果
flipper.setInAnimation()設(shè)置組件進(jìn)入的動(dòng)畫
flipper.setOutAnimation()設(shè)置組件出去的動(dòng)畫
flipper.showPrevious()顯示上一個(gè)視圖
flipper.showNext()顯示下一個(gè)視圖

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   //進(jìn)

<?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   //進(jìn)

<?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.實(shí)現(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);
? ? ? ? }
? ? ? ? //實(shí)例化手勢(shì)檢測(cè)器類
? ? ? ? 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//滾動(dòng)
? ? ? ? ? ? public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onLongPress(MotionEvent e) {//長(zhǎng)按

? ? ? ? ? ? }

? ? ? ? ? ? @Override//拖動(dòng)
? ? ? ? ? ? public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
? ? ? ? ? ? ? ? if(e1.getX()-e2.getX()>100){//右滑下一張
? ? ? ? ? ? ? ? ? ? rs_viewFlipper.showNext();
? ? ? ? ? ? ? ? ? ? //設(shè)置效果圖右滑下一張的樣式,一張圖片進(jìn),一張圖片出
? ? ? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? ? ? ? ? //設(shè)置效果圖左滑上一張的樣式,一張圖片進(jìn),一張圖片出
? ? ? ? ? ? ? ? ? ? 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);
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

汶上县| 平遥县| 彭州市| 墨玉县| 安乡县| 潮安县| 阜平县| 沂源县| 广宁县| 怀宁县| 商洛市| 静安区| 那坡县| 慈溪市| 通海县| 六安市| 江陵县| 平利县| 南京市| 资中县| 乌鲁木齐市| 马关县| 长阳| 五大连池市| 台北县| 平湖市| 德庆县| 秦皇岛市| 集安市| 南溪县| 论坛| 旬邑县| 大足县| 平江县| 宁海县| 德钦县| 南漳县| 双桥区| 法库县| 梧州市| 通州区|