记(动画小总结)
文章图片
动画.png 总的来说,动画就是以上三个分类,下面详细解释各个分类及使用方法
一.View动画
View动画指定开始和结束的动画样式,而中间如何过渡啊什么的,都是由系统带我们完成。
View动画总的来说使用较简单,先在res下新建anim文件夹
,在此文件夹中新建各个动画的样式。
View动画改变的只是View显示,而没改变View的响应区域
1.AlphaAnimation透明度动画
使用方法:
a.新建drawable_alpha.xml文件
//0.0代表完全透明
b.在activity或fragment中调用
Animation alphaAnim=AnimationUtils.loadAnimation(this,R.anim.drawable_alpha);
alphaAnim.setFillAfter(true);
//动画执行后停止在最后一秒
findViewById(R.id.ivPic).startAnimation(alphaAnim);
2.RotateAnimation旋转动画
使用方法:
a.新建drawable_alpha.xml文件
//旋转度数
b.在activity或fragment中调用
Animation rotateAnim=AnimationUtils.loadAnimation(this,R.anim.drawable_rotate);
rotateAnim.setFillAfter(true);
findViewById(R.id.ivPic).startAnimation(rotateAnim);
3.ScaleAnimation缩放动画
使用方法:
a.新建drawable_scale.xml文件
//Y的终止位置
b.在activity或fragment中调用
Animation scaleAnim=AnimationUtils.loadAnimation(this,R.anim.drawable_scale);
scaleAnim.setFillAfter(true);
findViewById(R.id.ivPic).startAnimation(scaleAnim);
4.TranslateAnimation平移动画
使用方法:
a.新建drawable_translate.xml文件
//终止位置
b.在activity或fragment中调用
Animation translateAnim=AnimationUtils.loadAnimation(this,R.anim.drawable_translate);
translateAnim.setFillAfter(true);
findViewById(R.id.ivPic).startAnimation(translateAnim);
二.属性动画 属性动画分为
ViewPropertyAnimator
和ObjectAnimator
属性动画是通过
不断改变View的属性
,从而实现动画的效果属性动画改变的
不仅是View的位置,也改变了View的响应区域
1.ViewPropertyAnimator
当我们调用 imageView.animate()返回的就是ViewPropertyAnimator
ViewPropertyAnimator只可以使用系统提供给我们的一系列方法
语法为: imageView.animate()+xxx
,后面接上是需要平移还是旋转还是缩放即可此处举个例子,比如我们想要将view移动200距离
imageView.animate().translationX(Utils.dpToPixel(200));
当然,这只是其中的一个动画效果,即移动200像素。如果想要又旋转又缩放等效果,你可以将他们组合在一起。此处举个例子,比如从透明度0到透明度,并且旋转360度且平移。
imageView.animate().alpha(1)
.translationX(Utils.dpToPixel(200))
.translationY(50)
.scaleX(1)
.scaleY(1)
.rotation(360);
ViewPropertyAnimator的监听器有两个,分别是
setListener()
和 setUpdateListener()
方法。如果想要移除监听,使用的是setListener(null)和setUpdateListener(null)2.ObjectAnimator
ObjectAnimator可以通过自定义View,而设置一些我们想要通过修改它的属性,而可以实现的动画效果
语法为: (1)设置属性的get/set方法 (2)ObjectAnimator objectAnimator = ObjectAnimator.ofxxx();
(3)objectAnimator .start()
,of后跟需要改变什么类型值就加上面,比如int,float...此处举个例子,比如我们想要设置自定义view的progress属性,而让他产生动画效果
public class Practice08ObjectAnimatorView extends View {
final float radius = dpToPixel(80);
RectF arcRectF = new RectF();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// TODO 为 progress 添加 getter 和 setter 方法(setter 方法记得加 invalidate())
float progress = 0;
public float getProgress() {
return progress;
}public void setProgress(float progress) {
this.progress = progress;
invalidate();
//一定要加,意思是设置之后,重新调用onDraw(),否则onDraw()不会重新执行
}
//省略了构造方法
{
paint.setTextSize(dpToPixel(40));
paint.setTextAlign(Paint.Align.CENTER);
}@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//关于自定义view的绘制省略
}
}
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "progress", 0, 65);
objectAnimator.setDuration(1000);
objectAnimator.start();
ViewPropertyAnimator的监听器有三个,分别是
addListener()
,addUpdateListener(),addPauseListener()
方法。如果想要移除监听,使用的是removeListener(),removeUpdateListener(),removePauseListener()3.ViewPropertyAnimator
和ObjectAnimator的通用功能
(1)
Interpolator
:速度设置器,通过设置不同的Interpolator,动画会以不同的速度模型执行。如:LinearInterpolator是线性,AccelerateInterpolator是持续加速...(2)
setDuration()
设置动画执行时长三.Drawable动画 Drawable也可以被理解为帧动画,其实就是一个一个图片累加在一起的
使用方法:
a.在drawable目录下,新建drawable_frame.xml
//执行时长ms
b.在使用的地方
ImageView imageView=findViewById(R.id.ivPic);
imageView.setImageResource(R.drawable.drawable_frame);
AnimationDrawable animationDrawable= (AnimationDrawable)
imageView.getDrawable();
animationDrawable.start();
四.对任何属性做动画的方法 1.能够给对象加上get,set方法(上面已有)
2.通过包裹一个类,增加get,set方法
举例:将B的宽扩大到500像素
//调用处
Button btStart = findViewById(R.id.btStart);
ViewWrapper viewWrapper = new ViewWrapper(btStart);
//此处get,set叫什么名,上面的ObjectAnimator.ofInt中“”里面就要对应什么名字,要一致
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(viewWrapper, "width", 300).setDuration(5000);
objectAnimator.start();
//声明处class ViewWrapper {
private View view;
public ViewWrapper(View view) {
this.view = view;
}public int getWidth() {
return view.getWidth();
}
//此处get,set叫什么名,上面的ObjectAnimator.ofInt中“”里面就要对应什么名字,要一致
public void setWidth(int width) {
view.getLayoutParams().width = width;
view.requestLayout();
//相当于执行了 invalidate();
}
}
【记(动画小总结)】学习资料:
https://hencoder.com/ui-1-6/
https://blog.csdn.net/singwhatiwanna/article/details/17841165
推荐阅读
- 一个小故事,我的思考。
- 家乡的那条小河
- EffectiveObjective-C2.0|EffectiveObjective-C2.0 笔记 - 第二部分
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- 昨夜小楼听风
- 20170612时间和注意力开销记录
- 2018年11月19日|2018年11月19日 星期一 亲子日记第144篇
- 叙述作文
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽