属性动画: ViewPropertyAnimator
ViewPropertyAnimator = view.animate()
示例:
imageView.animate()
.translationX(200.dp)
.translationY(200.dp)
.startDelay = 1000
改变view的属性值,大小 位置 偏移等,限制比较多
ObjectAnimator
ofFloat(target ,name,values):
java则需要在view类中声明name值的方法 不然会报错,kotlin则自动生成
kotlin:
var radius = 50.dp
set(value) {
field = value
invalidate()
}
java
public void setRadius(float value){
radius = value;
invalidate();
}
如果此view改变需要进行invalidate 或者postinvalidate
canvas.withSave{} 保存变更
前后台切换时,每次进入页面可能会被重新绘制,也就是调用onDraw ,需要在应用前 save , 比如
camera.save()
//
camera.restore()
AnimatorSet 可以操作多个属性动画进行合成
示例1:
val animateSet = AnimatorSet()
animateSet.playSequentially(bottomAnima,flipAnima,topAnima)
animateSet.start()
PropertyValuesHolder:将多个属性拼接在一起 示例:
val bottomHolder = PropertyValuesHolder.ofFloat("bottomFlip",60f)
KeyFrame: 关键帧
用法:
val keyFra = Keyframe.ofFloat(0f,0f)
val keyFra2 = Keyframe.ofFloat(0.2f,1.5f * LENGTH)
val keyFra3 = Keyframe.ofFloat(0.8f,0.6f * LENGTH)
val keyFra4 = Keyframe.ofFloat(1f,1f * LENGTH)
val keyFrameHolder = PropertyValuesHolder.ofKeyframe("translationX",keyFra,keyFra2,keyFra3,keyFra4)
val objectAni = ObjectAnimator.ofPropertyValuesHolder(imageView,keyFrameHolder)
objectAni.startDelay = 1000
objectAni.duration = 2000
objectAni.start()
Interpolator 从时间完成度到动画完成度
示例
val objectAni = ObjectAnimator.ofFloat() objectAni.interpolator = AccelerateInterpolator()
常用差值器:
AccelerateInterpolator 加速差值器
AccelerateDecelerateInterpolator 加减速差值器
LinearInterpolator 匀速差值器
TypeEvaluator:
源码:
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.animation;
/**
* This evaluator can be used to perform type interpolation between <code>float</code> values.
*/
public class FloatEvaluator implements TypeEvaluator<Number> {
/**
* This function returns the result of linearly interpolating the start and end values, with
* <code>fraction</code> representing the proportion between the start and end values. The
* calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,
* where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
* and <code>t</code> is <code>fraction</code>.
*
* @param fraction The fraction from the starting to the ending values
* @param startValue The start value; should be of type <code>float</code> or
* <code>Float</code>
* @param endValue The end value; should be of type <code>float</code> or <code>Float</code>
* @return A linear interpolation between the start and end values, given the
* <code>fraction</code> parameter.
*/
public Float evaluate(float fraction, Number startValue, Number endValue) {
float startFloat = startValue.floatValue();
return startFloat + fraction * (endValue.floatValue() - startFloat);
}
}
TypeEvaluator
示例:
class ProVinceEvaluator : TypeEvaluator<String> {
override fun evaluate(fraction: Float, startValue: String, endValue: String): String {
val startIndex = proVince.indexOf(startValue)
val endIndex = proVince.indexOf(endValue)
val currentIndex = startIndex + ((endIndex - startIndex) * fraction).toInt()
return proVince[currentIndex]
}
val proVinceView = findViewById<ProVince>(R.id.province)
val animator = ObjectAnimator.ofObject(proVinceView,"current", ProVinceEvaluator(),"台湾省")
animator.startDelay = 1000
animator.duration = 10000
animator.start()
Listeners: 动画监听器
ValueAnimator : 最基本的动画
硬件加速:
软件绘制: CPU------->View
硬件绘制:CPU-------->GPU
硬件加速缺点:兼容性不如软件绘制
离屏缓冲:例如:canvas.savelayer 可以用hard layer代替 ,setLayerType设置View的离屏缓冲
,不可写在onDraw方法中
setLayerType()文章来源:https://www.toymoban.com/news/detail-705252.html
LAYER_TYPE_HARDWARE 开启View离屏缓冲,使用硬件绘制
LAYER_TYPE_SOFTWARE 开启View的离屏缓冲,使用软件绘制
LAYER_TYPE_NONE 不使用离屏缓冲
view.withLayer 硬件绘制 需要开启离屏缓冲 ps:需要view自带的属性才有效果文章来源地址https://www.toymoban.com/news/detail-705252.html
到了这里,关于Android 动画的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!