java - Android按钮图片抖动动画

标签 java android xml imagebutton shake

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

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

代码:

activity_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>

主 Activity .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" />

最佳答案

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

顺便说一句,您可以像这样更改 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);  
            }  
        }); 

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

关于java - Android按钮图片抖动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837670/

相关文章:

java - 通过与旧值进行比较来拦截 DB 中的字段是否已使用 JAVA 更改

java - 为数据库以外的数据源设计 DAO

android - 如何使用内置的谷歌地图应用程序根据数据显示许多地点标记

android - C++ 服务器使用 TCP 向 Android 客户端发送消息

c++ - 使用 Boost 更好的 XML 格式化?

java - 在基于 php 的应用程序中嵌入 DITA Open Toolkit

java - 如何在 Thymeleaf 3.0.x 中的自定义方言的处理器中获取标签正文

java - 这个实现会被称为匿名类吗?

java - 从 Assets 文件夹或 SD 卡访问文件有什么区别

Java session 超时 - 当我第一次启动应用程序时,会显示 session 过期消息