动画 -- View动画 -- 缩放动画

一、概念

缩放动画,子类:ScaleAnimation,标签:<scale>
它可以使View具有放大或者缩小的动画效果。

二、实现

1. XML实现
<scale>标签常用属性如下:
android:fromXScale:起始的x方向上相对自身的缩放比例,浮点值,比如1.0表示起始时自身无变化,0.5表示缩小一倍,2.0表示放大一倍;
android:toXScale:结尾的x方向上相对自身的缩放比例,浮点值;
android:fromYScale:起始的y方向上相对自身的缩放比例,浮点值;
android:toYScale:结尾的y方向上相对自身的缩放比例,浮点值;
android:pivotX:缩放起点x轴坐标,可以是 数值、百分数、百分数p 三种样式,比如 50、50%、50%p,
50,表示在当前View的左上角x轴坐标加上50px做为起始点x轴坐标;
50%,表示在当前View的左上角x轴坐标加上自己宽度的50%做为起始点x轴坐标;
50%p,表示在当前View的左上角x轴坐标加上父控件宽度的50%做为起始点x轴坐标。
android:pivotY:缩放起点y轴坐标,取值及意义跟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. 代码实现
构造方法:
public ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
参数说明:
float fromX:起始的x方向上相对自身的缩放比例,浮点值,比如1.0表示起始时自身无变化,0.5表示缩小一倍,2.0表示放大一倍;
float toX:结尾的x方向上相对自身的缩放比例,浮点值;
float fromY:起始的y方向上相对自身的缩放比例,浮点值;
float toY:结尾的y方向上相对自身的缩放比例,浮点值;
int pivotXType:缩放起点x轴参考类型,
Animation.ABSOLUTE(绝对像素值);
Animation.RELATIVE_TO_SELF(参考自己);Animation.RELATIVE_TO_PARENT(参考父容器)。
float pivotXValue:缩放起点x轴坐标,
当pivotXType为ABSOLUTE时,当前View的左上角x轴坐标加上绝对像素值做为起始点x轴坐标;
当pivotXType为RELATIVE_TO_SELF或RELATIVE_TO_PARENT时,当前View的左上角x轴坐标加上 参考物宽度*pivotXValue 做为起始点x轴坐标。
int pivotYType:缩放起点y轴参考类型;
float pivotYValue:缩放起点y轴坐标。

//布局
<?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();
} 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,769评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,303评论 4 61
  • 《Just One Last Dance》是一首旋律优美的英语歌曲,由德国女歌手Sarah Connor与...
    周未眠阅读 342评论 0 0
  • 他搞了20多年的探险旅游,30年左右的户外运动,有人说他是昆明户外运动的“教父”;同时,他还是由世界商务策划师联合...
    语菲菲儿阅读 2,697评论 0 3
  • 夜深人静,窗外不时传来狗叫声,为原本烦躁的夏夜又增添了不少恼的气息。站在窗前,看着茫茫夜色,心中泛起一丝惆怅…… ...
    xiao嫣阅读 463评论 0 1