जब आपका ऐप्लिकेशन इस्तेमाल किया जा रहा होता है, तब स्क्रीन पर नई जानकारी दिखती है और पुरानी जानकारी हट जाती है. स्क्रीन पर दिखने वाली जानकारी में तुरंत बदलाव होने से, उपयोगकर्ताओं को परेशानी हो सकती है. साथ ही, हो सकता है कि वे अचानक दिखने वाले नए कॉन्टेंट को न देख पाएं. ऐनिमेशन की मदद से, बदलावों को धीरे-धीरे दिखाया जाता है. साथ ही, मोशन की मदद से उपयोगकर्ता का ध्यान खींचा जाता है, ताकि अपडेट ज़्यादा साफ़ तौर पर दिखें.
किसी व्यू को दिखाने या छिपाने के लिए, तीन सामान्य ऐनिमेशन का इस्तेमाल किया जा सकता है: रिवील ऐनिमेशन, क्रॉसफ़ेड ऐनिमेशन, और कार्डफ़्लिप ऐनिमेशन.
क्रॉसफ़ेड ऐनिमेशन बनाना
क्रॉसफ़ेड ऐनिमेशन को डिसॉल्व भी कहा जाता है. इसमें एक View या
ViewGroup धीरे-धीरे फ़ेड आउट होता है, जबकि दूसरा धीरे-धीरे फ़ेड इन होता है. यह ऐनिमेशन उन स्थितियों के लिए काम का है जहां आपको अपने ऐप्लिकेशन में कॉन्टेंट या व्यू बदलने हों. यहां दिखाए गए क्रॉसफ़ेड ऐनिमेशन में
ViewPropertyAnimator का इस्तेमाल किया गया है. यह Android 3.1 (एपीआई लेवल 12) और उसके बाद के वर्शन के लिए उपलब्ध है.
यहां प्रोग्रेस इंडिकेटर से टेक्स्ट कॉन्टेंट में क्रॉसफ़ेड का एक उदाहरण दिया गया है:
व्यू बनाना
वे दो व्यू बनाएं जिन्हें आपको क्रॉसफ़ेड करना है. यहां दिए गए उदाहरण में, एक प्रोग्रेस इंडिकेटर और स्क्रोल किया जा सकने वाला टेक्स्ट व्यू बनाया गया है:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView style="?android:textAppearanceMedium"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"
android:padding="16dp" />
</ScrollView>
<ProgressBar android:id="@+id/loading_spinner"
style="?android:progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
क्रॉसफ़ेड ऐनिमेशन सेट अप करना
क्रॉसफ़ेड ऐनिमेशन सेट अप करने के लिए, यह तरीका अपनाएं:
- उन व्यू के लिए मेंबर वैरिएबल बनाएं जिन्हें आपको क्रॉसफ़ेड करना है. ऐनिमेशन के दौरान व्यू में बदलाव करते समय, आपको इन रेफ़रंस की ज़रूरत होगी.
- उस व्यू की विज़िबिलिटी को
GONEपर सेट करें जो फ़ेड इन हो रहा है. ऐसा करने से, व्यू लेआउट स्पेस का इस्तेमाल नहीं करता और लेआउट की कैलकुलेशन में शामिल नहीं होता. इससे प्रोसेसिंग की स्पीड बढ़ जाती है config_shortAnimTimeसिस्टम प्रॉपर्टी को किसी मेंबर वैरिएबल में कैश करें. यह प्रॉपर्टी, ऐनिमेशन के लिए "कम" अवधि तय करती है. यह अवधि, हल्के ऐनिमेशन या बार-बार होने वाले ऐनिमेशन के लिए सही है.config_longAnimTimeऔरconfig_mediumAnimTimeभी उपलब्ध हैं.
यहां पिछले कोड स्निपेट के लेआउट को ऐक्टिविटी कॉन्टेंट व्यू के तौर पर इस्तेमाल करने का एक उदाहरण दिया गया है:
Kotlin
class CrossfadeActivity : Activity() { private lateinit var contentView: View private lateinit var loadingView: View private var shortAnimationDuration: Int = 0 ... override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_crossfade) contentView = findViewById(R.id.content) loadingView = findViewById(R.id.loading_spinner) // Initially hide the content view. contentView.visibility = View.GONE // Retrieve and cache the system's default "short" animation time. shortAnimationDuration = resources.getInteger(android.R.integer.config_shortAnimTime) } ... }
Java
public class CrossfadeActivity extends Activity { private View contentView; private View loadingView; private int shortAnimationDuration; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crossfade); contentView = findViewById(R.id.content); loadingView = findViewById(R.id.loading_spinner); // Initially hide the content view. contentView.setVisibility(View.GONE); // Retrieve and cache the system's default "short" animation time. shortAnimationDuration = getResources().getInteger( android.R.integer.config_shortAnimTime); } ... }
व्यू को क्रॉसफ़ेड करना
व्यू को सही तरीके से सेट अप करने के बाद, उन्हें क्रॉसफ़ेड करने के लिए यह तरीका अपनाएं:
- उस व्यू के लिए जिसकी विज़िबिलिटी
GONEपर सेट है, ऐल्फ़ा वैल्यू को 0 और विज़िबिलिटी कोVISIBLEपर सेट करें. इससे व्यू दिखता है, लेकिन पारदर्शी होता है. - उस व्यू के लिए जो फ़ेड इन हो रहा है, उसकी ऐल्फ़ा वैल्यू को 0 से 1 तक ऐनिमेट करें. उस व्यू के लिए जो फ़ेड आउट हो रहा है, उसकी ऐल्फ़ा वैल्यू को 1 से 0 तक ऐनिमेट करें.
-
Animator.AnimatorListenerमेंonAnimationEnd()का इस्तेमाल करके, उस व्यू की विज़िबिलिटी कोGONEपर सेट करें जो फ़ेड आउट हो रहा है. भले ही, ऐल्फ़ा वैल्यू 0 हो, लेकिन व्यू की विज़िबिलिटी कोGONEपर सेट करने से, व्यू लेआउट स्पेस का इस्तेमाल नहीं करता और लेआउट की कैलकुलेशन में शामिल नहीं होता. इससे प्रोसेसिंग की स्पीड बढ़ जाती है.
यहां दिए गए तरीके में, इसे करने का एक उदाहरण दिखाया गया है:
Kotlin
class CrossfadeActivity : Activity() { private lateinit var contentView: View private lateinit var loadingView: View private var shortAnimationDuration: Int = 0 ... private fun crossfade() { contentView.apply { // Set the content view to 0% opacity but visible, so that it is // visible but fully transparent during the animation. alpha = 0f visibility = View.VISIBLE // Animate the content view to 100% opacity and clear any animation // listener set on the view. animate() .alpha(1f) .setDuration(shortAnimationDuration.toLong()) .setListener(null) } // Animate the loading view to 0% opacity. After the animation ends, // set its visibility to GONE as an optimization step so it doesn't // participate in layout passes. loadingView.animate() .alpha(0f) .setDuration(shortAnimationDuration.toLong()) .setListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { loadingView.visibility = View.GONE } }) } }
Java
public class CrossfadeActivity extends Activity { private View contentView; private View loadingView; private int shortAnimationDuration; ... private void crossfade() { // Set the content view to 0% opacity but visible, so that it is // visible but fully transparent during the animation. contentView.setAlpha(0f); contentView.setVisibility(View.VISIBLE); // Animate the content view to 100% opacity and clear any animation // listener set on the view. contentView.animate() .alpha(1f) .setDuration(shortAnimationDuration) .setListener(null); // Animate the loading view to 0% opacity. After the animation ends, // set its visibility to GONE as an optimization step so it doesn't // participate in layout passes. loadingView.animate() .alpha(0f) .setDuration(shortAnimationDuration) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { loadingView.setVisibility(View.GONE); } }); } }
कार्ड फ़्लिप ऐनिमेशन बनाना
कार्ड फ़्लिप की मदद से, कॉन्टेंट के व्यू के बीच स्विच किया जा सकता है. इसके लिए, कार्ड के फ़्लिप होने जैसा ऐनिमेशन दिखाया जाता है. यहां दिखाए गए कार्ड फ़्लिप ऐनिमेशन में
FragmentTransaction का इस्तेमाल किया गया है.
कार्ड फ़्लिप इस तरह दिखता है:
ऐनिमेटर ऑब्जेक्ट बनाना
कार्ड फ़्लिप ऐनिमेशन बनाने के लिए, आपको चार ऐनिमेटर की ज़रूरत होती है. दो ऐनिमेटर तब काम आते हैं, जब कार्ड का सामने वाला हिस्सा ऐनिमेट होकर बाईं ओर जाता है और जब वह ऐनिमेट होकर बाईं ओर से आता है. बाकी दो ऐनिमेटर तब काम आते हैं, जब कार्ड का पीछे वाला हिस्सा ऐनिमेट होकर दाईं ओर से आता है और जब वह ऐनिमेट होकर दाईं ओर जाता है.
card_flip_left_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Before rotating, immediately set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<!-- Rotate. -->
<objectAnimator
android:valueFrom="-180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="@integer/card_flip_time_full" />
<!-- Halfway through the rotation, set the alpha to 1. See startOffset. -->
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="@integer/card_flip_time_half"
android:duration="1" />
</set>
card_flip_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Rotate. -->
<objectAnimator
android:valueFrom="0"
android:valueTo="180"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="@integer/card_flip_time_full" />
<!-- Halfway through the rotation, set the alpha to 0. See startOffset. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:startOffset="@integer/card_flip_time_half"
android:duration="1" />
</set>
card_flip_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Before rotating, immediately set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<!-- Rotate. -->
<objectAnimator
android:valueFrom="180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="@integer/card_flip_time_full" />
<!-- Halfway through the rotation, set the alpha to 1. See startOffset. -->
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="@integer/card_flip_time_half"
android:duration="1" />
</set>
card_flip_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Rotate. -->
<objectAnimator
android:valueFrom="0"
android:valueTo="-180"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="@integer/card_flip_time_full" />
<!-- Halfway through the rotation, set the alpha to 0. See startOffset. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:startOffset="@integer/card_flip_time_half"
android:duration="1" />
</set>
व्यू बनाना
कार्ड का हर हिस्सा एक अलग लेआउट होता है. इसमें अपनी पसंद का कोई भी कॉन्टेंट शामिल किया जा सकता है. जैसे, दो टेक्स्ट व्यू, दो इमेज या व्यू का कोई भी कॉम्बिनेशन, जिनके बीच फ़्लिप किया जा सकता है. उन फ़्रैगमेंट में दो लेआउट का इस्तेमाल करें जिन्हें आपको बाद में ऐनिमेट करना है. यहां दिए गए लेआउट से कार्ड का एक हिस्सा बनता है, जिसमें टेक्स्ट दिखता है:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#a6c"
android:padding="16dp"
android:gravity="bottom">
<TextView android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/card_back_title" />
<TextView style="?android:textAppearanceSmall"
android:textAllCaps="true"
android:textColor="#80ffffff"
android:textStyle="bold"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/card_back_description" />
</LinearLayout>
इसके बाद, यहां दिए गए लेआउट से कार्ड का दूसरा हिस्सा बनता है, जिसमें
ImageView दिखता है:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image1"
android:scaleType="centerCrop"
android:contentDescription="@string/description_image_1" />
फ़्रैगमेंट बनाना
कार्ड के सामने और पीछे के हिस्से के लिए, फ़्रैगमेंट क्लास बनाएं. अपनी फ़्रैगमेंट
क्लास में, बनाए गए लेआउट को
onCreateView()
तरीके से वापस लाएं. इसके बाद, पैरंट ऐक्टिविटी में इस फ़्रैगमेंट के इंस्टेंस बनाए जा सकते हैं, जहां आपको कार्ड दिखाना है.
यहां दिए गए उदाहरण में, पैरंट ऐक्टिविटी के अंदर नेस्ट किए गए फ़्रैगमेंट क्लास दिखाए गए हैं, जो उनका इस्तेमाल करती है:
Kotlin
class CardFlipActivity : FragmentActivity() { ... /** * A fragment representing the front of the card. */ class CardFrontFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View = inflater.inflate(R.layout.fragment_card_front, container, false) } /** * A fragment representing the back of the card. */ class CardBackFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View = inflater.inflate(R.layout.fragment_card_back, container, false) } }
Java
public class CardFlipActivity extends FragmentActivity { ... /** * A fragment representing the front of the card. */ public class CardFrontFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_card_front, container, false); } } /** * A fragment representing the back of the card. */ public class CardBackFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_card_back, container, false); } } }
कार्ड फ़्लिप को ऐनिमेट करना
पैरंट ऐक्टिविटी में फ़्रैगमेंट दिखाएं. इसके लिए, अपनी ऐक्टिविटी के लिए लेआउट बनाएं. यहां दिए गए उदाहरण में, एक
FrameLayout बनाया गया है, जिसमें रनटाइम के दौरान
फ़्रैगमेंट जोड़े जा सकते हैं:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ऐक्टिविटी कोड में, कॉन्टेंट व्यू को वह लेआउट सेट करें जिसे आपने बनाया है. ऐक्टिविटी बनने पर, डिफ़ॉल्ट फ़्रैगमेंट दिखाना एक अच्छी प्रैक्टिस है. यहां दी गई उदाहरण ऐक्टिविटी में, डिफ़ॉल्ट तौर पर कार्ड का सामने वाला हिस्सा दिखाने का तरीका बताया गया है:
Kotlin
class CardFlipActivity : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_activity_card_flip) if (savedInstanceState == null) { supportFragmentManager.beginTransaction() .add(R.id.container, CardFrontFragment()) .commit() } } ... }
Java
public class CardFlipActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_card_flip); if (savedInstanceState == null) { getSupportFragmentManager() .beginTransaction() .add(R.id.container, new CardFrontFragment()) .commit(); } } ... }
कार्ड का सामने वाला हिस्सा दिखने पर, फ़्लिप ऐनिमेशन की मदद से कार्ड का पीछे वाला हिस्सा सही समय पर दिखाया जा सकता है. कार्ड का दूसरा हिस्सा दिखाने के लिए, एक तरीका बनाएं. इसमें ये काम किए जाते हैं:
- फ़्रैगमेंट ट्रांज़िशन के लिए बनाए गए कस्टम ऐनिमेशन सेट करता है.
- दिखाए गए फ़्रैगमेंट को नए फ़्रैगमेंट से बदलता है और बनाए गए कस्टम ऐनिमेशन की मदद से इस इवेंट को ऐनिमेट करता है.
- पहले दिखाए गए फ़्रैगमेंट को फ़्रैगमेंट बैक स्टैक में जोड़ता है, ताकि जब उपयोगकर्ता 'वापस जाएं' बटन पर टैप करे, तो कार्ड वापस फ़्लिप हो जाए.
Kotlin
class CardFlipActivity : FragmentActivity() { ... private fun flipCard() { if (showingBack) { supportFragmentManager.popBackStack() return } // Flip to the back. showingBack = true // Create and commit a new fragment transaction that adds the fragment // for the back of the card, uses custom animations, and is part of the // fragment manager's back stack. supportFragmentManager.beginTransaction() // Replace the default fragment animations with animator // resources representing rotations when switching to the back // of the card, as well as animator resources representing // rotations when flipping back to the front, such as when the // system Back button is tapped. .setCustomAnimations( R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out ) // Replace any fragments in the container view with a fragment // representing the next page, indicated by the just-incremented // currentPage variable. .replace(R.id.container, CardBackFragment()) // Add this transaction to the back stack, letting users press // the Back button to get to the front of the card. .addToBackStack(null) // Commit the transaction. .commit() } }
Java
public class CardFlipActivity extends FragmentActivity { ... private void flipCard() { if (showingBack) { getSupportFragmentManager().popBackStack(); return; } // Flip to the back. showingBack = true; // Create and commit a new fragment transaction that adds the fragment // for the back of the card, uses custom animations, and is part of the // fragment manager's back stack. getSupportFragmentManager() .beginTransaction() // Replace the default fragment animations with animator // resources representing rotations when switching to the back // of the card, as well as animator resources representing // rotations when flipping back to the front, such as when the // system Back button is pressed. .setCustomAnimations( R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out) // Replace any fragments in the container view with a fragment // representing the next page, indicated by the just-incremented // currentPage variable. .replace(R.id.container, new CardBackFragment()) // Add this transaction to the back stack, letting users press // Back to get to the front of the card. .addToBackStack(null) // Commit the transaction. .commit(); } }
सर्कुलर रिवील ऐनिमेशन बनाना
रिवील ऐनिमेशन की मदद से, यूज़र इंटरफ़ेस (यूआई) एलिमेंट के ग्रुप को दिखाने या छिपाने पर, उपयोगकर्ताओं को विज़ुअल तौर पर एक जैसा अनुभव मिलता है. The
ViewAnimationUtils.createCircularReveal()
तरीके से, क्लिपिंग सर्कल को ऐनिमेट करके किसी व्यू को दिखाया या छिपाया जा सकता है. यह
ऐनिमेशन,
ViewAnimationUtils क्लास में उपलब्ध है,
यह Android 5.0 (एपीआई लेवल 21) और इसके बाद के वर्शन के लिए उपलब्ध है.
यहां एक उदाहरण दिया गया है, जिसमें पहले से न दिखने वाले व्यू को दिखाने का तरीका बताया गया है:
Kotlin
// A previously invisible view. val myView: View = findViewById(R.id.my_view) // Check whether the runtime version is at least Android 5.0. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Get the center for the clipping circle. val cx = myView.width / 2 val cy = myView.height / 2 // Get the final radius for the clipping circle. val finalRadius = Math.hypot(cx.toDouble(), cy.toDouble()).toFloat() // Create the animator for this view. The start radius is 0. val anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0f, finalRadius) // Make the view visible and start the animation. myView.visibility = View.VISIBLE anim.start() } else { // Set the view to invisible without a circular reveal animation below // Android 5.0. myView.visibility = View.INVISIBLE }
Java
// A previously invisible view. View myView = findViewById(R.id.my_view); // Check whether the runtime version is at least Android 5.0. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Get the center for the clipping circle. int cx = myView.getWidth() / 2; int cy = myView.getHeight() / 2; // Get the final radius for the clipping circle. float finalRadius = (float) Math.hypot(cx, cy); // Create the animator for this view. The start radius is 0. Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0f, finalRadius); // Make the view visible and start the animation. myView.setVisibility(View.VISIBLE); anim.start(); } else { // Set the view to invisible without a circular reveal animation below // Android 5.0. myView.setVisibility(View.INVISIBLE); }
ViewAnimationUtils.createCircularReveal() ऐनिमेशन में पांच पैरामीटर होते हैं.
पहला पैरामीटर वह व्यू होता है जिसे आपको स्क्रीन पर छिपाना या दिखाना है. अगले दो पैरामीटर, क्लिपिंग सर्कल के बीच के X और Y कोऑर्डिनेट होते हैं. आम तौर पर, यह व्यू का बीच वाला हिस्सा होता है. हालांकि, उपयोगकर्ता जिस पॉइंट पर टैप करता है उसका इस्तेमाल भी किया जा सकता है, ताकि ऐनिमेशन वहीं से शुरू हो जहां उपयोगकर्ता ने चुना है. चौथा पैरामीटर, क्लिपिंग सर्कल का शुरुआती रेडियस होता है.
पिछले उदाहरण में, शुरुआती रेडियस को शून्य पर सेट किया गया है, ताकि दिखने वाला व्यू सर्कल से छिपा रहे. आखिरी पैरामीटर, सर्कल का फ़ाइनल रेडियस होता है. किसी व्यू को दिखाते समय, फ़ाइनल रेडियस को व्यू से बड़ा रखें, ताकि ऐनिमेशन खत्म होने से पहले व्यू पूरी तरह से दिख जाए.
पहले से दिखने वाले व्यू को छिपाने के लिए, यह तरीका अपनाएं:
Kotlin
// A previously visible view. val myView: View = findViewById(R.id.my_view) // Check whether the runtime version is at least Android 5.0. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Get the center for the clipping circle. val cx = myView.width / 2 val cy = myView.height / 2 // Get the initial radius for the clipping circle. val initialRadius = Math.hypot(cx.toDouble(), cy.toDouble()).toFloat() // Create the animation. The final radius is 0. val anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0f) // Make the view invisible when the animation is done. anim.addListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { super.onAnimationEnd(animation) myView.visibility = View.INVISIBLE } }) // Start the animation. anim.start() } else { // Set the view to visible without a circular reveal animation below // Android 5.0. myView.visibility = View.VISIBLE }
Java
// A previously visible view. final View myView = findViewById(R.id.my_view); // Check whether the runtime version is at least Android 5.0. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Get the center for the clipping circle. int cx = myView.getWidth() / 2; int cy = myView.getHeight() / 2; // Get the initial radius for the clipping circle. float initialRadius = (float) Math.hypot(cx, cy); // Create the animation. The final radius is 0. Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0f); // Make the view invisible when the animation is done. anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); myView.setVisibility(View.INVISIBLE); } }); // Start the animation. anim.start(); } else { // Set the view to visible without a circular reveal animation below Android // 5.0. myView.setVisibility(View.VISIBLE); }
इस मामले में, क्लिपिंग सर्कल का शुरुआती रेडियस, व्यू के साइज़ के बराबर सेट किया जाता है, ताकि ऐनिमेशन शुरू होने से पहले व्यू दिखे. फ़ाइनल रेडियस को शून्य पर सेट किया जाता है, ताकि ऐनिमेशन खत्म होने पर व्यू छिप जाए.
ऐनिमेशन में एक लिसनर जोड़ें, ताकि ऐनिमेशन पूरा होने पर व्यू की विज़िबिलिटी को
INVISIBLE सेट किया जा सके.
अन्य संसाधन
- Jetpack Compose की मदद सेऐनिमेशन.
- Jetpack Compose की मदद से जेस्चर.