ऐनिमेशन का इस्तेमाल करके, कोई व्यू दिखाना या छिपाना

लिखने का तरीका आज़माएं
Android के लिए, Jetpack Compose हमारा सुझाया गया यूज़र इंटरफ़ेस (यूआई) टूलकिट है. Compose में ऐनिमेशन इस्तेमाल करने का तरीका जानें.

आपके ऐप्लिकेशन के इस्तेमाल के दौरान, स्क्रीन पर नई और पुरानी जानकारी दिखती है जानकारी हटा दी जाती है. स्क्रीन पर दिखने वाली चीज़ों को तुरंत बदला जा सकता है इससे उपयोगकर्ता परेशान हो सकते हैं और हो सकता है कि वे अचानक दिखने वाले नए कॉन्टेंट को न देख पाएं. एनिमेशन धीमा बदलाव कम कर सकते हैं और उपयोगकर्ता का ध्यान हिलते हुए देख सकते हैं, ताकि अपडेट और साफ़ तौर पर बताया जा सकता है.

यहां दिए गए तीन सामान्य ऐनिमेशन का इस्तेमाल करके, किसी व्यू को दिखाया या छिपाया जा सकता है: ऐनिमेशन, क्रॉसफ़ेड ऐनिमेशन, और कार्डफ़्लिप ऐनिमेशन.

क्रॉसफ़ेड ऐनिमेशन बनाएं

क्रॉसफ़ेड ऐनिमेशन—जिसे डिसॉल्व भी कहा जाता है— धीरे-धीरे फ़ेड हो जाता है एक 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>

क्रॉसफ़ेड ऐनिमेशन सेट अप करना

क्रॉसफ़ेड ऐनिमेशन सेट अप करने के लिए, यह तरीका अपनाएं:

  1. उन व्यू के लिए सदस्य वैरिएबल बनाएं जिन्हें आपको क्रॉसफ़ेड करना है. आपको इनकी ज़रूरत होगी इन रेफ़रंस को बाद में, ऐनिमेशन के दौरान व्यू में बदलाव करते समय.
  2. उस व्यू की विज़िबिलिटी सेट करें जिसे फ़ेड किया जा रहा है GONE. इससे यह व्यू नहीं दिखेगा और इसे लेआउट कैलकुलेशन से हटा दिया जाता है. इससे तेज़ी से अप प्रोसेस हो रहा है
  3. config_shortAnimTime को कैश मेमोरी में सेव करें मेंबर वैरिएबल में सिस्टम प्रॉपर्टी शामिल है. यह प्रॉपर्टी स्टैंडर्ड "short" के बारे में बताती है ऐनिमेशन की कुल अवधि. यह अवधि हल्के ऐनिमेशन के लिए सही है या अक्सर दिखने वाले ऐनिमेशन. 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);
    }
    ...
}

व्यू को क्रॉसफ़ेड करें

जब व्यू सही तरीके से सेट अप हो जाएं, तो यह तरीका अपनाकर उन्हें क्रॉसफ़ेड करें:

  1. फ़ेड इन हो रहे व्यू के लिए, ऐल्फ़ा वैल्यू को 0 और 'किसको दिखे' पर सेट करें मूल तारीख से VISIBLE तक GONE की सेटिंग. इससे व्यू दिखने लायक, लेकिन पारदर्शी हो जाता है.
  2. जो व्यू फ़ेड इन हो रहा है उसके लिए, ऐल्फ़ा वैल्यू को 0 से 1 तक ऐनिमेट करें. जो धीरे-धीरे गायब हो रहा है, उसकी ऐल्फ़ा वैल्यू को 1 से 0 तक ऐनिमेट करें.
  3. इसका इस्तेमाल किया जा रहा है onAnimationEnd() में Animator.AnimatorListener, 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();
    }
}

सर्कुलर रिवील ऐनिमेशन बनाएं

किसी ग्रुप को दिखाने या छिपाने पर, ऐनिमेशन की मदद से उपयोगकर्ता को लगातार स्क्रीन पर एक जैसा कॉन्टेंट दिखता है के यूज़र इंटरफ़ेस (यूआई) एलिमेंट. कॉन्टेंट बनाने 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 जब ऐनिमेशन पूरा करता है.

अन्य संसाधन