Android中属性动画Property Animation使用示例(四)

简介: MainActivity如下: package cc.cn;import android.animation.AnimatorInflater;import android.

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如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 <Button
        android:id="@+id/scaleXFirstButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dip"
        android:text="scaleXFirstButton" />
    
   <Button
        android:id="@+id/scaleXSecondButton"
        android:layout_width="wrap_content"
        android:layout_marginLeft="100dip"
        android:layout_height="wrap_content"
        android:text="scaleXSecondButton" />
   
   <Button
        android:id="@+id/scaleXThirdButton"
        android:layout_width="250dip"
        android:layout_marginLeft="100dip"
        android:layout_height="wrap_content"
        android:text="scaleXThirdButton" />
   
   <Button
        android:id="@+id/scaleXFourthButton"
        android:layout_width="250dip"
        android:layout_marginLeft="100dip"
        android:layout_height="wrap_content"
        android:text="scaleXFourthButton" />

</LinearLayout>

scalexanimator.xml如下:

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


目录
打赏
0
0
0
0
24
分享
相关文章
OpenCV 安卓编程示例:1~6 全
OpenCV 安卓编程示例:1~6 全
188 0
Android平台GB28181设备接入侧音频采集推送示例
GB/T28181是广泛应用于视频监控行业的标准协议规范,可以在不同设备之间实现互联互通。今天我们主要探讨Android平台的Audio采集部分。
153 1
Android kotlin MVVM 架构简单示例入门
Android kotlin MVVM 架构简单示例入门
67 1
android animation clipToPadding clipChildren
android animation clipToPadding clipChildren
技术经验分享:Android编译命令m、mm、mmm区别及工程搭建示例
技术经验分享:Android编译命令m、mm、mmm区别及工程搭建示例
555 0
|
10月前
|
Android 高通平台集成无源码apk示例
Android 高通平台集成无源码apk示例
142 0
Android App开发中集合动画和属性动画的讲解及实战演示(附源码 简单易懂 可直接使用)
Android App开发中集合动画和属性动画的讲解及实战演示(附源码 简单易懂 可直接使用)
97 0
Android App开发中集合动画和属性动画的讲解及实战演示(附源码 简单易懂 可直接使用)
Android动画——属性动画
在属性动画中,常用到的API有ValueAnimator,ObjectAnimator。 ValueAnimator:时间引擎,负责计算各个帧的属性值,基本上其他属性动画都会直接或间接继承它; ObjectAnimator: ValueAnimator 的子类,对指定对象的属性执行动画。
206 0

热门文章

最新文章

  • 1
    Android历史版本与APK文件结构
    16
  • 2
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    10
  • 3
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    15
  • 4
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    23
  • 5
    【03】微信支付商户申请下户到配置完整流程-微信开放平台创建APP应用-填写上传基础资料-生成安卓证书-获取Apk签名-申请+配置完整流程-优雅草卓伊凡
    60
  • 6
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    5
  • 7
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    8
  • 8
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    3
  • 9
    Android实战经验之Kotlin中快速实现MVI架构
    6
  • 10
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    5
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等