利用属性动画将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);

动画文件 scalexanimator.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:propertyName="scaleX"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>

方式二

    //------>以下为利用属性动画将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();
}

一些文件

alphaanimator.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:propertyName="alpha"
android:duration="2500"
android:valueFrom="1.0"
android:valueTo="0"
android:repeatCount="1"
android:repeatMode="reverse">
</objectAnimator>

coloranimation.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="backgroundColor"
    android:duration="5000"
    android:valueFrom="#ff0033"
    android:valueTo="#000099"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueType="intType">
</objectAnimator>

setanimator.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:ordering="together" >

<objectAnimator
android:duration="3000"
android:propertyName="scaleX"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>

<objectAnimator
android:duration="3000"
android:propertyName="scaleY"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>

</set>

translateranimatior.xml

 <?xml version="1.0" encoding="utf-8"?>
<objectAnimator 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:propertyName="translationX"
android:duration="2000"
android:valueFrom="0"
android:valueTo="150"
android:repeatCount="1"
android:repeatMode="reverse">
</objectAnimator>

10/10/2015 10:16:44 AM

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值