日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android动画笔记二

發(fā)布時(shí)間:2023/11/29 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android动画笔记二 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
從android3.0,系統(tǒng)提供了一個(gè)新的動(dòng)畫-property animation, 為什么系統(tǒng)會(huì)提供這樣一個(gè)全新的動(dòng)畫包呢,先來看看之前的補(bǔ)間動(dòng)畫都有什么缺陷吧

1、傳統(tǒng)的補(bǔ)間動(dòng)畫都是固定的編碼,功能是固定的,擴(kuò)展難度大。比如傳統(tǒng)動(dòng)畫只能實(shí)現(xiàn)移動(dòng)、縮放、旋轉(zhuǎn)、淡入淡出,四種效果,如果去改變view的背景,就只能自己去實(shí)現(xiàn)。
2、補(bǔ)間動(dòng)畫只是改變了view的顯示效果,不會(huì)真正改變view的屬性,比如一個(gè)動(dòng)畫的點(diǎn)擊時(shí)間,view移動(dòng)到另一個(gè)地方,它的監(jiān)聽事件還在換來的地方。
3、傳統(tǒng)動(dòng)畫不能對(duì)非view的對(duì)象進(jìn)行動(dòng)畫操作。
這估計(jì)就是property animation出現(xiàn)的原因了吧,說了這些來看看android.animation里面都有什么東西吧

ValueAnimator
ValueAnimator是整個(gè)動(dòng)畫機(jī)制當(dāng)中最核心的一個(gè)類,屬性動(dòng)畫中主要的時(shí)序引擎,如動(dòng)畫的時(shí)間,開始、結(jié)束屬性值,ValueAnimator還負(fù)責(zé)管理動(dòng)畫的播放次數(shù),播放模式,以及對(duì)動(dòng)畫設(shè)計(jì)監(jiān)聽器。

ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); anim.setDuration(300); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float currentValue = (float) animation.getAnimatedValue(); Log.d("TAG", "cuurent value is " + currentValue); } }); anim.start();

ObjectAnimator
允許你指定要進(jìn)行動(dòng)畫的對(duì)象以及該對(duì)象的一個(gè)屬性。該類會(huì)根據(jù)計(jì)算得到的新值自動(dòng)更新屬性。ValueAnimator是對(duì)值進(jìn)行了一個(gè)平滑的動(dòng)畫過渡,而objectAnimator則可以直接對(duì)任意對(duì)象的屬性進(jìn)行動(dòng)畫操作。
fter(Animator anim) 將現(xiàn)有動(dòng)畫插入到傳入的動(dòng)畫之后執(zhí)行
after(long delay) 將現(xiàn)有動(dòng)畫延遲指定毫秒后執(zhí)行
before(Animator anim) 將現(xiàn)有動(dòng)畫插入到傳入的動(dòng)畫之前執(zhí)行
with(Animator anim) 將現(xiàn)有動(dòng)畫和傳入的動(dòng)畫同時(shí)執(zhí)行

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f); animator.setDuration(5000); animator.start();

AnimatorSet
使動(dòng)畫組合到一起,并可設(shè)置組中動(dòng)畫的時(shí)序關(guān)系,如同時(shí)播放,有序播放或延遲播放。

ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f); ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f); ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f); AnimatorSet animSet = new AnimatorSet(); animSet.play(rotate).with(fadeInOut).after(moveIn); animSet.setDuration(5000); animSet.start();

Interpolator
先看一下TimeInterpolator接口的定義,

public interface TimeInterpolator { /** * Maps a value representing the elapsed fraction of an animation to a value that represents * the interpolated fraction. This interpolated value is then multiplied by the change in * value of an animation to derive the animated value at the current elapsed animation time. * * @param input A value between 0 and 1.0 indicating our current point * in the animation where 0 represents the start and 1.0 represents * the end * @return The interpolation value. This value can be more than 1.0 for * interpolators which overshoot their targets, or less than 0 for * interpolators that undershoot their targets. */ float getInterpolation(float input); }

getInterpolation()方法中接收一個(gè)input參數(shù),這個(gè)參數(shù)的值會(huì)隨著動(dòng)畫的運(yùn)行而不斷變化,不過它的變化是非常有規(guī)律的,就是根據(jù)設(shè)定的動(dòng)畫時(shí)長(zhǎng)勻速增加,變化范圍是0到1。動(dòng)畫一開始input的值是0, 結(jié)束時(shí)input的值是1, 而中間值則是隨著動(dòng)畫運(yùn)行的時(shí)長(zhǎng)在0到1之間變化。
ViewPropertyAnimator
為了方便對(duì)view的動(dòng)畫操作,在android3.1補(bǔ)充了ViewPropertyAnimator這個(gè)機(jī)制。

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f); ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start(); myView.animate().x(50f).y(100f);

為ViewGroup添加布局動(dòng)畫
可以在ViewGroup內(nèi),通過LayoutTransition類為布局的變化添加動(dòng)畫。
當(dāng)一個(gè)ViewGroup中添加或者移除某一個(gè)item,或者調(diào)用了View的setVisibility方法,使得View 變得VISIBLE或者GONE的時(shí)候,在ViewGroup內(nèi)部的View可以完成出現(xiàn)或者消失的動(dòng)畫。當(dāng)你添加或者移除View的時(shí)候,那些剩余的View也可以通過動(dòng)畫的方式移動(dòng)到自己的新位置。你可以通過setAnimator()方法并傳遞一個(gè)Animator對(duì)象,在LayoutTransition內(nèi)部定義以下動(dòng)畫。以下是幾種事件類型的常量:

APPEARING        為那些添加到父元素中的元素應(yīng)用動(dòng)畫
CHANGE_APPEARING    為那些由于父元素添加了新的item而受影響的item應(yīng)用動(dòng)畫
DISAPPEARING       為那些從父布局中消失的item應(yīng)用動(dòng)畫
CHANGE_DISAPPEARING  為那些由于某個(gè)item從父元素中消失而受影響的item應(yīng)用動(dòng)畫

你可以為這四種事件定義自己的交互動(dòng)畫,或者僅僅告訴動(dòng)畫系統(tǒng)使用默認(rèn)的動(dòng)畫。
API Demos中的LayoutAnimations sample向你展示了如何為布局轉(zhuǎn)換定義一個(gè)布局動(dòng)畫,然后將該動(dòng)畫設(shè)置到目標(biāo)View對(duì)象上
Keyframes
由一個(gè)鍵值對(duì)組成,可以為動(dòng)畫定義某一特定時(shí)間的特定狀態(tài)。每個(gè)keyframe可以擁有自己的插值器,用于控制前一幀和當(dāng)前幀的時(shí)間間隔間內(nèi)的動(dòng)畫。第一個(gè)參數(shù)為:要執(zhí)行該幀動(dòng)畫的時(shí)間節(jié)點(diǎn)(elapsed time / duration),第二個(gè)參數(shù)為屬性值。

Keyframe kf0 = Keyframe.ofFloat(0f, 0f); Keyframe kf1 = Keyframe.ofFloat(.5f, 360f); Keyframe kf2 = Keyframe.ofFloat(1f, 0f); PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(“rotation”, kf0, kf1, kf2);//動(dòng)畫屬性名,可變參數(shù) ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation) rotationAnim.setDuration(5000ms);

總結(jié)

以上是生活随笔為你收集整理的android动画笔记二的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。