• 大小: 0.04M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-02-01
  • 标签: 解锁  

资源简介

package com.codeshogun.android.swipesample;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class Main extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private GestureDetector gestureDetector;
	View.OnTouchListener gestureListener;
	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
        
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }
    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                	viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                	viewFlipper.showNext();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                	viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                	viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
	        return true;
	    else
	    	return false;
    }
}

资源截图

代码片段和文件信息

package com.codeshogun.android.swipesample;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class Main extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
    private Animation slideRightOut

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2009-04-16 21:37  SwipeSample\
     文件         232  2009-04-16 21:37  SwipeSample\.classpath
     文件         814  2009-04-16 21:37  SwipeSample\.project
     文件         650  2009-04-16 21:37  SwipeSample\AndroidManifest.xml
     目录           0  2009-04-16 21:37  SwipeSample\assets\
     目录           0  2009-04-16 21:45  SwipeSample\bin\
     文件        5500  2009-04-16 21:45  SwipeSample\bin\classes.dex
     目录           0  2009-04-16 21:37  SwipeSample\bin\com\
     目录           0  2009-04-16 21:37  SwipeSample\bin\com\codeshogun\
     目录           0  2009-04-16 21:37  SwipeSample\bin\com\codeshogun\android\
     目录           0  2009-04-16 21:43  SwipeSample\bin\com\codeshogun\android\swipesample\
     文件        1087  2009-04-16 21:45  SwipeSample\bin\com\codeshogun\android\swipesample\Main$1.class
     文件        1712  2009-04-16 21:45  SwipeSample\bin\com\codeshogun\android\swipesample\Main$MyGestureDetector.class
     文件        2687  2009-04-16 21:45  SwipeSample\bin\com\codeshogun\android\swipesample\Main.class
     文件         560  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R$anim.class
     文件         388  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R$attr.class
     文件         448  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R$drawable.class
     文件         433  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R$id.class
     文件         442  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R$layout.class
     文件         475  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R$string.class
     文件         705  2009-04-16 21:41  SwipeSample\bin\com\codeshogun\android\swipesample\R.class
     文件        7890  2009-04-16 21:41  SwipeSample\bin\resources.ap_
     文件       12393  2009-04-16 21:45  SwipeSample\bin\SwipeSample.apk
     目录           0  2009-04-16 21:41  SwipeSample\res\
     目录           0  2009-04-16 21:41  SwipeSample\res\anim\
     文件         835  2009-04-15 11:22  SwipeSample\res\anim\slide_left_in.xml
     文件         836  2009-04-15 11:22  SwipeSample\res\anim\slide_left_out.xml
     文件         836  2009-04-15 11:28  SwipeSample\res\anim\slide_right_in.xml
     文件         835  2009-04-15 11:28  SwipeSample\res\anim\slide_right_out.xml
     目录           0  2009-04-16 21:37  SwipeSample\res\drawable\
     文件        3180  2009-04-16 21:37  SwipeSample\res\drawable\icon.png
............此处省略11个文件信息

评论

共有 条评论