android - 如何在android中使动画可点击

标签 android animation imageview

我为图像制作了一个简单的动画,并在图像上设置了 OnClick 事件来进行 toast 。问题是我让图像开始在 onCreate 上执行动画,并且我设置了要单击的图像并触发 toast ,但问题是图像不可单击,但如果我按图像的原始位置图像, toast 开始(图像不随动画移动)

感谢您的帮助

这是 anim 文件夹中的动画代码 (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:fromXDelta="-80%p"
        android:toXDelta="80%p"
        android:duration="20000"
        android:repeatCount="100"
        android:repeatMode="restart"



        />


</set>

这是 Activity 类

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();

        }
    });

}

}

最佳答案

在整个动画过程中,您的 View 保持在旧位置(动画刚开始时的位置)。它只是在另一个地方绘制的。您必须在动画结束后移动动画 View :

注册动画的监听器。 http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

在 onAnimationEnd 实现中,修改 Activity 的布局,使其类似于动画的最终状态/布局。

评论后更新:

我认为执行此操作的唯一方法是在 Java 代码中创建您自己的自定义动画并实现自定义动画的“protected void applyTransformation(float interpolatedTime, Transformation t)”方法。例如,在我们的应用中,我们有一个动画,它实际上移动 View ,而不仅仅是在不同位置绘制它。例如。下面是增加或减少 View 实际高度的动画示例:

public class ViewHeightAnimation extends Animation {
    private final View  view;
    private final float diffHeight;
    private final int   startHeight;

    public ViewHeightAnimation(View view, float diffHeight, int startHeight) {
        this.view = view;
        this.diffHeight = diffHeight;
        this.startHeight = startHeight;

        setDuration(200);
        setInterpolator(new AccelerateDecelerateInterpolator());
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        android.view.ViewGroup.MarginLayoutParams layoutParams = (android.view.ViewGroup.MarginLayoutParams)view.getLayoutParams();
        layoutParams.height = Math.round(startHeight + (diffHeight * interpolatedTime));
        view.setLayoutParams(layoutParams);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

您的动画会有所不同,但也会使用“getLayoutParams()”和“setLayoutParams()”来修改 View (ImageView)的位置并相应地更改layoutParams.topMargin和layoutParams.leftMargin。

如果您不关心 Android 2.x 或更低版本,使用 ObjectAnimator(3.0 或更高版本)或 ViewPropertyAnimator(3.1 或更高版本)是更好的解决方案,正如前面的其他答案中提到的。

请告诉我这是否对您有帮助。

关于android - 如何在android中使动画可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14881575/

相关文章:

java - Android:围绕中心旋转图像?

Android ImageGetter 图像重叠文本

android - 应用程序仅在某些手机上崩溃(连接到互联网时?)

javascript - 如何在循环中为 jQuery 函数的变量设置动画?

javascript - 简单的 Canvas 动画: 50x50 image moving across

android - 在图像后面设置背景

android - 在每个 onClick 调用上运行相同的帧动画

android - 编程式 SD 卡安装/卸载

键盘启动时Android ScrollView不滚动

android - 以编程方式设置 Imageview 的高度和宽度