2016-05-10 8 views
0

Ich habe ein einfaches Android-Projekt, das eine leere Aktivität Seite mit einem Knopf in der Mitte ist. Ich will, wenn ich auf den Knopf klicke, dreht es sich 180 Grad (Wie das Bild, das Spiel zusammenpaßt, wenn man auf den Knopf zurückkehrt und die Hintergrundshow klickt).Android Button Animation Spin 180 Grad

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/activity_main" tools:context=".MainActivity"> 
<Button 
    android:layout_width="400px" 
    android:layout_height="400px" 
    android:id="@+id/btn_play" 
    android:layout_marginTop="174dp" 
    android:background="#001eff" 
    android:layout_centerHorizontal="true" /> 

Sorry für mein schlechtes Englisch und meine einfache Frage.

Danke.

+0

Schreiben Sie Ihren 'onClick'-Code so weit. ;) –

Antwort

0

Try Umsetzung roation Animation

erstellen rotation.xml in res/anim Ordner:

<rotate xmlns:android="”http://schemas.android.com/apk/res/android”"> 
    android:duration="4000" 
    android:fromdegrees="0" 
    android:pivotx="50%" 
    android:pivoty="50%" 
    android:todegrees="180" 
    android:toyscale="0.0" 
</rotate> 

Und in Ihrer Tätigkeit auf die Schaltfläche klicken:

btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       final Animation animation= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator); 


         animation.setAnimationListener(new Animation.AnimationListener() { 
          @Override 
          public void onAnimationStart(Animation animation) { 
          btn.startAnimation(animation); 
          } 

          @Override 
          public void onAnimationEnd(Animation animation) { 
           btn.setBackgroundResource(R.drawable.anotherImage); 
          } 

          @Override 
          public void onAnimationRepeat(Animation animation) { 

          } 
         }); 


        } 
      } 
     }); 
+0

es dreht sich nur, aber ich will etwas anderes, ich will, wenn ich darauf klicke, sehe die andere Seite des Knopfes (wie die Bild-Matching-Spiele). –

+0

@alikhorshidirozbahani Ok dann ändern Sie den Hintergrund der Schaltfläche, wenn es sich dreht. –

+0

ja, genau. –

0

können Sie verwenden ObjectAnimator für diese .

btn_play.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) 
      { 
      ObjectAnimator.ofFloat(view, "rotation", 0, 180).start(); 
      btn_play.setBackgroundColor(Color.RED) 
      } 
     }); 
+0

es nur drehen, aber ich möchte etwas anderes, ich will, wenn ich darauf klicken, sehen Sie die andere Seite des Knopfes in Animationsform (wie die Bild passenden Spiele) –

+0

überprüfen Sie meinen bearbeiteten Code .. Sie müssen Ihre Hintergrundfarbe ändern –

+0

Ich weiß das, mein Problem ist die Animation ist nicht etwas, das ich will.Lernen Sie mich ein Beispiel für Sie sagen: Stellen Sie sich ein Papier (meine Taste) haben zwei Seiten, zuerst sehe ich die erste Seite und dann, wenn ich es drehen kann ich die andere Seite des Papiers, aber die Anwendung tun es in der Animation. –

Verwandte Themen