Android中属性动画Property Animation使用示例(二)
发布日期:2021-06-30 11:14:49
浏览次数:2
分类:技术文章
本文共 2175 字,大约阅读时间需要 7 分钟。
MainActivity如下:
package cc.cn;import android.animation.ValueAnimator;import android.animation.ValueAnimator.AnimatorUpdateListener;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.MarginLayoutParams;import android.widget.Button;/** * Demo描述: * 属性动画中ValueAnimator使用实例 * 利用ValueAnimator使控件发生位移(变化Margin值) * * ValueAnimator是Property Animation很重要的一个类 * ValueAnimator可跟踪记录对象属性已经变化的时间及当前这个时间点的值 * * 参考资料: * 1 http://blog.csdn.net/linmiansheng/article/details/18716841 * 2 http://blog.csdn.net/linmiansheng/article/details/18763987 * 3 关于属性动画的中文文档,请参见: * http://blog.csdn.net/think_soft/article/details/7703684 * http://wiki.eoeandroid.com/Property_Animation * Thank you very much * */public class MainActivity extends Activity { private Button mValueAnimatorButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mValueAnimatorButton=(Button) findViewById(R.id.valueAnimatorButton); //ValueAnimator.ofInt(int,int)设定了值的变化范围 final ValueAnimator valueAnimator=ValueAnimator.ofInt(20,100); //利用AnimatorUpdateListener监听动画的执行 valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator va) { //得到动画当前执行的值 //这些值均在(20,100)之间 Integer animatedValue = (Integer)va.getAnimatedValue(); System.out.println("animatedValue="+animatedValue); MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mValueAnimatorButton.getLayoutParams(); marginLayoutParams.leftMargin = animatedValue; //不断修改Button的Margin值 mValueAnimatorButton.setLayoutParams(marginLayoutParams); } }); valueAnimator.setDuration(2000); //valueAnimator.setRepeatCount(1); //valueAnimator.setRepeatMode(ValueAnimator.REVERSE); valueAnimator.setTarget(mValueAnimatorButton); mValueAnimatorButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { valueAnimator.start(); } }); }}main.xml如下:
转载地址:https://it9527.blog.csdn.net/article/details/23354621 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月18日 01时00分15秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
shader入门精要3
2019-04-30
刷一下leetcode算法题,寻找两个正序数组的中位数
2019-04-30
2021-03-12
2019-04-30
戴森球计划这个游戏牛逼
2019-04-30
Redis 学习part one
2019-04-30
Bitcoin比特币源码解读——网络协议
2019-04-30
python+opencv学习(一)
2019-04-30
python 基于Opencv图像对比
2019-04-30
python +pytest 最强自动化
2019-04-30
python之json和dick数据类型
2019-04-30
python 类和方法,继承
2019-04-30
完全背包
2019-04-30
struct与class关键字的选择
2019-04-30
LeetCode-206:反转链表
2019-04-30
计算机网络(一)—— 概述(3、4):三种交换方式、计算机网络的定义和分类
2019-04-30
计算机网络(一)—— 概述(5):计算机网络的性能指标
2019-04-30
计算机网络(一)—— 概述(6):计算机网络体系结构
2019-04-30
计算机网络(一)—— 概述(7):总结
2019-04-30