我想通过获取它的坐标然后一个一个地增加或减少它们来为按钮设置动画,以便按钮可以向左然后向右。
问问题
10244 次
4 回答
9
使用 TranslateAnimation:
TranslateAnimation animation = new TranslateAnimation(start_x, start_y, end_x, end_y);
animation.setDuration(1000); // duartion in ms
animation.setFillAfter(false);
button.startAnimation(animation);
我不确定如何获得它的位置,button.getTop() 和 button.getLeft() 可以工作......
于 2012-07-06T19:05:29.557 回答
4
不确定这是否对您有帮助,但我遇到了同样的问题,我可以使用这些方法做到这一点,setTranslationX(float) setTranslationY(float)
你可以像这样使用它
Button button = (button) findViewById(your id);
button.setTranslationX(a float value);
这是提供更多信息的 android 文档http://developer.android.com/reference/android/view/View.html#attr_android:translationX
于 2014-06-17T10:54:07.553 回答
1
当按钮单击的图像从左向右移动时,您也可以将此代码用于片段。
将此代码插入 onCreateView 片段或直接使用按钮侦听器。
View view = inflater.inflate(R.layout.fragment_move,container,false);
mBtnMove = view.findViewById(R.id.btn_move);
mImageMove = view.findViewById(R.id.imv_move);
mBtnMove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float start_x_axis = 0; // initialize the axis value start from and end
float start_y_axis = 1000;
float end_x_axis = 0;
float end_y_axis = 0;
TranslateAnimation animation = new TranslateAnimation(start_x_axis, start_y_axis, end_x_axis, end_y_axis);
animation.setDuration(2500); // Duration of image to move from left to right
mImageMove.startAnimation(animation);
}
});
return view;
于 2019-03-25T14:25:27.050 回答
1
Solution for those who are looking for left to right animation)
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(1500); // animation duration
animation.setRepeatCount(1); // animation repeat count
animation.setFillAfter(false);
your_view .startAnimation(animation);//your_view for mine is imageView
Solution for those who are looking for repeated animation(for eg. left to right and right to left)
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(1500); // animation duration
animation.setRepeatCount(4); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left)
animation.setFillAfter(true);
your_view .startAnimation(animation);//your_view for mine is imageView
于 2016-12-15T07:48:38.560 回答