0

我在我的 XML 布局中定义了一个按钮,当单击它时,它会调用一个函数,该函数应该将 ImageView 人 100 个像素向右平移,它将图像向右移动,但是当我第二次单击该按钮时它会重置图像到它的起始位置,并在相同的路径上重复相同的动画。(我的 xml 中有 android:fillAfter="true")我尝试在 onAnimationEnd 中添加以下行 -

 @Override
            public void onAnimationEnd(Animation arg0) {

                man.layout(xCurrentPos + 100, yCurrentPos, xCurrentPos+ (man.getWidth()) + 100, yCurrentPos + man.getHeight());

            }

但是,当动画完成时,ImageView 在 ImageView 停止其动画的位置左侧出现了一点故障(它向后跳了一下),我尝试使用 xCurrentPos + 100 参数示例将其更改为 xCurrentPos + 70 以使其停止故障但什么也没有工作,我也试过把

man.layout(man.getLeft(), man.getTop(), man.getRight(), man.getBottom());

在 onAnimationEnd 但是它只是将图像重置为其原始位置

这是我的代码,

package com.junglejackapps.theman;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;

import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    Button bup, bdown, bleft, bright;
    int xCurrentPos;
    int yCurrentPos;

        ImageView man;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        bup = (Button) findViewById(R.layout.activity_main);
        bdown = (Button) findViewById(R.layout.activity_main);
        bleft = (Button) findViewById(R.layout.activity_main);
        bright = (Button) findViewById(R.layout.activity_main);
        man = (ImageView) findViewById(R.id.man); 

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick_function_up (View v) {

    }

    public void onClick_function_right (View v) {

        xCurrentPos = man.getLeft(); 
        yCurrentPos = man.getTop(); 

        Animation anim= new TranslateAnimation(xCurrentPos, xCurrentPos+100, yCurrentPos, yCurrentPos); 
        anim.setDuration(2500); 
        anim.setFillAfter(true); 
        anim.setFillEnabled(true); 
        anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {}

            @Override
            public void onAnimationRepeat(Animation arg0) {}

            @Override
            public void onAnimationEnd(Animation arg0) {

                man.layout(xCurrentPos + 100, yCurrentPos, xCurrentPos+ (man.getWidth()) + 100, yCurrentPos + man.getHeight());

            }
        });
        man.startAnimation(anim);

    }

    public void onClick_function_down (View v) {

    }

    public void onClick_function_left (View v) {

    }


}

和我的 XML 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:fillAfter="true">

    <ImageView
        android:id="@+id/man"
        android:layout_width="70dp"
        android:layout_height="120dp"
        android:layout_marginLeft="22dp"
        android:contentDescription="@string/arrow"
        android:src="@drawable/man"

        />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/button2"
        android:layout_marginBottom="20dp"
        android:text="" 
        android:onClick="onClick_function_up" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="19dp"
        android:text="" 
        android:onClick="onClick_function_up_down"/>

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/button2"
        android:layout_marginRight="33dp"
        android:text="" 
        android:onClick="onClick_function_right"
        />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button2"
        android:layout_marginLeft="15dp"
        android:text=""
        android:onClick="onClick_function_left" />

</RelativeLayout>

任何帮助或建议将不胜感激,谢谢。

4

1 回答 1

0

尝试在动画中使用增量,并在最后设置边距。

这是一个例子:

TranslateAnimation anim = new TranslateAnimation(0, 0, amountToMoveRight, amountToMoveDown);
        anim.setDuration(2500);

        anim.setAnimationListener(new TranslateAnimation.AnimationListener() 
        {

            @Override
            public void onAnimationStart(Animation animation) { }

            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) 
            {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
                params.topMargin += amountToMoveDown;
                params.leftMargin += amountToMoveRight;
                view.setLayoutParams(params);
            }
        });

        view.startAnimation(anim);

确保在这种情况下fillAfter设置为。false

希望这可以帮助 :)

于 2013-09-26T04:20:57.090 回答