android属性动画不流畅,Android动画之属性动画

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程

为了更好的演示,写了一个Demo,xml界面如下(最后有源码)

a94f825c714cd155b0ad430a2a319baa.png

要介绍的有:

1.透明动画:alpha

2.位移动画:translationX,translationY

3.旋转动画:rotation

4.缩放动画:scaleX,scaleY

5.组合显示:AnimatorSet(动画集合容器)

1.透明动画:alpha

15e61480d90f19f62138270f5e0f09a7.png

2.位移动画:translationX,translationY

2c52c6f577e933e6dcff384c4ba2d7ab.png

3.旋转动画:rotation

fc2e5c11d65a13d2bff1cae407f4c3ef.png

4…缩放动画:scaleX,scaleY

2c3a575eec8abc97ac8dfdde403da34f.png

5.组合显示:AnimatorSet(动画集合容器)

5c6a33ded3b37bcd240362db49ee736d.png

源码如下:

activity_third.xml文件:

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".ThirdActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/btn_alpha"

android:layout_width="0dp"

android:layout_height="40dp"

android:layout_weight="1"

android:text="透明动画" />

android:id="@+id/btn_translate"

android:layout_width="0dp"

android:layout_height="40dp"

android:layout_weight="1"

android:text="位移动画" />

android:id="@+id/btn_rotate"

android:layout_width="0dp"

android:layout_height="40dp"

android:layout_weight="1"

android:text="旋转动画" />

android:id="@+id/btn_scale"

android:layout_width="0dp"

android:layout_height="40dp"

android:layout_weight="1"

android:text="缩放动画" />

android:id="@+id/iv_show"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:src="@mipmap/ic_launcher" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:onClick="groupshow"

android:text="组合显示" />

ThirdActivity.java文件:

//属性动画

public class ThirdActivity extends AppCompatActivity implements View.OnClickListener {

private Button btn_alpha;

private Button btn_translate;

private Button btn_rotate;

private Button btn_scale;

private ImageView iv_show;

ObjectAnimator objectAnimator;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_third);

initView();

}

private void initView() {

btn_alpha = (Button) findViewById(R.id.btn_alpha);

btn_translate = (Button) findViewById(R.id.btn_translate);

btn_rotate = (Button) findViewById(R.id.btn_rotate);

btn_scale = (Button) findViewById(R.id.btn_scale);

iv_show = (ImageView) findViewById(R.id.iv_show);

btn_alpha.setOnClickListener(this);

btn_translate.setOnClickListener(this);

btn_rotate.setOnClickListener(this);

btn_scale.setOnClickListener(this);

}

@Override

public void onClick(View v) {

//ofFloat:三个参数 :1.受到动画影响的对象(UI控件)2. 要执行的动画类型 3. 一组动画的属性

switch (v.getId()) {

case R.id.btn_alpha://透明动画

objectAnimator = ObjectAnimator.ofFloat(iv_show, "alpha", 0.5f, 1f, 0.5f, 1f);

break;

case R.id.btn_translate://位移动画

//只会执行一个

objectAnimator = ObjectAnimator.ofFloat(iv_show, "translationX", 0, 200);

//objectAnimator=ObjectAnimator.ofFloat(iv_show,"translationY",0,200);

break;

case R.id.btn_rotate://旋转动画

objectAnimator = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);

break;

case R.id.btn_scale://缩放动画

//只会执行一个

objectAnimator = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 3f, 4f);

// objectAnimator=ObjectAnimator.ofFloat(iv_show,"scaleX",1f,2f,3f,4f);

break;

}

//动画持续时间

objectAnimator.setDuration(3000);

//启动动画

objectAnimator.start();

}

public void groupshow(View view) {//组合

ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv_show, "scaleY", 1f, 2f, 1f, 2f);

ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(iv_show, "scaleX", 1f, 2f, 1f, 2f);

ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(iv_show, "rotation", 0, 90f, 180f, 90f, 45f, 100f);

//创建属性动画的集合容器

AnimatorSet animatorSet = new AnimatorSet();

//同时播放

animatorSet.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3);

//按照顺序播放

// animatorSet.play(objectAnimator1).after(objectAnimator2).after(objectAnimator3);

/*

* 另一种方式

List list=new ArrayList<>();

list.add(objectAnimator1);

list.add(objectAnimator2);

list.add(objectAnimator3);

animatorSet.playSequentially(list);//按照顺序播放

animatorSet.playTogether(list);//同时执行

*/

//设置时长

animatorSet.setDuration(3000);

//启动

animatorSet.start();

}

}

本文同步分享在 博客“计蒙不吃鱼”(CSDN)。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值