Different fling (swipe) velocity on different Android devices with same density

后端 未结 3 1392
情话喂你
情话喂你 2021-02-01 18:32

I\'m writing my own image viewer that enables users to swipe left\\right to see the next\\previous image. I want to animate the image change according to the fling velocity.

3条回答
  •  抹茶落季
    2021-02-01 19:10

    I had a similar problem. Instead of working directly with the max and min fling velocities from ViewConfiguration, you can normalize the velocity to a value between 0 and 1.

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float maxFlingVelocity    = ViewConfiguration.get(mContext).getScaledMaximumFlingVelocity();
        float velocityPercentX    = velocityX / maxFlingVelocity;          // the percent is a value in the range of (0, 1]
        float normalizedVelocityX = velocityPercentX * PIXELS_PER_SECOND;  // where PIXELS_PER_SECOND is a device-independent measurement
    

    In other words, velocityPercentX gives you the "power" of the fling as a percent, and normalizedVelocityX is the velocity in terms of your application logic (such as an image width in device-independent pixels).

提交回复
热议问题