如何让Android中的动画可点击动画、Android

2023-09-06 11:03:01 作者:伱丶哥的独有

我做了一个图像一个简单的动画,我设置事件的OnClick在图像上敬酒。问题是,我做了对原始图像开始做对的onCreate动画和我做了设置图像被点击和火灾致祝酒辞,但问题是,图像无法点击,但如果我preSS图像的位置,面包开始(图像不与动画移动)

THX对您有所帮助。

这是在动画文件夹中的动画code(translate.xml)

 <?XML版本=1.0编码=UTF-8&GT?;<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:插值=@机器人:动画/ linear_interpolator>    <翻译        机器人:fromXDelta = -  80%P        机器人:toXDelta =80%P        机器人:时间=20000        安卓的repeatCount =100        机器人:REPEATMODE =重启        />< /集> 

这是Activity类

 包com.example.animatest;进口android.app.Activity;进口android.os.Bundle;进口android.util.Log;进口android.view.View;进口android.view.View.OnClickListener;进口android.view.animation.Animation;进口android.view.animation.AnimationUtils;进口android.widget.ImageView;进口android.widget.Toast;公共类MainActivity延伸活动{私人ImageView的image01;私人长期aefe;私人ImageView的image1的;私人ImageView的IMAGE2;@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    image01 =(ImageView的)findViewById(R.id.imageView1);    最终的动画animTranslate1 = AnimationUtils.loadAnimation(这一点,            R.anim.translate);    image01.startAnimation(animTranslate1);    image01.setOnClickListener(新OnClickListener(){        @覆盖        公共无效的onClick(查看视图){            Toast.makeText(MainActivity.this,你好,Toast.LENGTH_SHORT)                    。显示();        }    });}} 
Android实现带动画效果的可点击展开TextView

解决方案

在整个动画,你的看法还停留在原来的位置(当动画刚开始的位置)。这仅仅是画在另一个地方。你必须将你的动画视图动画结束之后:

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

在您onAnimationEnd实施,修改活动的布局,以便它类似于动画的最终状态/布局。

您的评论后更新:

我看到这样做的唯一方法是通过创建Java中的code自己的自定义动画和实现您的自定义动画的保护无效applyTransformation(浮动interpolatedTime,变换T)的方法。例如,在我们的应用程序,我们有一个动画实际的移动视图,而不是围绕着刚才的绘图它在不同的位置。例如。下面是增加或减少视图的实际高度动画的一个例子:

 公共类ViewHeightAnimation扩展动画{    私人最终查看视图。    私人最终浮动diffHeight;    私人最终诠释startHeight;    公共ViewHeightAnimation(查看视图,浮diffHeight,诠释startHeight){        this.view =视图。        this.diffHeight = diffHeight;        this.startHeight = startHeight;        setDuration(200);        setInterpolator(新AccelerateDecelerateInterpolator());    }    @覆盖    保护无效applyTransformation(浮动interpolatedTime,变换T){        android.view.ViewGroup.MarginLayoutParams的LayoutParams =(android.view.ViewGroup.MarginLayoutParams)view.getLayoutParams();        layoutParams.height = Math.round(startHeight +(d​​iffHeight * interpolatedTime));        view.setLayoutParams(的LayoutParams);    }    @覆盖    公共布尔willChangeBounds(){        返回true;    }} 

您动画会有所不同,但将使用'getLayoutParams()'和'setLayoutParams(),以及修改视图的(的ImageView)的位置,改变layoutParams.topMargin和layoutParams.leftMargin适当。

如果你不关心的Andr​​oid 2.x或更低,使用ObjectAnimator(3.0或更高版本)或ViewPropertyAnimator(3.1或更高版本)是一个更好的解决方案,如在对方的回答前面提到的。

让我知道如果这可以帮助你。

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 image is not moving with the animation)

thx for your help

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:fromXDelta="-80%p"
        android:toXDelta="80%p"
        android:duration="20000"
        android:repeatCount="100"
        android:repeatMode="restart"



        />


</set>

and this is the Activity Class

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

        }
    });

}

}

解决方案

During the entire animation, your view remains at the old location (location when the animation just started). It is just drawn in another spot. You'd have to move your animated view after your animation ends:

Register a listener to your animation. http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

In your onAnimationEnd implementation, modify your Activity's layout so that it resembles the final state/layout of your animation.

Update after your comment:

The only way I see of doing this is by creating your own custom Animation in Java code and implementing your custom Animation's 'protected void applyTransformation(float interpolatedTime, Transformation t)' method. For example, in our app we have an animation that actually moves a View around instead of just drawing it at a different location. E.g. below is an example of an Animation that increases or decreases the actual height of a 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;
    }
}

Your Animation would be different, but would be using the 'getLayoutParams()' and 'setLayoutParams()' as well to modify the View's (ImageView's) position and change layoutParams.topMargin and layoutParams.leftMargin appropriately.

If you are not concerned about Android 2.x or lower, using the ObjectAnimator (3.0 or higher) or ViewPropertyAnimator (3.1 or higher) is a better solution, as was mentioned in other answer earlier.

Let me know if this helps you.