Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

java - image is playing an animation and the image is clickable

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

        }
    });

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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+


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...