图像正在播放动画,图像可以点击

时间:2013-02-07 21:09:40

标签: java android animation onclick click

我为图像制作了一个简单的动画,然后在图像上设置事件OnClick以进行祝酒。问题是我让图像开始在onCreate上做动画,我设置了要点击的图像并触发吐司,但问题是图像不可点击,但如果我按下原始位置图像,吐司开始(onClick不随动画一起移动)

thx求助

这是动画文件夹(translate.xml)中的动画代码

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/linear_interpolator" >
    <translate
        android:duration="1500"
        android:fromXDelta="-100%p"
        android:repeatCount="0"
        android:repeatMode="reverse"
        android:toXDelta="0" />

    </set>

这是活动类

package com.example.animatest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

private ImageView image01;

private long aefe;
private ImageView image1;
private ImageView image2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image01 = (ImageView) findViewById(R.id.imageView1);

    final Animation animTranslate1 = AnimationUtils.loadAnimation(this,
            R.anim.translate);

    image01.startAnimation(animTranslate1);

    image01.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

            Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT)
                    .show();

        }
    });

}

}

1 个答案:

答案 0 :(得分:3)

阅读Android动画系统(docs link)的文档,特别是View Animation和Property Animation之间的区别。以下是View Animation doc的引用:

  

注意:无论动画如何移动或调整大小,保持动画的视图边界都不会自动调整以适应它。

基本上,在使用视图动画时,视图本身永远不会被翻译,只会翻译它的位置。对象保持其原始坐标,这就是您必须点击旧位置才能获得事件的原因。这是View Animation的一个已知限制,也是在Android 3.0 +中引入Property Animation的原因之一