Sobes.tech
Middle

Ի՞նչ պետք է անել էկրանների միջև անցումային անիմացիայի համար։

sobes.tech AI

Պատասխան AI-ից

Android-ում էկրանների միջև անցումների անիմացիաները իրականացնելու համար կարելի է օգտագործել մի քանի մոտեցումներ՝

  1. Ակտիվությունների (Activity Animations) անիմացիաներ: Դրանք սահմանվում են թեմաներում կամ ծրագրային կերպով։ Դրանք թույլ են տալիս սահմանել ստանդարտ մուտքի և ելքի անիմացիաները ակտիվությունների համար։

    override fun startActivity(intent: Intent) {
        super.startActivity(intent)
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
    }
    
    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)
    }
    

    Անիմացիայի ֆայլեր (օրինակ՝ res/anim/slide_in_right.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:fromXDelta="100%"
            android:toXDelta="0%"
            android:duration="@android:integer/config_shortAnimTime" />
    </set>
    
  2. Ֆրագմենտների (Fragment Animations) անիմացիաներ: Դրանք սահմանվում են ֆրագմենտի փոխհարաբերության ժամանակ։

    supportFragmentManager.beginTransaction()
        .setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out)
        .replace(R.id.container, fragment)
        .commit()
    
  3. Կիսված տարրերի (Shared Element Transitions) անիմացիաներ: Դրանք թույլ են տալիս անիմատորացնել մեկ կամ մի քանի View-ների անցումը ekranների միջև՝ ստեղծելով անընդհատության էֆեկտ։ Պահանջվում է աջակցություն transition-ի ակտիվացման թեմայում։

    Ակտիվության սկզբնական հատվածում՝

    val options = ActivityOptionsCompat.makeSceneTransitionAnimation(
        this,
        sharedView, // View անիմացիայի համար
        ViewCompat.getTransitionName(sharedView) // Անուն կապելու համար
    )
    startActivity(intent, options.toBundle())
    

    Ակտիվության ընդունող հատվածում՝

    // onCreate կամ onViewCreated մեթոդում (ֆրագմենտի համար)
    supportPostponeEnterTransition() // Դադարեցնել մուտքի անիմացիան՝ մինչև View-ները պատրաստ լինեն
    // Երբ View-ները պատրաստ լինեն (օրինակ՝ նկարի բեռնումից հետո),
    supportStartPostponedEnterTransition()
    

    Երկու էկրանների XML ձևաչափում՝

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:transitionName="image_transition" /> <!-- Անուն կապելու համար -->
    
  4. Component Animations (Navigation Component): Եթե օգտագործվում է Navigation Component, անցումների անիմացիաները կարելի է սահմանել ուղղակի նավիգացիայի գրաֆում՝

    <action
        android:id="@+id/action_fragmentA_to_fragmentB"
        app:destination="@id/fragmentB"
        app:enterAnim="@anim/slide_in_right"
        app:exitAnim="@anim/slide_out_left"
        app:popEnterAnim="@anim/slide_in_left"
        app:popExitAnim="@anim/slide_out_right" />
    

Ընտրությունը կախված է օգտագործվող ճարտարապետությունից (Activity, Fragments, Navigation Component) և անհրաժեշտ էֆեկտից։