溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android怎么使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View

發(fā)布時(shí)間:2022-04-29 10:07:57 來源:億速云 閱讀:124 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Android怎么使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View”,在日常操作中,相信很多人在Android怎么使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android怎么使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

    1.引言

    在開發(fā)過程中,我們經(jīng)常會(huì)遇到需要顯示或隱藏View視圖的情況,如果在隱藏或顯示View的過程中加上動(dòng)畫,能讓交互更加的友好和動(dòng)感,本文將介紹如何使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View。

    2.圓形揭露動(dòng)畫簡介

    圓形揭露動(dòng)畫是動(dòng)畫的一種,是由ViewAnimationUtils類提供的,調(diào)用ViewAnimationUtils.createCircularReveal()方法可以創(chuàng)建圓形揭露動(dòng)畫,使用此動(dòng)畫要求API級別為21及以上版本,createCircularReveal()方法的參數(shù)如下:

    //view:使用圓形動(dòng)畫的視圖
    //centerX:裁剪圓形的中心的X坐標(biāo),這個(gè)中心是指相對于視圖本身
    //centerY:裁剪圓形的中心的Y坐標(biāo),這個(gè)中心是指相對于視圖本身
    //startRadius:圓形的起始半徑
    //endRadius:圓形的結(jié)束半徑
    public static Animator createCircularReveal(View view,int centerX,  int centerY, float startRadius, float endRadius)

    3.使用圓形揭露動(dòng)畫隱藏或顯示View

    3.1 簡易布局

    簡易布局如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btn_hide_or_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="隱藏或顯示"
            android:textColor="@color/black"
            android:textSize="18sp" />
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginTop="50dp"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>

    3.2 使用圓形揭露動(dòng)畫隱藏View

    首先要計(jì)算得出View相對于自身的中心點(diǎn)的X坐標(biāo)和Y坐標(biāo),然后調(diào)用Math.hypot()方法計(jì)算得出圓形的半徑,接著調(diào)用ViewAnimationUtils.createCircularReveal(imageView, ivXCenter, ivYCenter, circleRadius, 0f)創(chuàng)建圓形揭露動(dòng)畫,增加動(dòng)畫執(zhí)行的Listener,在動(dòng)畫執(zhí)行結(jié)束后調(diào)用imageView.setVisibility(View.GONE),最后啟動(dòng)動(dòng)畫,示例如下:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         int width = imageView.getWidth();
         int height = imageView.getHeight();
         int ivXCenter = width/2;
         int ivYCenter = height/2;
         float circleRadius = (float) Math.hypot(ivXCenter, ivYCenter);
         Animator circularReveal = ViewAnimationUtils.createCircularReveal(imageView, ivXCenter, ivYCenter, circleRadius, 0f);
         circularReveal.addListener(new AnimatorListenerAdapter() {
             @Override
             public void onAnimationEnd(Animator animation) {
                 super.onAnimationEnd(animation);
                 imageView.setVisibility(View.GONE);
                 isVisible = false;
             }
         });
         circularReveal.start();
     }else {
         imageView.setVisibility(View.GONE);
         isVisible = false;
     }

    3.3 使用圓形揭露動(dòng)畫顯示View

    使用圓形揭露動(dòng)畫顯示View,先計(jì)算得出View相對于自身的中心點(diǎn)的X坐標(biāo)和Y坐標(biāo),然后算出圓形的半徑,接著創(chuàng)建圓形揭露動(dòng)畫,此時(shí)的起始半徑是0f,結(jié)束半徑是圓形的半徑,調(diào)用imageView.setVisibility(View.VISIBLE),最后啟動(dòng)動(dòng)畫,示例如下:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        int width = imageView.getWidth();
        int height = imageView.getHeight();
        int ivXCenter = width/2;
        int ivYCenter = height/2;
        float circleRadius = (float) Math.hypot(ivXCenter, ivYCenter);
        Animator circularReveal = ViewAnimationUtils.createCircularReveal(imageView, ivXCenter, ivYCenter, 0f, circleRadius);
        imageView.setVisibility(View.VISIBLE);
        isVisible = true;
        circularReveal.start();
    }else {
        imageView.setVisibility(View.VISIBLE);
        isVisible = true;
    }

    到此,關(guān)于“Android怎么使用圓形揭露動(dòng)畫巧妙地隱藏或顯示View”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI