图像播放动画,图像点击 [英] image is playing an animation and the image is clickable

查看:109
本文介绍了图像播放动画,图像点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

THX对您有所帮助。

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

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

这是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系统的动画文件在读取(的文档链接),特别是观看动画和属性动画的区别。下面是从观看动画DOC报价:


  

请注意:无论你如何动画可能移动或调整,保存你的动画不会自动调整视图的范围,以适应它。


从本质上讲,使用观看动画时,视图本身永远不会翻译,仅在其绘制的位置。对象仍保持其原始坐标,这就是为什么你必须挖掘旧位置得到一个事件。这是观看动画的一个已知的限制,是属性动画是在Android 3.0的推出的原因之一+

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)

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:duration="1500"
        android:fromXDelta="-100%p"
        android:repeatCount="0"
        android:repeatMode="reverse"
        android:toXDelta="0" />

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

        }
    });

}

}

解决方案

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:

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.

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+

这篇关于图像播放动画,图像点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆