Android
Animatoren
Suche…
Animation einer ImageView schütteln
Erstellen Sie im Ordner res einen neuen Ordner mit dem Namen "anim", um Ihre Animationsressourcen zu speichern, und legen Sie diesen in diesem Ordner ab.
shakeanimation.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-15"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="15" />
Erstellen Sie eine leere Aktivität namens Landing
activity_landing.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgBell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_notifications_white_48dp"/>
</RelativeLayout>
Und die Methode zum Animieren der Bildansicht auf Landing.java
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext=this;
setContentView(R.layout.activity_landing);
AnimateBell();
}
public void AnimateBell() {
Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shakeanimation);
ImageView imgBell= (ImageView) findViewById(R.id.imgBell);
imgBell.setImageResource(R.mipmap.ic_notifications_active_white_48dp);
imgBell.setAnimation(shake);
}
Animation ein- / ausblenden
Verwenden Sie einen ObjectAnimator , um eine Ansicht zu erhalten, die langsam ObjectAnimator oder ObjectAnimator . Wie im folgenden Code dargestellt, legen Sie eine Dauer mit .setDuration(millis) wobei der millis Parameter die Dauer (in Millisekunden) der Animation ist. Im folgenden Code werden die Ansichten über 500 Millisekunden oder 1/2 Sekunde ein- / ausgeblendet. Um die ObjectAnimator -Animation zu starten, rufen Sie .start() . Sobald die Animation abgeschlossen ist, wird onAnimationEnd(Animator animation) aufgerufen. Hier können Sie die Sichtbarkeit Ihrer Ansicht auf View.GONE oder View.VISIBLE .
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
void fadeOutAnimation(View viewToFadeOut) {
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(viewToFadeOut, "alpha", 1f, 0f);
fadeOut.setDuration(500);
fadeOut.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// We wanna set the view to GONE, after it's fade out. so it actually disappear from the layout & don't take up space.
viewToFadeOut.setVisibility(View.GONE);
}
});
fadeOut.start();
}
void fadeInAnimation(View viewToFadeIn) {
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(viewToFadeIn, "alpha", 0f, 1f);
fadeIn.setDuration(500);
fadeIn.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStar(Animator animation) {
// We wanna set the view to VISIBLE, but with alpha 0. So it appear invisible in the layout.
viewToFadeIn.setVisibility(View.VISIBLE);
viewToFadeIn.setAlpha(0);
}
});
fadeIn.start();
}
TransitionDrawable-Animation
In diesem Beispiel wird eine Transaktion für eine Bildansicht mit nur zwei Bildern angezeigt (nach der Transaktion können nacheinander mehrere Bilder für die Positionen der ersten und zweiten Ebene als Schleife verwendet werden.)
-
res/values/arrays.xmlzures/values/arrays.xml
<resources>
<array
name="splash_images">
<item>@drawable/spash_imge_first</item>
<item>@drawable/spash_img_second</item>
</array>
</resources>
private Drawable[] backgroundsDrawableArrayForTransition;
private TransitionDrawable transitionDrawable;
private void backgroundAnimTransAction() {
// set res image array
Resources resources = getResources();
TypedArray icons = resources.obtainTypedArray(R.array.splash_images);
@SuppressWarnings("ResourceType")
Drawable drawable = icons.getDrawable(0); // ending image
@SuppressWarnings("ResourceType")
Drawable drawableTwo = icons.getDrawable(1); // starting image
backgroundsDrawableArrayForTransition = new Drawable[2];
backgroundsDrawableArrayForTransition[0] = drawable;
backgroundsDrawableArrayForTransition[1] = drawableTwo;
transitionDrawable = new TransitionDrawable(backgroundsDrawableArrayForTransition);
// your image view here - backgroundImageView
backgroundImageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(4000);
transitionDrawable.setCrossFadeEnabled(false); // call public methods
}
ValueAnimator
ValueAnimator einfache Weise einen Wert (eines bestimmten Typs, z. B. int , float usw.) animieren.
Die übliche Art, es zu benutzen, ist:
- Erstellen Sie einen
ValueAnimator, der einen Wert vonminbismaxanimiert - Fügen Sie einen
UpdateListenerin dem Sie den berechneten animierten Wert verwenden (den Sie mitgetAnimatedValue()).
Es gibt zwei Möglichkeiten, den ValueAnimator erstellen:
(das Beispiel Code animiert einen float von 20f bis 40f in 250ms )
- Von
xml(füge es in/res/animator/):
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250"
android:valueFrom="20"
android:valueTo="40"
android:valueType="floatType"/>
ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(context,
R.animator.example_animator);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator anim) {
// ... use the anim.getAnimatedValue()
}
});
// set all the other animation-related stuff you want (interpolator etc.)
animator.start();
- Aus dem Code:
ValueAnimator animator = ValueAnimator.ofFloat(20f, 40f);
animator.setDuration(250);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator anim) {
// use the anim.getAnimatedValue()
}
});
// set all the other animation-related stuff you want (interpolator etc.)
animator.start();
ObjectAnimator
ObjectAnimator ist eine Unterklasse von ValueAnimator mit der zusätzlichen Möglichkeit , den berechneten Wert auf die Eigenschaft eines festlegen . target
Genau wie beim ValueAnimator gibt es zwei Möglichkeiten, den ObjectAnimator erstellen:
(Der Beispielcode animiert ein alpha einer View von 0.4f bis 0.2f in 250ms )
- Von
xml(in den/res/animator)
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250"
android:propertyName="alpha"
android:valueFrom="0.4"
android:valueTo="0.2"
android:valueType="floatType"/>
ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(context,
R.animator.example_animator);
animator.setTarget(exampleView);
// set all the animation-related stuff you want (interpolator etc.)
animator.start();
- Vom Code:
ObjectAnimator animator = ObjectAnimator.ofFloat(exampleView, View.ALPHA, 0.4f, 0.2f);
animator.setDuration(250);
// set all the animation-related stuff you want (interpolator etc.)
animator.start();
ViewPropertyAnimator
ViewPropertyAnimator ist eine vereinfachte und optimierte Methode zum Animieren von Eigenschaften einer View .
Für jede einzelne View ist ein ViewPropertyAnimator Objekt über die animate() -Methode verfügbar. Sie können damit mehrere Eigenschaften gleichzeitig mit einem einfachen Aufruf animieren. Jede einzelne Methode eines ViewPropertyAnimator gibt den Zielwert eines bestimmten Parameters an, den der ViewPropertyAnimator animieren soll.
View exampleView = ...;
exampleView.animate()
.alpha(0.6f)
.translationY(200)
.translationXBy(10)
.scaleX(1.5f)
.setDuration(250)
.setInterpolator(new FastOutLinearInInterpolator());
Hinweis: Der Aufruf von start() für ein ViewPropertyAnimator Objekt ist NICHT obligatorisch. Wenn Sie dies nicht tun, lassen Sie die Plattform den Start der Animation zum richtigen Zeitpunkt erledigen (nächster Animations-Durchlauf). Wenn Sie das tatsächlich tun (call start() ), stellen Sie sicher, dass die Animation sofort gestartet wird.
Erweitern und Reduzieren Sie die Animation der Ansicht
public class ViewAnimationUtils {
public static void expand(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.WRAP_CONTENT
: (int)(targtetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}