0
這可能是一個非常簡單的問題,我有一個動態添加的片段。該片段有一個按鈕,其中包含一個單擊事件,用於爲x軸上的視圖+50和y軸上的+50動畫。Android動畫 - 查看不刷新
動畫工作正常,但問題是當我點擊按鈕後動畫它不起作用,但點擊事件仍然在原來的位置。如果這沒有意義,我會發佈一個鏈接到YouTube視頻,顯示發生了什麼。我只是好奇,如果有什麼我需要刷新?
https://www.youtube.com/watch?v=TLaS6u5v4m0
這裏是我的代碼:
public class AnimationFragment extends Fragment {
public AnimationFragment() {
// Required empty public constructor
}
View view;
float curX, curY, newX, newY;
boolean animating = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
curX = 0; curY = 0;
view = inflater.inflate(R.layout.fragment_animation, container, false);
// get the button and add the onclick event
Button animateButton = (Button)view.findViewById(R.id.animateButton);
//
if (null != animateButton) {
animateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animateButtonClick(v);
}
});
}
return view;
}
public void animateButtonClick(View view) {
// wait for the animation to be complete
if (animating) return;
animating = true;
// get the new x and new y
newX = curX + 50.0f;
newY = curY + 50.0f;
// get the layout that will be animated
final RelativeLayout animationFragment = (RelativeLayout)getView().findViewById(R.id.animationFragment);
Animation slide = new TranslateAnimation(
Animation.ABSOLUTE,curX,
Animation.ABSOLUTE, newX,
Animation.ABSOLUTE, curY,
Animation.ABSOLUTE, newY);
// add other animation attributes
slide.setDuration(400);
slide.setFillAfter(true);
slide.setFillEnabled(true);
// start the animation
animationFragment.startAnimation(slide);
slide.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// nothing
}
@Override
public void onAnimationEnd(Animation animation) {
// make the view enable
animating = false;
// sets the new x and new y
curX = newX;
curY = newY;
}
@Override
public void onAnimationRepeat(Animation animation) {
// nothing
}
});
}
}
當我擺脫這些,除非我設置視圖animationFragments x和y的onAnimationEnd事件動畫是行不通的事件。這是有道理的,因爲它不是視圖的動畫,而是視圖的位圖。但奇怪的部分,它仍然看起來像垃圾。 'animationFragment.setX(下一頁末); animationFragment.setY(newY); ' https://www.youtube.com/watch?v=TI9zWDiA_zA – 2015-02-23 21:53:08
你已經標記了問題已解決,但你之前的評論說它不是 - 我很困惑。看看TranslateAnimation的參數,好像你正在使用它錯了:http://developer.android.com/reference/android/view/animation/TranslateAnimation.html – Quanturium 2015-02-23 22:01:24