2

画像の簡単なアニメーションを作成し、画像に OnClick イベントを設定してトーストを作成しました。問題は、onCreateで画像のアニメーションを開始し、画像をクリックしてトーストを起動するように設定したことですが、問題は画像をクリックできないことですが、元の位置を押すと画像、トーストが開始されます(画像はアニメーションで動いていません)

あなたの助けをthx

これは、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>

これがアクティビティクラスです

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

        }
    });

}

}
4

3 に答える 3

2

アニメーション全体を通して、ビューは元の位置 (アニメーションが開始されたばかりの位置) にとどまります。別の場所に描かれているだけです。アニメーションが終了したら、アニメーション ビューを移動する必要があります。

リスナーをアニメーションに登録します。 http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

onAnimationEnd の実装で、アクティビティのレイアウトを変更して、アニメーションの最終的な状態/レイアウトに似るようにします。

コメントの後に更新します。

これを行う唯一の方法は、Java コードで独自のカスタム アニメーションを作成し、カスタム アニメーションの 'protected void applyTransformation(float interpolatedTime, Transformation t)' メソッドを実装することです。たとえば、私たちのアプリでは、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()」を使用してビュー (ImageView) の位置を変更し、layoutParams.topMargin と layoutParams.leftMargin を適切に変更します。

Android 2.x 以下について心配していない場合は、ObjectAnimator (3.0 以上) または ViewPropertyAnimator (3.1 以上) を使用することをお勧めします。

これが役立つかどうか教えてください。

于 2013-02-14T19:29:26.063 に答える
0

imageViewをアニメーション化する方法は、外観を移動するだけなので、実際にはimageViewはまだ古い位置にあります。このタイプのアニメーションでは、やりたいことを簡単に行う方法はありません。ObjectAnimatorsの使用を検討する必要があります...

于 2013-02-14T21:30:51.267 に答える
0

このようにしてみてください:

final Animation animTranslate1 = AnimationUtils.loadAnimation(this,R.anim.translate);
animTranslate1.setFillAfter(true);
image01.startAnimation(animTranslate1);

それが機能しない場合は、新しいプロパティ アニメーション フレームワークを使用する必要があります (これは、以前の重複した質問への回答で指摘されました)。

詳しくはこちらをご覧ください

于 2013-02-14T19:23:45.140 に答える