中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何Android項目中使用ViewFlipper與GestrueDetector實現滑屏效果

發布時間:2020-11-24 15:45:02 來源:億速云 閱讀:152 作者:Leah 欄目:移動開發

如何Android項目中使用ViewFlipper與GestrueDetector實現滑屏效果?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1.main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/bg"
  android:orientation="vertical" >
  <ViewFlipper
    android:id="@+id/viewFlipper1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:persistentDrawingCache="animation"
    android:flipInterval="1000"
    android:inAnimation="@anim/push_left_in"
    android:outAnimation="@anim/push_left_out"
    >
    <include android:id="@+id/lay1" layout="@layout/layout1"/>
    <include android:id="@+id/lay2" layout="@layout/layout2"/>
  </ViewFlipper>
</LinearLayout>

注:layout1和layout2 布局很簡單,就是有一個textview和一個button,就不在這里寫出了。其中包含了兩個特效xml文件,放在res/anim下

1.push_left_in

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<set xmlns:Android="http://schemas.android.com/apk/res/android">
  <translate
  android:fromXDelta="100%p"
  android:toXDelta="0"
  android:duration="500"/>
  <alpha
  android:fromAlpha="0.0"
  android:toAlpha="1.0"
  android:duration="500" />
</set>

2 push_left_out

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
  android:fromXDelta="0"
  android:toXDelta="-100%p"
  android:duration="500"/>
  <alpha
    android:fromAlpha="1.0"
  android:toAlpha="0.0"
  android:duration="500" />
</set>

主類:

package com.wj.gesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class GestureDetectorTest extends Activity implements OnClickListener, OnTouchListener,OnGestureListener,OnDoubleTapListener{
  GestureDetector gestureDetector;
  private Button next = null,up = null;
  private ViewFlipper flipper = null;
  private static final int FLING_MIN_DISTANCE = 100;
  private static final int FLING_MIN_VELOCITY = 200;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initview();
    setListener();
  }
  private void initview(){
    gestureDetector = new GestureDetector(this);
    next = (Button)this.findViewById(R.id.button1);
    up = (Button)this.findViewById(R.id.button2);
    next.setOnClickListener(this);
    up.setOnClickListener(this);
    flipper  = (ViewFlipper)this.findViewById(R.id.viewFlipper1);
    flipper.setLongClickable(true);
  }
  private void setListener(){
    flipper.setOnTouchListener(this);
  }
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    //Toast.makeText(this, "ontouch", 1000).show();
    return gestureDetector.onTouchEvent(event);
  }
  @Override
  public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  @Override
  public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub
  }
  @Override
  public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {
    return false;
  }
  @Override
  public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
  }
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
    if (e1.getX()-e2.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY) {
      flipper.setInAnimation(inFromRightAnimation());
      flipper.setOutAnimation(outToLeftAnimation());
      flipper.showNext();
    }else if (e2.getX()-e1.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX) > FLING_MIN_VELOCITY) {
      flipper.setInAnimation(inFromLeftAnimation());
      flipper.setOutAnimation(outToRightAnimation());
      flipper.showPrevious();
    }
    return false;
  }
  protected Animation inFromRightAnimation() {
    Animation inFromRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, +1.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromRight.setDuration(500);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
  }
  protected Animation outToLeftAnimation() {
    Animation outtoLeft = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, -1.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoLeft.setDuration(500);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
  }
  protected Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, -1.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromLeft.setDuration(500);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
  }
  protected Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, +1.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f,
        Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoRight.setDuration(500);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
  }
  @Override
  public boolean onSingleTapConfirmed(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  @Override
  public boolean onDoubleTap(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  @Override
  public boolean onDoubleTapEvent(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  @Override
  public void onClick(View v) {
    if (v==next) {
      flipper.showNext();
    }else{
      flipper.showPrevious();
    }
  }
}

關于如何Android項目中使用ViewFlipper與GestrueDetector實現滑屏效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

漳浦县| 沽源县| 太白县| 永修县| 白玉县| 民丰县| 忻城县| 安义县| 浦城县| 耒阳市| 溧阳市| 永登县| 六枝特区| 广水市| 满城县| 宣汉县| 明星| 临湘市| 麻城市| 新晃| 阿鲁科尔沁旗| 天峨县| 乐昌市| 那坡县| 湘潭市| 芦溪县| 博客| 安塞县| 古丈县| 新化县| 六安市| 含山县| 三原县| 洞口县| 西藏| 开江县| 任丘市| 那坡县| 陇西县| 怀宁县| 沙河市|