web-dev-qa-db-ja.com

ObjectAnimatorでドローアブルを回転させる方法は?

ドローアブルのアルファ化は次のようにうまく機能します。

if(mAlphaAnimation == null){
        mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION);
        mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power);
        mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE);
        mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
        mAlphaAnimation.addUpdateListener(this);
 }

しかし、以下のようにドローアブルを回転させたい場合、それはうまくいきません。

private void createRotateAnim(float fromDegress,float toDegress,int duration){
    if(mRotateAnimation == null){
        mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration);
        mRotateAnimation.setStartDelay(100);
        mRotateAnimation.setInterpolator(new AccelerateInterpolator());
        mRotateAnimation.addUpdateListener(this);
    }
}

誰でも私がこの問題を修正するのを助けることができます、またはこれらは回転描画可能なアニメーションを作成する他の方法です。

英語が下手でごめんなさい。

19
marine8888

画像に適用されたこの単純な回転アニメーションを試してください。

 ImageView imageview = (ImageView)findViewById(R.id.myimage);
 RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,    
 0.5f,  Animation.RELATIVE_TO_SELF, 0.5f);
  rotate.setDuration(500);
 imageview.startAnimation(rotate);

この答えは質問のためだけです。クリック可能な領域がViewの現在の位置と異なるのは正しいことです。クリック可能な領域を正しくするためにこの質問を確認してください。 ボタンはTranslateAnimation後にクリックできません

18
Rahul

代わりにObjectAnimatorを試してください。

ImageView imageview = (ImageView)findViewById(R.id.image);

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
                    "rotation", 0f, 360f);
            imageViewObjectAnimator.setDuration(1000); // miliseconds
            imageViewObjectAnimator.start();

[〜#〜] edit [〜#〜]この質問には注意が必要なので、他のトランジションの代わりにObjectAnimatorを使用する理由を説明しましょうアニメーター

ObjectAnimatorを使用することの重要な点は、別のアニメーションメソッド(トランジションアニメーションや他のいくつかのアニメーターなど)を使用する場合、アイテムの表示領域とクリック可能領域の両方が移動することです。 Buttonは、画面の左下から左上に移動します。表示領域のみが移動し、Button自体は移動しません。この場合、クリック可能な領域は前の位置にあります。クリック可能な領域は、ボタンを移動した左上ではなく、左下のままです。

ObjectAnimatorで同じことを行うと、可視領域とクリック可能領域の両方が目的の場所に移動します。

59
Naskov