安卓控件属性动画使用大全

首先写一个xml布局文件,用于显示效果,如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dialogtest.AnimationActivity" >

    <Button
        android:id="@+id/alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="透明度按钮" />
    <Button
        android:id="@+id/rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋转按钮" />
    <Button
        android:id="@+id/translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="平移按钮" />
    <Button
        android:id="@+id/scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大小按钮" />
    <Button
        android:id="@+id/set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多重动画按钮" />
    <Button
        android:id="@+id/prop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="属性动画按钮" />

    <ImageView
        android:id="@+id/imgList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/animation_list" />

</LinearLayout>


然后在Activity中抓取所有的按钮

                Button alphaButton = (Button) findViewById(R.id.alpha);
		Button rotateButton = (Button) findViewById(R.id.rotate);
		Button transButton = (Button) findViewById(R.id.translate);
		Button scaleButton = (Button) findViewById(R.id.scale);
		Button setButton =(Button) findViewById(R.id.set);
		Button propButton = (Button) findViewById(R.id.prop);


1、透明度动画

在Activity中写法1:

直接通过对象进行设置

Animation alpha = new AlphaAnimation(0,1);
		alpha.setDuration(5000);
		alphaButton.setAnimation(alpha);
写法2:

通过xml文件设置

	Animation alpha2 = AnimationUtils.loadAnimation(this,R.anim.animation_alpha);
		alphaButton.setAnimation(alpha2);

按照把动画都写在xml中比较清晰可靠,下面的例子全是依据xml的写法

                //旋转动画
		Animation rotate = AnimationUtils.loadAnimation(this, R.anim.animation_rotate);
		rotateButton.setAnimation(rotate);
		
		//平移动画
		Animation translate = AnimationUtils.loadAnimation(this, R.anim.animation_translate);
		transButton.setAnimation(translate);
		
		//大小动画
		Animation scale = AnimationUtils.loadAnimation(this, R.anim.animation_scale);
		scaleButton.setAnimation(scale);
		
		//多重动画
		Animation set = AnimationUtils.loadAnimation(this, R.anim.animation_set);
		setButton.setAnimation(set);
		
		//逐帧动画
		ImageView imgList = (ImageView) findViewById(R.id.imgList);
		AnimationDrawable animationDrawable = (AnimationDrawable) imgList.getDrawable();
		animationDrawable.start();
		
		//属性动画
		ObjectAnimator oa=ObjectAnimator.ofFloat(propButton, "rotationX", 0.0f, 360f);
		oa.setDuration(5000);
		oa.start();

下面是xml文件的写法

1、透明度渐变

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1" android:duration="5000">
    

</alpha>

2、旋转

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="720" android:duration="5000">
    

</rotate>

3、大小变化

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="200" android:fromYScale="0" android:toYScale="200" android:duration="5000" >
    

</scale>

4、平移

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="500" android:duration="5000">
    

</translate>

如果想要加入插补效果,比如平移到头超出一块,再弹回来,可以这样写

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="500" android:duration="5000" android:interpolator="@android:anim/overshoot_interpolator">
    

</translate>



5、多重属性变化

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
    <alpha android:fromAlpha="0" android:toAlpha="1"></alpha>
    <translate android:fromXDelta="0" android:toXDelta="200"></translate>
    

</set>

6、帧动画,注意帧动画要用到imgView

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/btn_qq_nor" android:duration="150"></item>
    <item android:drawable="@drawable/btn_qq_press" android:duration="150"></item>
    <item android:drawable="@drawable/btn_sina_nor" android:duration="150"></item>
    <item android:drawable="@drawable/btn_sina_press" android:duration="150"></item>
    <item android:drawable="@drawable/btn_weichat_nor" android:duration="150"></item>
    <item android:drawable="@drawable/btn_weichat_press" android:duration="150"></item>
    <item android:drawable="@drawable/btn_tenteweibo_nor" android:duration="150"></item>
    <item android:drawable="@drawable/btn_tenteweibo_press" android:duration="150"></item>

</animation-list>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值