“何書(shū)還”之二(關(guān)于A(yíng)nimation使用)


官方介紹Animation使用

我看了一下官方文檔,但是無(wú)奈我的理解能力不夠好,跑了好多次還是看不見(jiàn)炫酷的動(dòng)畫(huà)效果,無(wú)奈只好去翻閱官方demo的源碼,終于知道了怎么使用。

Touch Feedback(觸摸反饋)

在XML文件中添加以下代碼可以添加觸摸反饋的動(dòng)畫(huà)效果(類(lèi)似于漣漪的效果)

  • ?android:attr/selectableItemBackground(有邊界,可以使用在listview的item上)
  • ?android:attr/selectableItemBackgroundBorderless(沒(méi)有邊界,或者直接看是看不到明顯的邊界,但是點(diǎn)擊是有漣漪效果)
    需要說(shuō)明android:attr/selectableItemBackgroundBorderless是API 21新增加的**

Circular Reveal

這個(gè)主要是提供一個(gè)圓形的顯示或者隱藏的動(dòng)畫(huà)效果,主要使用的api是[ViewAnimationUtils.createCircularReveal()](http://developer.android.com/reference/android/view/ViewAnimationUtils.html#createCircularReveal(android.view.View, int, int, float, float)

// previously invisible view
View myView = findViewById(R.id.my_view);

// get the center for the clipping circle
int cx = (myView.getLeft() + myView.getRight()) / 2;
int cy = (myView.getTop() + myView.getBottom()) / 2;

// get the final radius for the clipping circle
int finalRadius = Math.max(myView.getWidth(), myView.getHeight());

// create the animator for this view (the start radius is zero)
Animator anim =
    ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);

// make the view visible and start the animation
myView.setVisibility(View.VISIBLE);
anim.start();

里面幾個(gè)參數(shù)的意思分別為:

  • myView 要展示動(dòng)畫(huà)效果的視圖
  • cx 動(dòng)畫(huà)開(kāi)始的X坐標(biāo)
  • cy 動(dòng)畫(huà)開(kāi)始的Y坐標(biāo)
  • startRadius 動(dòng)畫(huà)開(kāi)始的角度
  • finalRadius 動(dòng)畫(huà)結(jié)束的角度

Activity Transitions

關(guān)于這個(gè)我就?不多寫(xiě)了,博主大苞米對(duì)于這個(gè)寫(xiě)的十分詳細(xì),給大家附上鏈接自己看一下,我就?Activity之間的視圖共享寫(xiě)一下,因?yàn)槲以谶@個(gè)地方花了一些時(shí)間,所以記下來(lái)以備后面忘記。

首先需要在XML文件中你需要共享的View定義一個(gè)android:transitionName

<ImageView
                android:id="@+id/bookPage"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:scaleType="centerCrop"
                android:tint="@color/photo_tint"
                android:transitionName="bookPage" />

然后可以定義動(dòng)畫(huà)效果:

1.可以在style文件中定義

<item name="android:windowContentTransitions">true</item>  
<item name="android:windowEnterTransition">@transition/explode</item>  
<item name="android:windowExitTransition">@transition/explode</item>

2.可以在java文件中定義

getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);  
getWindow().setExitTransition(new Explode());  

然后在使用Intent跳轉(zhuǎn)的時(shí)候,加入ActivityOptions

Intent intent = new Intent(this, Activity2.class);  
// shareView: 需要共享的視圖  
// bookPage就是剛剛 transitionName所寫(xiě)的值
ActivityOptions options = ActivityOptions  
        .makeSceneTransitionAnimation(this, shareView, "bookPage");  
startActivity(intent, options.toBundle());

對(duì)于一次共享多個(gè)View的需要使用Pair.create(view,"shareName"),這里必須是View。

關(guān)于結(jié)束Activity時(shí)同樣動(dòng)畫(huà)效果倒回可以使用Activity.finishAfterTransition()。

添加TranstionListener

同樣你可以在進(jìn)入動(dòng)畫(huà)時(shí)候添加動(dòng)畫(huà)監(jiān)聽(tīng)器,這樣你就可以在本頁(yè)面加載更多的動(dòng)畫(huà)效果

getWindow().getEnterTransition().addListener(new TransitionAdapter() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onTransitionEnd(Transition transition) {
                    ObjectAnimator color = ObjectAnimator.ofArgb(bookPage.getDrawable(), "tint",
                            getResources().getColor(R.color.photo_tint), 0);
                    color.start();
                    renewButton.animate().scaleX(1.0f);
                    renewButton.animate().scaleY(1.0f);
                    renewButton.animate().alpha(1.0f);
                    getWindow().getEnterTransition().removeListener(this);
                }
            });

關(guān)閉Activity時(shí)同樣可以使用,需要重寫(xiě)onBackPressed()方法:

@Override
    public void onBackPressed() {
        super.onBackPressed();
        if (Build.VERSION.SDK_INT == 21) {
            ObjectAnimator color = ObjectAnimator.ofArgb(bookPage.getDrawable(), "tint",
                    0, getResources().getColor(R.color.photo_tint));
            color.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    finishAfterTransition();
                }
            });
            color.start();
            renewButton.animate().scaleX(0.0f);
            renewButton.animate().scaleY(0.0f);
            renewButton.animate().alpha(0.0f);
            finishAfterTransition();
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容