• Android 动画


    属性动画: ViewPropertyAnimator

    ViewPropertyAnimator  = view.animate() 

    示例:

    1. imageView.animate()
    2. .translationX(200.dp)
    3. .translationY(200.dp)
    4. .startDelay = 1000

    改变view的属性值,大小 位置 偏移等,限制比较多

    ObjectAnimator 

    ofFloat(target ,name,values):  

    java则需要在view类中声明name值的方法 不然会报错,kotlin则自动生成

    kotlin:

    1. var radius = 50.dp
    2. set(value) {
    3. field = value
    4. invalidate()
    5. }

    java

    1. public void setRadius(float value){
    2. radius = value;
    3. invalidate();
    4. }

    如果此view改变需要进行invalidate 或者postinvalidate

    canvas.withSave{} 保存变更

    前后台切换时,每次进入页面可能会被重新绘制,也就是调用onDraw ,需要在应用前 save , 比如
    camera.save()

    //

    camera.restore()

    AnimatorSet 可以操作多个属性动画进行合成

    示例1:

    1. val animateSet = AnimatorSet()
    2. animateSet.playSequentially(bottomAnima,flipAnima,topAnima)
    3. animateSet.start()
    PropertyValuesHolder:将多个属性拼接在一起
    示例:
    
    
    
    val bottomHolder = PropertyValuesHolder.ofFloat("bottomFlip",60f)

     KeyFrame: 关键帧

    用法:

    1. val keyFra = Keyframe.ofFloat(0f,0f)
    2. val keyFra2 = Keyframe.ofFloat(0.2f,1.5f * LENGTH)
    3. val keyFra3 = Keyframe.ofFloat(0.8f,0.6f * LENGTH)
    4. val keyFra4 = Keyframe.ofFloat(1f,1f * LENGTH)
    5. val keyFrameHolder = PropertyValuesHolder.ofKeyframe("translationX",keyFra,keyFra2,keyFra3,keyFra4)
    6. val objectAni = ObjectAnimator.ofPropertyValuesHolder(imageView,keyFrameHolder)
    7. objectAni.startDelay = 1000
    8. objectAni.duration = 2000
    9. objectAni.start()

    Interpolator 从时间完成度到动画完成度

    示例

    val objectAni = ObjectAnimator.ofFloat()
    objectAni.interpolator = AccelerateInterpolator()

    常用差值器:

    AccelerateInterpolator 加速差值器

    AccelerateDecelerateInterpolator 加减速差值器

    LinearInterpolator 匀速差值器

    TypeEvaluator:

    源码:

    1. /*
    2. * Copyright (C) 2010 The Android Open Source Project
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package android.animation;
    17. /**
    18. * This evaluator can be used to perform type interpolation between float values.
    19. */
    20. public class FloatEvaluator implements TypeEvaluator<Number> {
    21. /**
    22. * This function returns the result of linearly interpolating the start and end values, with
    23. * fraction representing the proportion between the start and end values. The
    24. * calculation is a simple parametric calculation: result = x0 + t * (x1 - x0),
    25. * where x0 is startValue, x1 is endValue,
    26. * and t is fraction.
    27. *
    28. * @param fraction The fraction from the starting to the ending values
    29. * @param startValue The start value; should be of type float or
    30. * Float
    31. * @param endValue The end value; should be of type float or Float
    32. * @return A linear interpolation between the start and end values, given the
    33. * fraction parameter.
    34. */
    35. public Float evaluate(float fraction, Number startValue, Number endValue) {
    36. float startFloat = startValue.floatValue();
    37. return startFloat + fraction * (endValue.floatValue() - startFloat);
    38. }
    39. }

    TypeEvaluator

    示例:

    1. class ProVinceEvaluator : TypeEvaluator<String> {
    2. override fun evaluate(fraction: Float, startValue: String, endValue: String): String {
    3. val startIndex = proVince.indexOf(startValue)
    4. val endIndex = proVince.indexOf(endValue)
    5. val currentIndex = startIndex + ((endIndex - startIndex) * fraction).toInt()
    6. return proVince[currentIndex]
    7. }
    1. val proVinceView = findViewById<ProVince>(R.id.province)
    2. val animator = ObjectAnimator.ofObject(proVinceView,"current", ProVinceEvaluator(),"台湾省")
    3. animator.startDelay = 1000
    4. animator.duration = 10000
    5. animator.start()

    Listeners: 动画监听器

    ValueAnimator : 最基本的动画

    硬件加速:

    软件绘制: CPU------->View

    硬件绘制:CPU-------->GPU

    硬件加速缺点:兼容性不如软件绘制

    离屏缓冲:例如:canvas.savelayer 可以用hard layer代替        ,setLayerType设置View的离屏缓冲

    ,不可写在onDraw方法中 

    setLayerType()

    LAYER_TYPE_HARDWARE 开启View离屏缓冲,使用硬件绘制
    LAYER_TYPE_SOFTWARE 开启View的离屏缓冲,使用软件绘制
    LAYER_TYPE_NONE 不使用离屏缓冲

    view.withLayer 硬件绘制    需要开启离屏缓冲  ps:需要view自带的属性才有效果

  • 相关阅读:
    C++:重载
    2310C++子类已调用基类构造器
    纯JS电子表格控件SpreadJS正式发布v16.2——新增甘特图插件
    并列句------六级
    databinding 一篇文章就够了
    前端核武器:开源FrontendBlocks所见即所得编辑器让所有人都能做前端布局
    Sql 中常用日期转换Convert(Datetime)
    Qt MV架构-委托类
    Vue+ECharts实现可视化地图
    机器人C++库(7)Robotics Library 之防碰撞模块 rl::sg
  • 原文地址:https://blog.csdn.net/qq_29769851/article/details/132685062