平滑滚动在Android中 [英] Smooth scrolling in Android

查看:128
本文介绍了平滑滚动在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个在所谓的浮动图像市场上的应用程序。这个程序有最平滑滚动的逻辑之一。基本上,应用程序有一个空白的画布,覆盖整个屏幕,然后有在空白的画布的顶部显示一些图像。用户可以刷卡和应用程序移动的图像中滑动的方向。再加上它确实动力学滚动了。再加上有没有滚动条,因此似乎像开发者已经创建一个自定义视图实现所有平滑滚动的逻辑。

There is an app in the market place called Floating images. This app has one of the smoothest scrolling logic. Basically the app has a blank canvas covering whole screen and then there are some images displayed on top of the blank canvas. User can swipe and the app moves the image in the direction of swipe. Plus it does kinetic scrolling too. Plus there are no scrollbars so it seems like the developer has created a custom view implementing all smooth scrolling logic.

这将是真棒,如果我能得到它的源代码..但任何人有如何实现这种功能的任何伪code或逻辑。任何线索,网站链接将是有益的。

it would be awesome if i could get the source of it.. but anyone has any pseudo code or logic on how to implement this kind of feature. Any leads , site links would be helpful.

推荐答案

我有OpenGL的,也没有加速,而是刷卡(所谓的一扔的在Android的API)没有经验并不难实现。你作出这样一个自定义的查看时,需要第一件事,正在实施 GestureDetector 并调用其的onTouchEvent()在视图中的的onTouchEvent()

I have no experience with OpenGL nor accelerometer, but swipe (called fling in Android's API) is not hard to achieve. First thing you need when making such a custom View, is implementing a GestureDetector and call its onTouchEvent() in your view's onTouchEvent()

GestureDetector mGD = new GestureDetector(getContext(),
                                        new SimpleOnGestureListener() {

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
        // beware, it can scroll to infinity
        scrollBy((int)distanceX, (int)distanceY);
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float vX, float vY) {
        mScroller.fling(getScrollX(), getScrollY(),
                -(int)vX, -(int)vY, 0, (int)mMaxScrollX, 0, (int)mMaxScrollY);
        invalidate(); // don't remember if it's needed
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        if(!mScroller.isFinished() ) { // is flinging
            mScroller.forceFinished(true); // to stop flinging on touch
        }
        return true; // else won't work
    }
});

@Override
public boolean onTouchEvent(MotionEvent event) {
    return mGD.onTouchEvent(event);
}

OnGestureListener.onScroll()直接调用 View.scrollBy() onFling()方法,你需要一个滚轮

While OnGestureListener.onScroll() calls directly View.scrollBy(), for the onFling() method you'll need a Scroller.

滚轮的是,作为基准说,封装了滚动一个简单的对象。它可用于连续滚动或反应以甩。 Scroller.fling()开始内部本身一扔滚动的模拟,并通过观察它,你可以复制它的光滑与连续重绘动画:

Scroller is a simple object that, as reference says, encapsulates scrolling. It can be used for continuous scrolling or to react to flings. Scroller.fling() begin a "simulation" of fling scroll inside itself, and by watching it you can copy its smoothness with a continuous redrawing animation:

@Override
protected void onDraw(Canvas canvas) {
    // ....your drawings....

    // scrollTo invalidates, so until animation won't finish it will be called
    // (used after a Scroller.fling() )
    if(mScroller.computeScrollOffset()) {
        scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
    }
}

这是,直到动画运行,计算我们到达了点,然后滚动出现。

that is, until animation is running, calculate the point we reached and scroll there.

随着最后一个音符:记得要返回 OnGestureListener.onDown(),即使你不'吨要下来做任何事情,否则将无法正常工作。

As a last note: remember to return true in your OnGestureListener.onDown(), even if you don't want to do anything on down, or it won't work.

和小心,因为滚轮中的Andr​​oid 2.2有一个bug为其一扔动画实际上并不会结束,即使它达到你作为参数传递的限制(尚未计算补偿方面他们,所以它不会实际移动)。

And be careful, because Scroller in Android 2.2 has a bug for which the fling animation will not actually end even if it reaches the limits you passed as arguments (yet computed offset respects them, so it won't actually move).

这篇关于平滑滚动在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆