最近在溫習(xí)一下舊的姿勢(知識)酝锅,順便熟練一下markdown,做個筆記。壕曼。
視圖動畫
-
透明度動畫
AlphaAnimation aa = new AlphaAnimation(0,1); aa.setDuration(1000); view.starttAnimation(aa);
-
旋轉(zhuǎn)動畫
/** * @param ①旋轉(zhuǎn)的初始角度 ②旋轉(zhuǎn)的終止角度 ③ ④ 旋轉(zhuǎn)中心的橫縱坐標(biāo),也可以用自身當(dāng)作參考系 */ RotateAnimation ra = new RotateAnimation(0,360,100,100) ra.setDuration(1000); view.startAnimation(ra);
-
位移動畫
/** *@param 始末位置的橫縱坐標(biāo) */ TranslateAnimation ta = new TranslateAnimation(0,200,0,300); ta.setDuration(1000); view.startAnimation(ta);
-
縮放動畫
/* * @param 縮放的中心點 */ ScaleAnimation sa = new ScaleAnimation(0,2,0,2); sa.setDuratuon(1000); view.startAnimation(sa);
-
動畫集合
AnimationSet as = new AnimationSet(true); as.addAnimation(animation1); as.addAnimation(animation2); ..... view.startAnimation(as)
屬性動畫
1. ObjectAnimator
/*
* @param ①操作的View ②要操縱的屬性 ③設(shè)置屬性的變化值
*/
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"translateX",300);
animator.setDuration(1000);
animator.start();
-
要操縱的屬性必須具有g(shù)et月褥、set方法,不然ObjectAnimator就無法起效
- translationX translationY
- rotationrotationX rotationY
- scaleX scaleY
- x和y
- alpha
2. PropertyValuesHolder
類似于視圖動畫中的AniamtionSet
PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat("translationY", 100f);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("ScaleX", 1f, 0, 1f);
PropertyValuesHolder sacleY = PropertyValuesHolder.ofFloat("ScaleY", 1f, 0, 1f);
ObjectAnimator.ofPropertyValuesHolder(mImage,translationY,scaleX,sacleY).setDuration(1000).start();
3.ValueAnimator
本身不提供任何動畫效果屯曹,可以在AnimatorUpdateListener中監(jiān)聽數(shù)值的變換
ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
}
});
4.AnimatorSet
ObjectAnimator translationX = ObjectAnimator.ofFloat(mImage, "translationX", 300f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImage, "ScaleX", 1f, 0, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImage, "ScaleY", 1f, 0, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(1000);
animatorSet.playTogether(translationX,scaleX,scaleY);
animatorSet.start();
5.在XML中使用動畫
<objectAnimator
android:duration="1000"
android:propertyName="scaleX"
android:valueFrom="1.0"
android:valueTo="2.0"
android:valueType="floatType"/>
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.scale_animator);
animator.setTarget(mImage);
animator.start();
6.View的animate方法
mImage.animate()
.alpha(1) //設(shè)置透明度
.y(300) //設(shè)置 坐標(biāo)
.setDuration(1000) //設(shè)置動畫時長
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
.withEndAction(new Runnable() {
@Override
public void run() {
}
})
.start();
布局動畫
所謂的布局動畫是指在ViewGroup上狱庇,給ViewGroup增加View時添加一個動畫過渡效果
Android:animateLayoutChanges = "true"
mViewGroup = (LinearLayout) findViewById(R.id.imagegroup);
//設(shè)置過渡動畫
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1);
scaleAnimation.setDuration(2000);
//設(shè)置布局動畫的顯示屬性
LayoutAnimationController lac = new LayoutAnimationController(scaleAnimation, 0.5f);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
//給ViewGroup設(shè)置布局動畫
mViewGroup.setLayoutAnimation(lac);
-
LayoutAnimationController.ORDER_NORMAL
——順序 -
LayoutAnimationController.ORDER_RANDOM
——隨機(jī) -
LayoutAnimationController.ORDER_REVERSE
——反序
Interpolators(插值器)
定義動畫的變換速率,類似于物理中的加速度
android:interpolator="@android:anim/accelerate_interpolator" 設(shè)置動畫為加速動畫(動畫播放中越來越快)
android:interpolator="@android:anim/decelerate_interpolator" 設(shè)置動畫為減速動畫(動畫播放中越來越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 設(shè)置動畫為先加速在減速(開始速度最快 逐漸減慢)
android:interpolator="@android:anim/anticipate_interpolator" 先反向執(zhí)行一段恶耽,然后再加速反向回來(相當(dāng)于我們彈簧密任,先反向壓縮一小段,然后在加速彈出)
android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段偷俭,然后加速反向回來浪讳,執(zhí)行完畢自帶回彈效果(更形象的彈簧效果)
android:interpolator="@android:anim/bounce_interpolator" 執(zhí)行完畢之后會回彈跳躍幾段(相當(dāng)于我們高空掉下一顆皮球,到地面是會跳動幾下)
android:interpolator="@android:anim/cycle_interpolator" 循環(huán)涌萤,動畫循環(huán)一定次數(shù)淹遵,值的改變?yōu)橐徽液瘮?shù):Math.sin(2* mCycles* Math.PI* input)
android:interpolator="@android:anim/linear_interpolator" 線性均勻改變
android:interpolator="@android:anim/overshoot_interpolator" 加速執(zhí)行,結(jié)束之后回彈
自定義動畫
繼承Animation 重寫initialize () 以及 applyTransformation()方法
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
//設(shè)置默認(rèn)時長
setDuration(5000);
//動畫結(jié)束后保留狀態(tài)
setFillAfter(true);
//設(shè)置默認(rèn)的插值器
setInterpolator(new BounceInterpolator());
this.mCenterWidth = width/2;
this.mCenterHeight = height/2;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
this.mCamera = new Camera();
Matrix matrix = t.getMatrix();
mCamera.save();
//使用Camera設(shè)置旋轉(zhuǎn)的角度
mCamera.rotateX(20*interpolatedTime);
//將旋轉(zhuǎn)變換作用到Metrix上
mCamera.getMatrix(matrix);
mCamera.restore();
//通過pre 方法設(shè)置矩陣作用前的的偏移量來改變旋轉(zhuǎn)中心
matrix.preTranslate(mCenterWidth,mCenterHeight);
matrix.postTranslate(-mCenterWidth,-mCenterHeight);
}
SVG (Scalable Vector Graphics ) 5.0
path
使用<path>標(biāo)簽創(chuàng)建SVG 常用的指令
- L 繪制直線
- M 移動到某一坐標(biāo)
- A 繪制弧線
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height = "200dp"
android:width = "200dp"
android:viewportHeight = "100"
android:viewportWidth = "100">
<group
android:name="test"
android:pivotX="36"
android:pivotY="36"
android:rotation="0">
<path
android:name="v"
android:fillColor="@color/colorAccent"
android:pathData="M 25 50 a 25,25 0 1,0 50,0 L 25 80"
android:strokeColor="@color/colorPrimary"
android:strokeWidth="2"
/>
</group>
</vector>
AnimatedVectorDrawable
-
在res的drawable文件夾下通過<animated-vector>標(biāo)簽來聲明對AnimatedVectorDrawable的使用形葬,并指定其作用的path或group.
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:drawable="@drawable/yuanhu" tools:targetApi="lollipop"> <target android:animation="@anim/rotate_animator" android:name="test"/> </animated-vector>
-
對應(yīng)的vector即為靜態(tài)的VectorDrawable.
注意:target 中name的屬性和vector中需要作用的name屬性保持一致
-
通過animation屬性合呐,將一個動畫作用到了對應(yīng)name的元素上,objectAnimator的代碼如下:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="4000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" /> </set>
-
將AnimatrdVectorDrawable XML 文件設(shè)置給一個ImageView 作為背景顯示
<ImageView android:id="@+id/vectorimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/animator_yuanhu"/>
-
在代碼中控制SVG動畫
mVector = (ImageView) findViewById(R.id.vectorimage); ((Animatable) mVector.getDrawable()).start();