問題描述
我為圖像制作了一個簡單的動畫,并在圖像上設置了事件 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模板網!