Frage

Ich habe ein Layout mit einer Bildansicht und einer Web-Ansicht erstellt. Die Web-Ansicht wird eine Standard-Sichtbarkeit gegangen sein. Wenn die Aktivität Brände es die Bildansicht zeigt erste und wenn die Web-Ansicht fertig geladen ist die URL, es kennzeichnet sich als sichtbar und der Imageview ist als versteckt markiert.

Wenn die Imageview gezeigt wird, würde Ich mag es immer wieder für ein wenig hinzugefügt pizazz nur drehen.

Ich habe noch nie Animationen gemacht, bevor in Android und alle Beiträge, die ich fand, als ich das Internet gefragt waren nicht hilfreich; also, ich habe um Hilfe zu SO zurückgegeben.

Also, wenn ich mit diesem ...

starten
    final ImageView splash = (ImageView)findViewById(R.id.splash);

Wie erstelle ich eine wiederholte drehen Animation und es auf die Image?

Danke nochmal!

War es hilfreich?

Lösung

Verwenden Sie ein RotateAnimation , die Einstellung der Drehpunkt der Mitte des Bildes.

RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);

// Later.. stop the animation
splash.setAnimation(null);

Andere Tipps

Wie ein Bild um seinen Mittelpunkt drehen:

ImageView view = ... //Initialize ImageView via FindViewById or programatically

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

//Setup anim with desired properties
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely
anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds

//Start animation
view.startAnimation(anim); 
//Later on, use view.setAnimation(null) to stop it.

Dies bewirkt, dass das Bild zu drehen um seinen Mittelpunkt (0,5 oder 50% seiner Breite / Höhe). Ich veröffentliche diese für zukünftige Leser, die hier von Google bekommen, wie ich, und wer das Bild um seinen Mittelpunkt drehen will, ohne in absoluten Pixeln die Mitte zu definieren.

Sie können auch einfach die Drehen-Animation-Funktion verwenden. Das läuft eine bestimmte Animation, für eine vorbestimmte Menge an Zeit, an einem Image.

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);

Dann eine Animation XML-Datei in Ihrem res erstellen / Anim genannt rotate_picture mit dem Inhalt:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false">

    <rotate 
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="5000"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>
</set>

Jetzt leider wird dies nur einmal ausgeführt werden. Sie erhalten eine Schleife irgendwo müssen sie die Animation machen wiederholen, während sie wartet. Ich experimentierte ein wenig und bekam mein Programm in Endlosschleifen stecken, so bin ich nicht sicher, ob der beste Weg, um das. EDIT: Christopher Antwort liefert die Informationen darüber, wie es Schleife richtig zu machen, so meine schlechten Vorschlag über separate Threads Entfernen

Eine Möglichkeit - Sie Split Bild in N es leicht jedes Mal drehen. Ich würde sagen, 5 ist genug. erstellen Sie dann so etwas wie dieses in ziehbar

<animation-list   android:id="@+id/handimation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
 </animation-list> 

Code Start

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);

Code stop

AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);

hier

imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
    imgDics.setOnClickListener(onPlayer2Click);
    anim = new RotateAnimation(0f, 360f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                            0.5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(4000);

    // Start animating the image
    imgDics.startAnimation(anim);

Im Inneren des Elements setzen:

android:repeatCount="infinite"

Ich habe herausgefunden, dass, wenn Sie die .getWidth verwenden / 2 etc ..., dass es nicht funktionieren wird Ihnen die Anzahl der Pixel ist das Bild bekommen müssen und teilen sie durch 2 selbst und geben Sie dann einfach in die Nummer für die letzten 2 Argumente.

so sagen, Ihr Bild war ein 120 Pixel 120 Pixel quadratisch, ur x und y würde 60 Pixel betragen. so in Ihrem Code, würden Sie rechts:

RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

und jetzt Ihr Bild seine Mitte schwenken um.

Verifiziert Code:

imageView.setImageResource(R.drawable.ic_arrow_up);

boolean up = true;

if (!up) { 
    up = true; 
    imageView.startAnimation(animate(up)); 
} else { 
    up = false; 
    imageView.startAnimation(animate(up)); 
}

private Animation animate(boolean up) {
    Animation anim = AnimationUtils.loadAnimation(this, up ? R.anim.rotate_up : R.anim.rotate_down);
    anim.setInterpolator(new LinearInterpolator()); // for smooth animation
    return anim;
}

ziehbar / ic_arrow_up.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#3d3d3d"
        android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>

Anim / rotate_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="-180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="0" />
</set>

Anim / rotate_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <rotate
        android:duration="200"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="180" />
</set>

Sie keine harte Codebild Grenzen. Nur Verwendung:

RotateAnimation anim = new RotateAnimation( fromAngle, toAngle, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top