Android Animation (动画设计)

Android Animation(动画设计)

        本文主要介绍逐帧动画,补间动画,属性动画

使用简单的图片

1.Bitmap和BitmapFactory

把一个Bitmap包装成BitmapDrawable对象,调用BitmapDrawable的构造器
BitmapDrawable drawable = new BitmapDrawable(bitmap);
获取BitmapDrawable所包装的Bitmap,
Bitmap bitmap = drawable.getBitmap();

2.Bitmap的其他方法

createBitamap(Bitmap source,int x,int y,int width,int height);从源source中从指定的坐标点(x,y)开始挖取(width,height)的图像,创建新的Bitmap
createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean filter):对原(src)进行缩放,缩放成(dstWidth,dstHeight)
createBitmap(int width,int height,Config config):创建一个(width,height)的新位图,创建副本时调用
.......

Animation

简单变换

1.canvas变换
canvas.rotate(float degress,float px,float py):旋转变换
canvas.scale(float sx,float sy,float px,float py):缩放变换
canvas.skew(float sx,float sy):倾斜变换
canvas.translation(float dx,float dy):平移

canvas.drawTextOnPath(String text,Path path,float hOffset,float vOffset,Paint paint):沿着路径绘制文本
2.Matrix变换
setTranslate(float dx,float dy);空调之Matrix进行平移
setSkew(float kx,float ky,float px,float py):Matrix以px,py为轴心进行倾斜,kx,ky为X,Y,上的倾斜方向
setsKew(float kx,float ky)
setRotate(float degress);
setRoatate(float degress,float px,float py)以px,py为轴心进行旋转,degress控制旋转的角度
setScale(float sx,float sy);
setScale(float sx,float sy,float px,float py);Matrix以px,py为轴心进行缩放,sx,sy为缩放的比例
canvas.drawBitmap(Bitmap bitmap,Matrix matrix,Paint paint);
3.AnimationDrawable与逐帧动画
//res下定义xml动画文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@mipmap/g1" android:duration="200"/>
    <item android:drawable="@mipmap/g2" android:duration="200"/>
    <item android:drawable="@mipmap/g3" android:duration="200"/>
</animation-list>

//MainActivity中定义
    ImageView iv = (ImageView)findIdBy(R.id.image);
    iv.setBackgroundResource(R.drawable.animation);
    AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
    ad.start();
4.Tween(补间动画)与Interpolator 
    AlphaAnimation:透明度改变的 (0 ~1)
    ScaleAnimation:大小缩放的动画(pivotX,pivotY)缩放中心
    TranslateAnimation:位移变化的动画
    RotateAnimation:旋转动画(pivotX,pivotY)旋转中心
    Interpolator接口,负责控制动画的变化速度,可以使四种不同的变化以(匀速,加速,减速,抛物线,)等各种速度变化
    >LinearInterpolator:动画以均匀地速度变化
    >AccelerateInterpolator:加速
    >AccelerateDecelerateInterpolator:中间加速,两边较慢
    >CycleInterpolator:循环播放特定次数,变化速度按正旋变化
    >DecelerateInterpolator:减速
//在java文件中使用
TranslateAnimation translation = new TranslateAnimation(-100,100,-100,100);
ScaleAnimation scale = new ScaleAnimation(1f,0.5f,1f,0.5f);
AlphaAnimation alpha = new AlphaAnimation(1f,0.5f);
RotateAnimation rotate = new RotateAnimation(0,180,0.5f,0.5f);
setDuration(3000);//设置时间
xxx.setFillAfter(true);//转换后保持动画的状态
iv.startAnimation(rotate);//开始
<!--xml使用xml布局定义-->
<set
    android:shareInterpolator="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"//使用Interpolator接口定义动画的速度
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
            android:fromXScale="1.4"
            android:toXScale="0.0"
            android:fromYScale="0.6"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="700"
            android:duration="400"
            android:fillBefore="false" />
        <rotate
            android:fromDegrees="0"
            android:toDegrees="-45"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="700"
            android:duration="400" />
    </set>
</set>

//Mainactivity中使用
Animation anim = AnimationUtils.loadAnimation(this, R.anim.animation);
anim。startAnimation(anim);
4.属性动画(属性动画与补间动画相比,
    补间动画:只改变了图片的位置
    属性动画:改变了位置,也改变了动画的真是坐标)
属性动画的API
Animator:提供了创建属性动画基类,只在被继承,重写时使用
ValueAnimator:(1)计算各帧的相关属性值
            (2)为指定的对象设置指定的值
ObjectAnimator:ValueAnimator的子类
AnimatorSet:Animator的子类,组合多个Animator1按照顺序播放

ValueAnimator创建动画
1.调用ValueAnimator的ofInt(),ofFloat();ofObject()静态方法创建Animator实例
2.调用ValueAnimator的setXXX()
     oa1.setDuration(3000);//持续时间
        oa1.setRepeatCount(1);重复次数
        设置重复模式
        oa1.setRepeatMode(ValueAnimator.RESTART);从头开始模式
    setRepeatMode(Animation.REVERSE);反方向执行

3.调用anim.start();方法启用
4.为ValueAnimator注册监听器AnimatorUpdateListener,在监听器上可以监听ValueAnimator改变的值
//使用ObjectAnimation分别定义不同的属性
    ObjectAnimation oa1,oa2,oa3,oa4;
    oa1 = ObjectAnimator.ofFloat(iv, "translationX",-100,100);
    oa2 = ObjectAnimator.ofFloat(iv,"scaleX",1f,0.5f);
    oa3 = ObjectAnimator.ofFloat(iv,"rotation",0,45);
     oa4 = ObjectAnimator.ofFloat(iv,"alpha",1f,0.4f);
     oa.start();

//使用Animation集合
        AnimatorSet set = new AnimatorSet();
        //oa1,oa2,oa3,oa4:不同的Animation动画变换法则,一起使用
        set.playSequentially(oa1,oa2,oa3,oa4);//设置变换法则
        set.setTarget(iv);//设置变换对象
        set.start();

2.Paint画笔工具,(Canvas(画板的画笔))

Paint的一些常用属性 去·
setARGB(int a,int r,int g,int b);设置颜色(value 0~255)
setAntiAlias(boolean aa)是否抗锯齿
setColor(int color)设置颜色
setPathEffect(PathEffect path)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值