objectAnimation与ValueAnimation的使用

简介:

在给控件做动画的时候,我一直都是使用animation的基本动画来给控件界面添加动画,能简单肯定不会用复杂的,我记得在我刚开始的时候要做一个效果,就是要按钮移动然后结束以后固定到结束的点,我记得当时的我是通过给动画添加监听器来判断动画结束的时候,然后让动态设置控件的位置,但是随着对android接触的越来越多,我渐渐的觉得自己当时的方法是相当的笨。


                 下来就说下objectAnimation跟 ValueAnimation吧:

objectAnimation:

这个子类ValueAnimator支持动画的目标对象上的属性。这类带参数的构造函数定义的目标对象动画以及属性的名称,将动画。然后确定适当的设置/获取函数内部和动画将调用这些函数作为必要的动画属性。

         也就是这个动画可以修改view的属性,还能固定他的显示位置,那么就很简单了


ValueAnimation

这个类提供了一个简单的计时引擎运行动画动画计算值和设置在目标对象。
有一个所有动画使用的定时脉冲。它运行在一个自定义处理程序以确保属性更改在UI线程上发生。
默认情况下,ValueAnimator使用非线性插值,通过AccelerateDecelerateInterpolator类,它加速,减慢的动画。这种行为可以改变callingsetInterpolator(TimeInterpolator)。

     这个是说动画的所有属性都可以让我们控制,使用更加灵活。


其实学习这个是我在想要画一个抛物线的时候,当我画完了以后我就想抛物线的动画挺好看,然后就研究了下,总结了四种抛物线的动画实现。

        (1)

 

public static  int mCount;
    /** 
     * 整个抛物线
     * @param imageView 
     */  
    public static void startAnimation(final View view) {  
    	mCount = 300; 
        Keyframe[] keyframes = new Keyframe[mCount];  
        final float keyStep = 1f / (float) mCount;  
        float key = keyStep;  
        for (int i = 0; i < mCount; ++i) {  
            keyframes[i] = Keyframe.ofFloat(key, i+ 1);  
            key += keyStep;  
        }  
  
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("translationX", keyframes);  
        key = keyStep;  
        for (int i = 0; i < mCount; ++i) {  
            keyframes[i] = Keyframe.ofFloat(key, -getY(i + 1));  
            key += keyStep;  
        }  
  
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("translationY", keyframes);  
        ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(view, pvhY, pvhX).setDuration(3000);  
        yxBouncer.setInterpolator(new BounceInterpolator());  
        yxBouncer.start();  
    }  
 <pre name="code" class="java">   public static final float a = -1f / 75f;  
  
    /** 
     * 这里是根据三个坐标点{(0,0),(300,0),(150,300)}计算出来的抛物线方程 
     *  
     * @param x 
     * @return 
     */  
    public static  float getY(float x) {  
    	if (x>300) {
			x= 300;
		}
        return a * x * x + 4 * x;  
    }  

          (2) 

 

 /** 
     * 半个抛物线,从上向下
     * @param imageView 
     */  
    public static void starthalfAnimation(final View view) {  
    	mCount = 150 ;
        Keyframe[] keyframes = new Keyframe[mCount];  
        final float keyStep = 1f / (float) mCount;  
        float key = keyStep;  
        for (int i = 0; i < mCount; ++i) {  
            keyframes[i] = Keyframe.ofFloat(key, i+ 150);  
            key += keyStep;  
        }  
  
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("translationX", keyframes);  
        key = keyStep;  
        for (int i = 0; i < mCount; ++i) {  
            keyframes[i] = Keyframe.ofFloat(key, -getY(i + 150));  
            key += keyStep;  
        }  
  
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("translationY", keyframes);  
        ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(view, pvhY, pvhX).setDuration(3000);  
        yxBouncer.setInterpolator(new BounceInterpolator());  
        yxBouncer.start();  
    }  
   (3)
//也是抛物线
    public static void parabolaAnimation(View view){
    	PropertyValuesHolder holderY = PropertyValuesHolder.ofFloat("translationY", 0.0f,180f);
    	PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat("translationX", 0.0f,180f);
    	ObjectAnimator.ofPropertyValuesHolder(view,holderX,holderY).setDuration(2000).start();
    }
    

      (4) 
public static void ParabolaValueAnimation(final View view){
    	ValueAnimator valueAnimator = new ValueAnimator();
    	valueAnimator.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				/*ViewGroup group = (ViewGroup) view.getParent();
				if (group != null) {
					group.removeView(view);
				}*/
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				
			}
		});
    	valueAnimator.setDuration(3000);
    	valueAnimator.setObjectValues(new PointF(0, 0));
    	valueAnimator.setInterpolator(new BounceInterpolator());
    	valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
    		/**
    		 * 
    		 * @param fraction  从0。0 -  1.0
    		 * @param startValue
    		 * @param endValue
    		 * @return
    		 */
			@Override
			public PointF evaluate(float fraction, PointF startValue,
					PointF endValue) {
				PointF pointF= new PointF();
				Log.e("point", fraction+"");
				pointF.x = 200 * fraction * 3/2;
				pointF.y = 0.5f * 200 * (fraction * 3) * (fraction * 3)/2;  
				return pointF;
			}
		});
    	
    	valueAnimator.start();
    	valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				PointF pointF = (PointF)animation.getAnimatedValue();
				view.setTranslationX(pointF.x);
				view.setTranslationY(pointF.y);
			}
		});
    }
  我实验了多种动画,可以看看。

Demo:http://download.csdn.net/detail/u012808234/9095419

相关文章
|
Kotlin
Kotlin | 实现数据类(data)深拷贝
在Kotlin中,data数据类默认的copy方法实现的是浅拷贝,但我们有时候需要实现深拷贝。 在kotlin中,实现就比较容易了。
562 0
Kotlin | 实现数据类(data)深拷贝
|
Android开发
【RecyclerView】 十四、GridLayoutManager 网格布局管理器 ( GridLayoutManager.SpanSizeLookup 指定 item 元素占用网格个数 )
【RecyclerView】 十四、GridLayoutManager 网格布局管理器 ( GridLayoutManager.SpanSizeLookup 指定 item 元素占用网格个数 )
1180 0
【RecyclerView】 十四、GridLayoutManager 网格布局管理器 ( GridLayoutManager.SpanSizeLookup 指定 item 元素占用网格个数 )
|
Android开发
【错误记录】Android 应用打包错误 ( Entry name ‘assets/xxx.xml‘ collided )
【错误记录】Android 应用打包错误 ( Entry name ‘assets/xxx.xml‘ collided )
903 0
【错误记录】Android 应用打包错误 ( Entry name ‘assets/xxx.xml‘ collided )
|
Java Kotlin
Kotlin data数据类、copy()函数、sealed密封类
Kotlin data数据类、copy()函数、sealed密封类使用
125 0
|
8月前
|
存储 XML Java
SharedPreferences详解及其ANR解决方案
SharedPreferences详解及其ANR解决方案
|
11月前
|
XML Android开发 数据格式
|
Android开发
android.os.DeadObjectException的解决办法
android.os.DeadObjectException的解决办法
494 0
|
Android开发
【错误记录】Android Studio 编译报错 ( AppCompat does not support the current theme features )
【错误记录】Android Studio 编译报错 ( AppCompat does not support the current theme features )
271 0
【错误记录】Android Studio 编译报错 ( AppCompat does not support the current theme features )
|
Java
Exception in thread “main“ java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibilit
Exception in thread “main“ java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibilit
227 0
|
7天前
|
存储 安全 数据挖掘
性能30%↑|阿里云AnalyticDB*AMD EPYC,数据分析步入Next Level
第4代 AMD EPYC加持,云原生数仓AnalyticDB分析轻松提速。
性能30%↑|阿里云AnalyticDB*AMD EPYC,数据分析步入Next Level

热门文章

最新文章