web-dev-qa-db-ja.com

android

以下のようなanim.xmlファイルを作成して、AndroidでIOSアイコンシェーキングのようなimageviewをシェイクします。ただし、同じ結果は得られません。より良いアイデアはありますか?

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:duration="300"

    Android:fromDegrees="-2"
    Android:pivotX="50%"
    Android:pivotY="50%"
    Android:repeatCount="infinite"
    Android:toDegrees="2" />
69
Tyler I

Android:repeatMode="reverse"を設定してみてください。以下のアニメーションは、Galaxy Nexusを非常に合理的に模倣しています。明らかに、自分の好みに合わせてパラメーターを微調整できます。

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:duration="100"
    Android:fromDegrees="-5"
    Android:pivotX="50%"
    Android:pivotY="50%"
    Android:repeatCount="infinite"
    Android:repeatMode="reverse"
    Android:toDegrees="5" />
155
MH.

素敵なシェイクアニメーション;

res/anim /shake.xml

<set xmlns:Android="http://schemas.Android.com/apk/res/Android">

    <translate Android:duration="150"
        Android:fromXDelta="-10%"
        Android:repeatCount="5"
        Android:repeatMode="reverse"
        Android:toXDelta="10%"/>
</set>

使用方法

final Animation animShake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn_done = (Button) findViewById(R.id.btn_act_confirm_done); 
btn_done.startAnimation(animShake);

使用方法(簡易バージョン):

btn_done.startAnimation(AnimationUtils.loadAnimation(this,R.anim.shake));
51
Sam

これを試すことができます:

shake.xml

<translate xmlns:Android="http://schemas.Android.com/apk/res/Android" 
           Android:fromXDelta="0" 
           Android:toXDelta="10" 
           Android:duration="1000" 
           Android:interpolator="@anim/cycle_7" />

cycle_7.xml

<cycleInterpolator xmlns:Android="http://schemas.Android.com/apk/res/Android" 
                   Android:cycles="7" />
44
adneal

これを使用してみてください:

<set xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <rotate
        Android:duration="70"
        Android:fromDegrees="-5"
        Android:pivotX="50%"
        Android:pivotY="50%"
        Android:repeatCount="5"
        Android:repeatMode="reverse"
        Android:interpolator="@Android:anim/linear_interpolator"
        Android:toDegrees="5" />
    <translate
        Android:fromXDelta="-10"
        Android:toXDelta="10"
        Android:repeatCount="5"
        Android:repeatMode="reverse"
        Android:interpolator="@Android:anim/linear_interpolator"
        Android:duration="70" />
</set>
28
Simon Heinen

Android)にシェイク効果を作成し、GitHubに投稿しました。それがうまく機能するかどうかを確認してください。

https://github.com/teoinke/ShakeAnimation

関連コード:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:interpolator="@Android:anim/overshoot_interpolator"
    Android:fillAfter="true">

    <translate
        Android:startOffset="100"
        Android:fromXDelta="0%p"
        Android:toXDelta="10%p"
        Android:duration="50" />

    <translate
        Android:startOffset="150"
        Android:fromXDelta="0%p"
        Android:toXDelta="-25%p"
        Android:duration="110" />


    <translate
        Android:startOffset="260"
        Android:fromXDelta="0%p"
        Android:toXDelta="25%p"
        Android:duration="120" />


    <translate
        Android:startOffset="380"
        Android:fromXDelta="0%p"
        Android:toXDelta="-20%p"
        Android:duration="130" />


    <translate
        Android:startOffset="510"
        Android:fromXDelta="0%p"
        Android:toXDelta="10%p"
        Android:duration="140" />

</set>
14
Teo Inke

このようなシェイク効果を作成するには

enter image description here

まず、アニメーションフォルダー内のシェイクアニメーションをshake.xmlとして定義します

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <rotate
        Android:duration="70"
        Android:fromDegrees="-5"
        Android:interpolator="@Android:anim/linear_interpolator"
        Android:pivotX="50%"
        Android:pivotY="50%"
        Android:repeatCount="5"
        Android:repeatMode="reverse"
        Android:toDegrees="5" />
    <translate
        Android:duration="70"
        Android:fromXDelta="-10"
        Android:interpolator="@Android:anim/linear_interpolator"
        Android:repeatCount="5"
        Android:repeatMode="reverse"
        Android:toXDelta="10" />
</set>

その後、コードで

if (TextUtils.isEmpty(phone.getText())
 || phone.getText().length() < 10)
    {
     //shake animation
    phone.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.shake));
     }
10
Hitesh Sahu

これは、iOSの「不正なPIN」シェーキングクローンとして非常にうまく機能します(完全ではありませんが)。

    final float FREQ = 3f;
    final float DECAY = 2f;
    // interpolator that goes 1 -> -1 -> 1 -> -1 in a sine wave pattern.
    TimeInterpolator decayingSineWave = new TimeInterpolator() {
                @Override
                public float getInterpolation(float input) {
                    double raw = Math.sin(FREQ * input * 2 * Math.PI);
                    return (float)(raw * Math.exp(-input * DECAY));
                }
            };

    shakeField.animate()
            .xBy(-100)
            .setInterpolator(decayingSineWave)
            .setDuration(500)
            .start();
10
lincolnq
/**
 *
 * @param view      view that will be animated
 * @param duration  for how long in ms will it shake
 * @param offset    start offset of the animation
 * @return          returns the same view with animation properties
 */
public static View makeMeShake(View view, int duration, int offset) {
    Animation anim = new TranslateAnimation(-offset,offset,0,0);
    anim.setDuration(duration);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(5);
    view.startAnimation(anim);
    return view;
}

使用:

TextView tv;
makeMeShake(tv,20,5);    // it will shake quite fast
4
Windless

IOSの揺れの非常に良い近似を作成しました(アイコンを長押ししてホーム画面からアプリを削除する場合)。乱数を生成する必要があるため、プログラム内でコード内に適用する必要があります。

int dur1 = 70 + (int)(Math.random() * 30);
int dur2 = 70 + (int)(Math.random() * 30);

// Create an animation instance
Animation an = new RotateAnimation(-3, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

// Set the animation's parameters
an.setDuration(dur1);               // duration in ms
an.setRepeatCount(-1);                // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE);
an.setFillAfter(true);               // keep rotation after animation


// Create an animation instance
Animation an2 = new TranslateAnimation(-TranslateAnimation.RELATIVE_TO_SELF,0.02f,
        TranslateAnimation.RELATIVE_TO_SELF,0.02f,
        -TranslateAnimation.RELATIVE_TO_SELF,0.02f,
        TranslateAnimation.RELATIVE_TO_SELF,0.02f);

// Set the animation's parameters
an2.setDuration(dur2);               // duration in ms
an2.setRepeatCount(-1);                // -1 = infinite repeated
an2.setRepeatMode(Animation.REVERSE);
an2.setFillAfter(true);               // keep rotation after animation

AnimationSet s = new AnimationSet(false);//false means don't share interpolators
s.addAnimation(an);
s.addAnimation(an2);

// Apply animation to image view
itemView.setAnimation(s);

このコードは、アダプターのgridview(getView)内に適用されるように設計されましたが、最後の行を次のように変更することにより、任意のビューに適用できます。

yourViewName.setAnimations(s);

2

Kotlinユーザーの場合:

最初にshake.xmlというアニメーションリソースファイルを作成します。 Android Studioのresフォルダーを右クリックし、[新規作成]をクリックします。Androidリソースファイル>ファイル名にshakeと入力します[リソースタイプ]ドロップダウンでAnimationを選択し、[OK]をクリックします。

shake.xml内に次を貼り付けます:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <translate Android:duration="200"
               Android:fromXDelta="-5%"
               Android:repeatCount="3"
               Android:repeatMode="reverse"
               Android:toXDelta="5%"/>
</set>

ビューで呼び出すだけです!

フラグメント内から:

myView.startAnimation(AnimationUtils.loadAnimation(view!!.context, R.anim.shake))

アクティビティ内から:

myView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.shake))

(注-myViewは、アニメーション化するビューに指定されたIDです)

アニメーションを微調整する場合は、shake.xmlの値を変更するだけです。

1
Joe

2時間以上頭を叩いて、景色を揺らしたり揺らしたりする方法を知りました。

残念ながら、受け入れられた回答は、フラグメントのonCreateView以外は機能しません。

OnClickメソッドとその中にある場合の例。以下のようなアニメーションは動作しません。

コードを確認してください。

    DoneStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
           register(view);

        }
    });

Registerメソッドには、以下のコードのようないくつかのチェックがあります

 private void register(View view) {
    String type = typedThings.getText.toString(); 
   String  km = Km_Now.getText().toString();

    if (serviceType == null) {
        animationServiceList = AnimationUtils.loadAnimation(getActivity(), R.anim.shake_wobble);
        silverServiceButton.setAnimation(animationServiceList);
        generalServiceButton.setAnimation(animationServiceList);
        platinumServiceButton.setAnimation(animationServiceList);
        animationServiceList.start();
    } else if (km == null) {
        animationEditText = AnimationUtils.loadAnimation(getActivity(), R.anim.shake_wobble);
        Km_Now.setAnimation(animationEditText);
        animationEditText.start();
    }

AnimationServiceList.start()の呼び出し;決して呼び出されません

解決策:ObjectAnimatorのようなPropertyAnimatorを使用します。

0
Tot

IOSウォブルアニメーションは、回転時にピボットxとyをランダムに変更しようとするほど単純ではありません。ただし、プログラムで値を変更する必要があります。アニメーションの翻訳を同時に使用することもできます

0
wooram jung

他の答えも正しいですが、これは、補間器を使用して前後の動きに滑らかな数値を生成するため、これよりも少しスムーズです

    public class WobblyView extends ImageView implements ValueAnimator.AnimatorUpdateListener {
    private final ValueAnimator va = ValueAnimator.ofInt(-10, 10);

    public WobblyView(Context context) {
        this(context, null);
    }

    public WobblyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WobblyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setAdjustViewBounds(true);
        setImageResource(R.drawable.ic_logo);
        va.setInterpolator(new AccelerateDecelerateInterpolator());
        va.setRepeatMode(ValueAnimator.REVERSE);
        va.setRepeatCount(ValueAnimator.INFINITE);
        va.setDuration(1000);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        va.addUpdateListener(this);
        va.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        va.removeUpdateListener(this);
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int heading = (int) animation.getAnimatedValue();
        setRotation(heading);
    }
}
0
Deliganli