2014-02-17 12 views
10

Ich bin neu mit Android und ich bin auf der Suche nach einer Möglichkeit, mein Buttonimage auf Klick zu schütteln. Ich habe das bis jetzt, aber es stürzt die ganze Zeit ab. Oder wenn Sie darauf klicken, funktioniert nichts. Ich hoffe, Sie können mir helfen. Wenn ich etwas vergessen habe, sag es einfach. Code kann unordentlich sein.Android Buttonbild Shake Animation

Ich habe einen Animationsordner. mit shakeanim.xml

Code:

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> 

MainActivity.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" /> 
+0

Wenn Sie wirklich wollen, dass es schütteln Sie sollten benutzerdefinierte Interpolaror verwenden, die hin und her geht – pskink

+0

Leider können Sie den Interpolator in Ihrer XML-Ressource der Animation nicht verwenden und es ist nur programmgesteuert verfügbar. –

+1

Überprüfen Sie dies: http://stackoverflow.com/questions/9448732/shaking-wobble-view-animation-in-android – zaingz

Antwort

16

Ersetzen Sie Ihre shakeanim.xml Datei mit diesem.

<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 können Sie Ihre MainActivity „setOnClickListener“ wie folgt ändern, wie Sie bereits Image erklärt.

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

Wenn dies hilft, markieren Sie es bitte als die richtige Antwort. Danke

+0

die Animation springt! –

+0

was ist mit diesem formXDelta – Dennis