在具有相同密度的不同Android设备上,不同的猛击(滑动)速度 [英] Different fling (swipe) velocity on different Android devices with same density
问题描述
我正在编写自己的图像查看器,使用户能够左右滑动以查看下一张上一张图像。我想根据掠过的速度来动画化图像变化。
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.
要检测掠过的手势及其速度,我遵循了这个基本手势检测,并按照建议的答案进行了提示:
To detect a fling gesture and its velocity, I followed this basic gesture detection and did as the accepted answer suggested:
public class SelectFilterActivity extends Activity implements OnClickListener
{
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... */
// Gesture detection
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
}
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) {
Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
问题是我变得与众不同密度相同的不同Android设备的速度值(velocityX和velocityY)。基本上,这意味着在一台设备上的滑动感觉很慢且不敏感,而其他设备感觉太灵敏。
The problem is that I get different velocity values (velocityX & velocityY) for different Android devices with same density. Basically it means that in one device the swipe feels slow and not-sensitive, and other devices feels too sensitive.
我认为这可能与默认设置有关设备上的最大速度,可以使用-
I thought it might have something to do with the "default" maximum velocity on the device, that can be fetched using -
ViewConfiguration.get(mContext).getScaledMaximumFlingVelocity()
在相同密度的不同设备上的结果与预期的不同:
The results on different devices with same density aren't the same as expected:
Samsung S I 480x800 density 1.5 Android 2.1
getScaledMinimumFlingVelocity(): 75
getScaledMaximumFlingVelocity(): 3600
ViewConfiguration.MAXIMUM_FLING_VELOCITY = 4000
HTC Desire 480x800 density 1.5 Android 2.3.3
getScaledMinimumFlingVelocity(): 75
getScaledMaximumFlingVelocity(): 6000
ViewConfiguration.MAXIMUM_FLING_VELOCITY = 4000
Samsung S III mini 480x800 density 1.5 Android 4.1
getScaledMinimumFlingVelocity(): 75
getScaledMaximumFlingVelocity(): 12000
ViewConfiguration.MAXIMUM_FLING_VELOCITY = 8000
您可以还看到 ViewConfiguration 对于Android 4.0以下及更高版本的 MAXIMUM_FLING_VELOCITY
具有2个不同的值。
You can also see that ViewConfiguration has 2 different values for MAXIMUM_FLING_VELOCITY
in Android below 4.0 and above.
为何具有相同密度和几乎相同api水平的不同设备没有相同的最大速度?
当用户做出滑动手势时,如何在不同的设备上获得相同的体验?
我可以使用最大速度数据在所有Android设备上提供一致的体验吗?
How come different devices with same density and almost same api level don't have the same max velocity? How can I get the same experience on different devices when a user makes a swipe gesture? Can I use the max velocity data to make a uniform experience across all Android devices?
推荐答案
我也有类似经历问题。可以直接将速度归一化为0到1之间的值,而不必直接使用 ViewConfiguration
的最大和最小摆动速度。
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
换句话说, velocityPercentX
为您提供猛击的威力,以百分比表示,而 normalizedVelocityX
是应用逻辑的速度(例如,与设备无关的像素的图像宽度)。
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).
这篇关于在具有相同密度的不同Android设备上,不同的猛击(滑动)速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!