Java Android 按钮图像抖动动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21837670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:59:34  来源:igfitidea点击:

Android buttonimage shake animation

javaandroidxmlimagebuttonshake

提问by The M

I am new with android and im looking for a way to shake my buttonimage on click. I got this so far but it crashes all the time. Or if you click it nothing works. I hope you people can help me out. if i forgot anythiing just say it. code can be messy.

我是 android 新手,我正在寻找一种方法来在点击时晃动我的按钮图像。到目前为止我得到了这个,但它一直崩溃。或者,如果您单击它,则没有任何效果。我希望你们能帮助我。如果我忘了任何事情就说出来。代码可能很乱。

i got a anim folder. with shakeanim.xml

我有一个动画文件夹。使用shakeanim.xml

code:

代码:

activity_main.xml

活动_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

     <ImageButton
         android:id="@+id/imageButton1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:src="@drawable/chest"
        android:background="@null"/>

     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_above="@+id/imageButton1"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="51dp"
         android:text="100 Clicks" />

</RelativeLayout>

MainActivity.java

主活动.java

package com.example.egghatcher;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.TextView;


public class MainActivity extends Activity {

    ImageButton imageButton;

    TextView clicksToGo;

    Animation shake;

    private int clicks = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        shake = AnimationUtils.loadAnimation(this, R.anim.shakeanim);
    }

    public void addListenerOnButton() {

        imageButton = (ImageButton) findViewById(R.id.imageButton1);

        clicksToGo = (TextView)findViewById(R.id.textView1);

        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clicks--;               
                clicksToGo.setText("You need to click " + clicks + " more times to open it");
                findViewById(R.id.imageButton1).startAnimation(shake);  
            } 
        });

    }


}

shakeanim.xml

动画文件

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="0"
    android:interpolator="@android:anim/cycle_interpolator"

    android:toXDelta="10" />

回答by A. Adam

Replace your shakeanim.xml file with this.

用这个替换你的 Shakeanim.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>

btw you can change your MainActivity "setOnClickListener" like this as you have already declared imageButton.

顺便说一句,您可以像这样更改 MainActivity“setOnClickListener”,因为您已经声明了 imageButton。

imageButton.setOnClickListener(new OnClickListener() {

            @Override 
            public void onClick(View v) {
                clicks--;               
                clicksToGo.setText("You need to click " + clicks + " more times to open it");
                imageButton.startAnimation(shake);  
            }  
        }); 

If this helps please mark it as the right answer. Thank you

如果这有帮助,请将其标记为正确答案。谢谢