Quv*_*Quv 5 android location translate-animation
我知道多种方法来获取View的位置值.
getLocationOnScreen()
getLocationInWindow()
getLeft()
Run Code Online (Sandbox Code Playgroud)
但是,它们都没有实际返回由startAnimation()方法移动的View I的当前位置,而只返回原始位置.
所以,现在让我们在每个Click上创建一个向右移动10个像素的视图(我省略了布局,因为您可以在主XML中放置任何视图并将其赋予onClickListener).
public class AndroidTestActivity extends Activity implements OnClickListener {
LinearLayout testView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testView = (LinearLayout) this.findViewById(R.id.test);
testView.setOnClickListener(this);
}
public void onClick(View v) {
int[] pLoS = new int[2];
testView.getLocationOnScreen(pLoS);
TranslateAnimation move = new TranslateAnimation(pLoS[0], pLoS[0] + 10, 0f, 0f);
move.setFillAfter(true);
move.setFillEnabled(true);
testView.startAnimation(move);
}
}
Run Code Online (Sandbox Code Playgroud)
如你所见,这不能按我的意思工作,因为getLocationOnScreen()总是返回相同的值(在我的情况下为0),并且不反映我在TranslateAnimation中使用的值...
任何的想法?
假设您使用的是 Android < 3.0,那么您的问题可能与我在这里提出的问题类似。基本上,动画与视图本身是分开的,即 Android 会为视图的副本进行动画处理。这就是为什么getLocationOnScreen()总是返回 0。移动(动画)的不是视图,而是移动(动画)的副本。如果您看到我的问题的答案,则此问题已在更高版本的 Android 中得到解决。
| 归档时间: |
|
| 查看次数: |
5475 次 |
| 最近记录: |