一、概念
縮放動(dòng)畫,子類:ScaleAnimation,標(biāo)簽:<scale>
它可以使View具有放大或者縮小的動(dòng)畫效果。
二、實(shí)現(xiàn)
1. XML實(shí)現(xiàn)
<scale>標(biāo)簽常用屬性如下:
android:fromXScale:起始的x方向上相對(duì)自身的縮放比例,浮點(diǎn)值,比如1.0表示起始時(shí)自身無變化,0.5表示縮小一倍,2.0表示放大一倍;
android:toXScale:結(jié)尾的x方向上相對(duì)自身的縮放比例,浮點(diǎn)值;
android:fromYScale:起始的y方向上相對(duì)自身的縮放比例,浮點(diǎn)值;
android:toYScale:結(jié)尾的y方向上相對(duì)自身的縮放比例,浮點(diǎn)值;
android:pivotX:縮放起點(diǎn)x軸坐標(biāo),可以是 數(shù)值、百分?jǐn)?shù)、百分?jǐn)?shù)p 三種樣式,比如 50、50%、50%p,
50,表示在當(dāng)前View的左上角x軸坐標(biāo)加上50px做為起始點(diǎn)x軸坐標(biāo);
50%,表示在當(dāng)前View的左上角x軸坐標(biāo)加上自己寬度的50%做為起始點(diǎn)x軸坐標(biāo);
50%p,表示在當(dāng)前View的左上角x軸坐標(biāo)加上父控件寬度的50%做為起始點(diǎn)x軸坐標(biāo)。
android:pivotY:縮放起點(diǎn)y軸坐標(biāo),取值及意義跟android:pivotX一樣。
//res/anim/scale_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:pivotX="150%"
android:pivotY="150%"
android:duration="3000"
android:repeatCount="2"
/>
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500px"
android:layout_height="500px"
android:layout_marginLeft="100px"
android:background="@color/colorAccent">
<TextView
android:id="@+id/animate_tv"
android:layout_width="wrap_content"
android:layout_height="200px"
android:gravity="center"
android:layout_centerInParent="true"
android:textColor="@color/colorAccent"
android:text="Hello Android!"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
//代碼,MainActivity
private void scaleAnimationXML() {
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
mAnimate_tv.setAnimation(scaleAnimation);
mAnimate_tv.startAnimation(scaleAnimation);
}
private void stopAnimation() {
mAnimate_tv.clearAnimation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
scaleAnimationXML();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimation();
}
2. 代碼實(shí)現(xiàn)
構(gòu)造方法:
public ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
參數(shù)說明:
float fromX:起始的x方向上相對(duì)自身的縮放比例,浮點(diǎn)值,比如1.0表示起始時(shí)自身無變化,0.5表示縮小一倍,2.0表示放大一倍;
float toX:結(jié)尾的x方向上相對(duì)自身的縮放比例,浮點(diǎn)值;
float fromY:起始的y方向上相對(duì)自身的縮放比例,浮點(diǎn)值;
float toY:結(jié)尾的y方向上相對(duì)自身的縮放比例,浮點(diǎn)值;
int pivotXType:縮放起點(diǎn)x軸參考類型,
Animation.ABSOLUTE(絕對(duì)像素值);
Animation.RELATIVE_TO_SELF(參考自己);Animation.RELATIVE_TO_PARENT(參考父容器)。
float pivotXValue:縮放起點(diǎn)x軸坐標(biāo),
當(dāng)pivotXType為ABSOLUTE時(shí),當(dāng)前View的左上角x軸坐標(biāo)加上絕對(duì)像素值做為起始點(diǎn)x軸坐標(biāo);
當(dāng)pivotXType為RELATIVE_TO_SELF或RELATIVE_TO_PARENT時(shí),當(dāng)前View的左上角x軸坐標(biāo)加上 參考物寬度*pivotXValue 做為起始點(diǎn)x軸坐標(biāo)。
int pivotYType:縮放起點(diǎn)y軸參考類型;
float pivotYValue:縮放起點(diǎn)y軸坐標(biāo)。
//布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500px"
android:layout_height="500px"
android:layout_marginLeft="100px"
android:background="@color/colorAccent">
<TextView
android:id="@+id/animate_tv"
android:layout_width="wrap_content"
android:layout_height="200px"
android:gravity="center"
android:layout_centerInParent="true"
android:textColor="@color/colorAccent"
android:text="Hello Android!"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
//代碼,MainActivity
private void scaleAnimationCode() {
mAnimation = new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 1.5f);
mAnimation.setDuration(3000);
mAnimation.setRepeatCount(2);
mAnimation.setFillAfter(true);
mAnimate_tv.setAnimation(mAnimation);
mAnimate_tv.startAnimation(mAnimation);
}
private void stopAnimation() {
mAnimate_tv.clearAnimation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
scaleAnimationCode();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopAnimation();
}