. Android 补间动画 alpha、scale、translate、rotate、set的xml属性及用法

Android的animation由四种类型组成:alpha、scale、translate、rotate

 

2、动作文件存放位置

动作定义文件应该存放在res/anim文件夹下,访问时采用R.anim.XXX.xml的方式,位置如图:

 

二、scale标签——调节尺寸

 

1、自有属性

 

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,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。(具体意义,后面会举例演示)

android:pivotY 缩放起点Y轴坐标,取值及意义跟android:pivotX一样。

--------------------- 

1、新建工程、新建scale动画文件(scaleanim.xml)

 

新建一个工程,并且在res文件夹下,新建一个anim文件夹,然后再新建一个scaleanim.xml文件,结构如图所示:

 

scaleanim.xml的代码为:(从TextView中心点,从0放大到1.4倍,反复一次,最后还原到初始化状态)

 

    <?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="0.0"
        android:toXScale="1.4"
        android:fromYScale="0.0"
        android:toYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="700"
        android:fillBefore="true"
        android:repeatCount="1"
        android:repeatMode="restart"
        />

 

2、XML布局文件

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.harvic.animation_demo.MainActivity" >

<Button android:id="@+id/btn_animation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="scale animation"/>
<TextView
        android:id="@+id/tv"
                android:layout_width="100dip"
                android:layout_height="200dip"
                android:background="#ff00ff"
                android:text="@string/hello_world"
                android:layout_gravity="center_horizontal"/>

</LinearLayout>

 

3、JAVA代码

--------------------- 

 

public class MainActivity extends Activity {

    Button scaleBtn    ;
    Animation scaleAnimation;

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scaleanim);
        scaleBtn = (Button)findViewById(R.id.btn_animation);
        tv =(TextView)findViewById(R.id.tv);

        scaleBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tv.startAnimation(scaleAnimation);
            }
    

 

 

 

(1)通过scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scaleanim);从XML文件中获取动画

(2)利用startAnimation将动画传递给指定控件显示。

 

 

补间动画也可以带插值器的

 

    <?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.4"
        android:fromYScale="0.0"
        android:toYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="700"
        android:fillAfter="true"
        />

 代码生成alpha、scale、translate、rotate、set替代xml中的补间动画

 

三、ScaleAnimation

这是scale标签对应的类,官方SDK页面为:《ScaleAnimation》

在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,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。(具体意义,后面会举例演示)

android:pivotY 缩放起点Y轴坐标,取值及意义跟android:pivotX一样。

放到代码中,ScaleAnimation有下面几个构造函数:

ScaleAnimation(Context context, AttributeSet attrs) 从XML文件加载动画,基本用不到

ScaleAnimation(float fromX, float toX, float fromY, float toY)

ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)

ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

第一个构造函数是从本地XML文件加载动画,基本用不到的,我们主要看下面三个构造函数。

在标签属性android:pivotX中有三种取值,数,百分数,百分数p;体现在构造函数中,就是最后一个构造函数的pivotXType,它的取值有三个,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;

 

这三个构造函数难度不大,就不再细讲,举个例子说明:

 

在第一篇中Scale的例子的XML代码为:

 

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXScale="0.0"

    android:toXScale="1.4"

    android:fromYScale="0.0"

    android:toYScale="1.4"

    android:pivotX="50"

    android:pivotY="50"

    android:duration="700" />

对应的代码构造代码为:

 

scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

scaleAnim.setDuration(700);

在控件使用的时候,同样是使用:

tv.startAnimation(scaleAnim);

--------------------- 

带插值器的java的补间动画

ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

interpolateScaleAnim.setInterpolator(new BounceInterpolator());

interpolateScaleAnim.setDuration(3000);

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值