Android中属性动画Property Animation使用示例(四)
发布日期:2021-06-30 11:14:48
浏览次数:2
分类:技术文章
本文共 4206 字,大约阅读时间需要 14 分钟。
MainActivity如下:
package cc.cn;import android.animation.AnimatorInflater;import android.animation.IntEvaluator;import android.animation.ObjectAnimator;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.widget.Button;/** * Demo描述: * 利用属性动画将Button变宽的四种方式示例 * * 参考资料: * 1 http://blog.csdn.net/singwhatiwanna/article/details/17841165 * 2 关于属性动画的中文文档,请参见: * 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 mScaleXFirstButton; private Button mScaleXSecondButton; private Button mScaleXThirdButton; private Button mScaleXFourthButton; private ObjectAnimator mObjectAnimator1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ //------>以下为利用属性动画将Button变宽的方式一 //该方式存在的问题:Button被拉伸的同时按钮中的文字亦被拉伸,效果不好. //解决方法:参见以下的方式二、三和四 mScaleXFirstButton=(Button) findViewById(R.id.scaleXFirstButton); mScaleXFirstButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mObjectAnimator1.start(); } }); mObjectAnimator1=(ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.scalexanimator); mObjectAnimator1.setTarget(mScaleXFirstButton); //------>以下为利用属性动画将Button变宽的方式二 //该方式中可将Button变宽. //但是存在一个问题: //布局文件中scaleXSecondButton宽度的设置是android:layout_width="wrap_content" //若将宽度改为一个具体的值比如250dip,那么此时是没有动画效果的. //解决办法参见方式三 mScaleXSecondButton=(Button) findViewById(R.id.scaleXSecondButton); mScaleXSecondButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ObjectAnimator.ofInt(mScaleXSecondButton, "width", 500).setDuration(2000).start(); } }); //------>以下为利用属性动画将Button变宽的方式三 //该方法解决了在方式二中的问题. //原因分析: //属性动画要求动画作用的对象提供该属性的get和set方法.即在此例中 //我们要修改的是对象的width属性.所以要有该属性对应的get和set方法 mScaleXThirdButton=(Button) findViewById(R.id.scaleXThirdButton); final ViewWrapper viewWrapper=new ViewWrapper(mScaleXThirdButton); mScaleXThirdButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ObjectAnimator.ofInt(viewWrapper, "width", 500).setDuration(2000).start(); } }); //------>以下为利用属性动画将Button变宽的方式四 //在该示例中主要采用了ValueAnimator mScaleXFourthButton=(Button) findViewById(R.id.scaleXFourthButton); mScaleXFourthButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startPropertyAnimation(mScaleXFourthButton,mScaleXFourthButton.getWidth(),500); } }); } private void startPropertyAnimation(final View target, final int startValue, final int endValue){ final IntEvaluator intEvaluator=new IntEvaluator(); //将动画值限定在(1,100)之间 ValueAnimator valueAnimator=ValueAnimator.ofInt(1,100); //动画持续时间 valueAnimator.setDuration(5000); //监听动画的执行 valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { //得到当前瞬时的动画值,在(1,100)之间 Integer currentAnimatedValue=(Integer) valueAnimator.getAnimatedValue(); //计算得到当前系数fraction float fraction=currentAnimatedValue/100f; System.out.println("currentAnimatedValue="+currentAnimatedValue+",fraction="+fraction); //评估出当前的宽度其设置 target.getLayoutParams().width=intEvaluator.evaluate(fraction, startValue, endValue); target.requestLayout(); } }); //开始动画 valueAnimator.start(); } private class ViewWrapper { private View mTargetView; public ViewWrapper(View target) { mTargetView = target; } public int getWidth() { return mTargetView.getLayoutParams().width; } public void setWidth(int width) { mTargetView.getLayoutParams().width = width; mTargetView.requestLayout(); } } }main.xml如下:
scalexanimator.xml如下:
转载地址:https://it9527.blog.csdn.net/article/details/23354223 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月27日 08时03分59秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
[转]深度剖析闪电网络
2019-04-30
听李天飞《大话西游》有感
2019-04-30
走进开源代码(三)
2019-04-30
Linux下开发Qt界面程序时命令行传参数的一个坑
2019-04-30
SourceInsight使用技巧(转)
2019-04-30
QT之旅——post 文件
2019-04-30
树莓派为连接不同Wifi分配固定IP的方法
2019-04-30
[转]Linux 下编译、安装、配置 QT
2019-04-30
新手教学看eMule 0.50a Xtreme 8.0设置
2019-04-30
如何在Linux使用Eclipse + CDT开发C/C++程序?
2019-04-30
Eclipse官网下载页面的Packages 和Developer Builds区别
2019-04-30
在CentOS 6.4安装Qt5.0.1
2019-04-30
深入浅出TCP之send和recv
2019-04-30
yum和apt-get的区别
2019-04-30
vim中文帮助的安装
2019-04-30
linux下获取所有文件夹和文件,支持nfs和xfs
2019-04-30
用分区魔术师把linux所占的分区删除后重写mbr
2019-04-30
软件架构师书籍
2019-04-30
Java程序员到架构师的推荐阅读书籍
2019-04-30
LFS、BLFS、ALFS、HLFS的区别
2019-04-30