Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上相对移动图像?_Android_Image_Animation_Screen - Fatal编程技术网

如何在Android上相对移动图像?

如何在Android上相对移动图像?,android,image,animation,screen,Android,Image,Animation,Screen,我正在创建一个类似于速度等级的应用程序 如果你走得快,我的图像会上升,如果你走得慢,它会下降(不是到顶部,上部越快,下部越慢) 目前,在每次onLocationChange上,我都会计算推荐的速度并获得实际值。然后我把图像移动如下: imageView.setY((float) speedvalue); 我有两个问题: setY是以像素为单位的,所以它不适合每个屏幕 图像瞬间移动,我希望它像动画一样移动 如何解决这些问题?您可以设置视图移动的动画,如下所示: TranslateAnimatio

我正在创建一个类似于速度等级的应用程序

如果你走得快,我的图像会上升,如果你走得慢,它会下降(不是到顶部,上部越快,下部越慢)

目前,在每次onLocationChange上,我都会计算推荐的速度并获得实际值。然后我把图像移动如下:

imageView.setY((float) speedvalue);
我有两个问题: setY是以像素为单位的,所以它不适合每个屏幕

图像瞬间移动,我希望它像动画一样移动


如何解决这些问题?

您可以设置视图移动的动画,如下所示:

TranslateAnimation animation = new TranslateAnimation(startXCoordinate, finishXCoordinate, startYCoordinate, finishYCoordinate);

animation.setDuration(1000);

view.startAnimation(animation);
其次,使用此代码将像素转换为dps,它将在不同屏幕上移动相同的数量:


intdistancetomove=(int)TypedValue.applyDimension(1,howManyDPsToMove,context.getResources().getDisplayMetrics())

第一件事是可行的,但第二件事对我不起作用,它将760 dp的分辨率转换为2280像素,在屏幕更低的设备上。android:animateLayoutChanges=“true”是另一种不需要太多控制但更简单的动画制作方法。