Android
एनिमेटर
खोज…
एक ImageView का शेक एनीमेशन
Res फ़ोल्डर के तहत, अपने एनीमेशन संसाधनों को संग्रहीत करने के लिए "एनीमेशन" नामक एक नया फ़ोल्डर बनाएं और इसे उस फ़ोल्डर पर रखें।
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" />
लैंडिंग नामक एक रिक्त गतिविधि बनाएँ
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>
और 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);
}
में / बाहर एनीमेशन फीका
किसी दृश्य को धीरे-धीरे या उसके बाहर फीका करने के लिए, ObjectAnimator उपयोग करें। जैसा कि नीचे दिए गए कोड में देखा गया है, .setDuration(millis) का उपयोग करके एक अवधि निर्धारित करें जहां millis पैरामीटर एनीमेशन की अवधि (मिलीसेकंड में) है। नीचे दिए गए कोड में, विचार 500 मिलीसेकंड या 1/2 सेकंड से अधिक में फीका हो जाएगा। ObjectAnimator का एनीमेशन शुरू करने के लिए, .start() कॉल करें। एक बार एनीमेशन पूरा हो जाने के बाद, onAnimationEnd(Animator animation) कहा जाता है। यहाँ View.GONE या 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();
}
TransrawDrawable एनीमेशन
यह उदाहरण केवल दो छवियों के साथ एक छवि दृश्य के लिए एक लेनदेन प्रदर्शित करता है। (लूप के रूप में प्रत्येक लेनदेन के बाद पहली और दूसरी परत पदों के लिए एक के बाद एक और छवियों का उपयोग कर सकता है)
-
res/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 एक मान को चेतन करने का एक सरल तरीका पेश करता है (किसी विशेष प्रकार, जैसे int , float , आदि)।
इसका उपयोग करने का सामान्य तरीका है:
- एक बनाएं
ValueAnimatorकि से एक मूल्य चेतनminके लिएmax - एक
UpdateListenerसूची जोड़ें जिसमें आप गणना किए गए एनिमेटेड मान का उपयोग करेंगे (जिसे आपgetAnimatedValue()साथ प्राप्त कर सकते हैं)
दो तरीके हैं जिनसे आप ValueAnimator बना सकते हैं:
(उदाहरण के कोड एक एनिमेट float से 20f को 40f में 250ms )
-
xml(इसे/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();
- कोड से:
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 का एक उपवर्ग है ValueAnimator एक की संपत्ति की गणना मान सेट करने की अतिरिक्त क्षमता के साथ target View ।
ValueAnimator तरह, ObjectAnimator बनाने के दो तरीके हैं:
(उदाहरण कोड 0.2f में 250ms से 0.4f में एक View एक alpha को 0.2f 250ms )
-
xml(इसे/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();
- कोड से:
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 एक View गुणों को View लिए एक सरल और अनुकूलित तरीका है।
हर एक View में एक ViewPropertyAnimator वस्तु animate() विधि के माध्यम से उपलब्ध है। आप एक साधारण कॉल के साथ एक साथ कई गुणों को चेतन करने के लिए उपयोग कर सकते हैं। ViewPropertyAnimator की हर एक विधि एक विशिष्ट पैरामीटर के लक्ष्य मान को निर्दिष्ट करती है जिसे ViewPropertyAnimator को चेतन करना चाहिए।
View exampleView = ...;
exampleView.animate()
.alpha(0.6f)
.translationY(200)
.translationXBy(10)
.scaleX(1.5f)
.setDuration(250)
.setInterpolator(new FastOutLinearInInterpolator());
नोट: ViewPropertyAnimator ऑब्जेक्ट पर कॉलिंग start() अनिवार्य नहीं है । यदि आप ऐसा नहीं करते हैं, तो आप उचित समय में एनीमेशन की शुरुआत को संभालने के लिए मंच को दे रहे हैं (अगला एनीमेशन हैंडलिंग पास)। यदि आप वास्तव में ऐसा करते हैं (कॉल start() ) तो आप सुनिश्चित कर रहे हैं कि एनीमेशन तुरंत शुरू हो गया है।
विस्तार और संक्षिप्त करें एनीमेशन देखें
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);
}
}