Android onTouch动画onTouch停止动画回落

时间:2013-01-30 19:11:28

标签: java android animation android-animation

我试图让一个imageView在触摸按钮时向上移动,当按钮被释放时,我希望它再次向下移动。我开始编写一些示例代码,至少让它向上移动,但我已经在onClick中获得了一个空指针。有什么想法吗?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class Test extends Activity {
    ImageView img_left;
    ImageView img_right;
    Button left;
    Button right;
    TranslateAnimation moveup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        moveup = new TranslateAnimation(0, 0, 0, 300);
        moveup.setDuration(3000);
        moveup.setFillAfter(true);
        left = (Button) findViewById(R.id.left);
        right = (Button) findViewById(R.id.right);

        left.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                img_left.startAnimation(moveup);
            }

        });



    }

1 个答案:

答案 0 :(得分:0)

你没有初始化你的ImageView对象,就程序而言,它们只是空引用。

您需要执行以下操作:

//initialize the ImageView
img_left = (ImageView)findViewById(R.id.ID of the image view);
//same thing for img_right