我为图像制作了一个简单的动画,并在图像上设置了事件 OnClick 来祝酒.问题是我让图像开始在 onCreate 上制作动画,并设置要单击的图像并触发 toast 但问题是图像不可点击,但如果我按下原始位置图像,吐司开始(onClick没有随着动画移动)
I've made a simple animation for an image and I set the event OnClick on the image to make a toast. The problem is that I made the image started doing the animation on the onCreate and I made set the image to be clicked and fire the toast but the problem is that the image isn't clickable, but if I press on the original position of the image, the toast is started (the onClick is not moving with the animation)
感谢您的帮助
这是anim文件夹中的动画代码(translate.xml)
this is the animation code in anim folder (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();
}
});
}
}
阅读Android动画系统的文档(docs link),特别是 View Animation 和 Property Animation 之间的区别.以下是来自 View Animation 文档的引用:
Have a read over the documentation for the Android animation system (docs link), specifically the difference between View Animation and Property Animation. Here is a quote from the View Animation doc:
注意:无论您的动画如何移动或调整大小,保存动画的 View 的边界都不会自动调整以适应它.
Note: Regardless of how your animation may move or resize, the bounds of the View that holds your animation will not automatically adjust to accommodate it.
本质上,当使用视图动画时,视图本身永远不会被翻译,只会翻译它被绘制的位置.对象保持在其原始坐标,这就是为什么您必须点击旧位置才能获得事件.这是 View Animation 的一个已知限制,也是在 Android 3.0+ 中引入 Property Animation 的原因之一
Essentially, when using View Animation, the view itself is never translated, only the location at which it is drawn. The object remains at its original coordinates, which is why you have to tap the old location to get an event. This is a known limitation of View Animation and is one of the reasons Property Animation was introduced in Android 3.0+
这篇关于图片正在播放动画,图片可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!