本文介绍了机器人:如何创建不同的"屏幕"并采用触摸拖动它们之间的资产净值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创造一个同样的导航,我有在主屏幕?也就是说,我想不同的画面,我可以使用触摸和拖​​拽之间导航?我还没有看到任何关于它,所以现在是时候要问:)

Can i create the same kind of navigation that I have on the main screens?That is, I want different screens that I can navigate between using the touch-and-drag?I haven't seen anything about it, so it was time to ask :)

呵呵,林谈论的Andr​​oid应用程序:)

Oh, Im talking about apps for android :)

推荐答案

没有什么内置的,但你可以用ViewFlipper,GestureDetector和动画为捏造事实(你不会用得到主屏幕的触感阻力此方法):

There is nothing built-in, but you can use ViewFlipper, GestureDetector and Animation to "fake it" (you wont get the tactile drag of the home screen using this method):

public class SwipeExample extends Activity {

    private static final int LEFT = 0;
    private static final int RIGHT = 1;

    ViewFlipper flipper;
    GestureDetector gestureDetector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);

    	flipper = (ViewFlipper) findViewById(R.id.flipper);
    	gestureDetector = new GestureDetector(new MyGestureDetector());

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	if (gestureDetector.onTouchEvent(event))
    		return true;
    	else
    		return false;
    }

    private Animation animateInFrom(int fromDirection) {

    	Animation inFrom = null;

    	switch (fromDirection) {
    	case LEFT:
    		inFrom = 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);
    		break;
    	case RIGHT:
    		inFrom = 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);
    		break;
    	}

    	inFrom.setDuration(250);
    	inFrom.setInterpolator(new AccelerateInterpolator());
    	return inFrom;
    }

    private Animation animateOutTo(int toDirection) {

    	Animation outTo = null;

    	switch (toDirection) {
    	case LEFT:
    		outTo = 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);
    		break;
    	case RIGHT:
    		outTo = 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);
    		break;
    	}

    	outTo.setDuration(250);
    	outTo.setInterpolator(new AccelerateInterpolator());
    	return outTo;
    }

    class MyGestureDetector extends SimpleOnGestureListener {

    	// from:
    	// http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

    	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;

    	@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;
    			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
    					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    				// right to left swipe
    				flipper.setInAnimation(animateInFrom(RIGHT));
    				flipper.setOutAnimation(animateOutTo(LEFT));
    				flipper.showNext();
    			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
    					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    				// left to right swipe
    				flipper.setInAnimation(animateInFrom(LEFT));
    				flipper.setOutAnimation(animateOutTo(RIGHT));
    				flipper.showPrevious();
    			}
    		} catch (Exception e) {}
    		return false;
    	}
    }

}

这篇关于机器人:如何创建不同的"屏幕"并采用触摸拖动它们之间的资产净值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:55
查看更多