帧动画,补间动画,和属性动画

帧动画:FrameAnimation
多张图片快速切换,形成动画效果
帧动画使用xml定义.
创建一个Folder在res下,然后定义一个xml文件,根节点是animation——list.
将素材复制到drawable_hdpi文件夹下作为资源id.

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">  //         oneshot(只启动一次)  为true 表示只播放一次,false为循环播放。
    <item android:drawable="@drawable/g1" android:duration="200" />
    <item android:drawable="@drawable/g2" android:duration="200" />  //资源文drawable图片, 持续时间。

</animation-list>

xml定义好之后,要将文件显示在ImageView中。
步骤: 定义一个ImageView组件通过findViewById找到。
2,设置imageview的背景资源 iv.setBackgroundResource(R.drawable.定义的xml文件)
3,然后在获取background强转成animationdrawable; AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
4,开始帧动画的动作。 ad.start();

dmeo:

package com.zh.frameanimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ImageView  iv = (ImageView) findViewById(R.id.iv);
        iv.setBackgroundResource(R.drawable.frameanimation);
        AnimationDrawable  ad = (AnimationDrawable) iv.getBackground();
        ad.start();

    }
}

II补间动画:
组件由原始状态向终极状态转变时,为了让过渡更自然,而自动生成的动画。
有平移 (translate),缩放(scale),透明(alpha),和旋转(roatate).
平移:(此动画的播放位置从初始位置(10,20)开始的)可以设置为(0,0)原点播放。
TranslateAnimation ta = new TranslateAnimation(10, 100, 20, 200);
参数的意义:
10:表示的x坐标起始位置
* iv(显示动画的对象)的真实 x + 10
100: 表示x坐标的结束位置
iv的真实位置是x+100;
20:表示y轴的起始位置;
*iv的真实位置是y+20;
200:表示y轴的结束位置:
iv的真实位置是y+200;
ta.setDuration(2000); 设置持续时间是2秒;
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
TranslateAnimation ta = new TranslateAnimation(fromXType, fromXValue, toXType, toXValue,
fromYType, fromYValue, toYType, toYValue);
fromXtype;
eg.TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 5 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);

Animation.RELATIVE_TO_SELF:类型是相对于自己。
Animation.RELATIVE_TO_SELF, 1 X的起始位置是;
iv的真实位置是iv的真实x+1*iv的宽。(X+一倍自己的宽度iv.getWidth)
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
y轴方向;y轴的起始位置是 iv 的y轴的真实位置是:y+0.5倍的iv的高度;
结束位置是: 2倍iv的高度+y轴的位置。
(x,y可以为负数从相反的方向开始)。
设置重复的模式:相反的Reverse
ta.setRepeatMode(Animation.REVERSE);
//RELATIVE_TO_PARENT 这个是相对于父元素来说。
demo:

public void translate(View v) {
        //
        // TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 100);
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT,
                3, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 2);
        // 设置持续的时间。
        ta.setDuration(2000);
        // 设置重复的次数
        ta.setRepeatCount(2);
        ta.setRepeatMode(Animation.REVERSE);  //相反的方向。

        iv.startAnimation(ta);// 设置动画开始。animation(动画)

    }


缩放:scale
//获得缩放动画对象。(默认不写缩放中心的话是左上角)
ScaleAnimation sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, iv.getWidth() / 2, iv.getHeight() / 2); //缩放的开始位置和结束位置,还有缩放的
x坐标的开始位置; x轴方向: x的真实起始位置是 X+0.5倍的iv的宽度,
2:表示x坐标缩放的结束位置
* 2 * iv宽
iv.getWidth() / 2:表示缩放点的x坐标
* iv的真实x + iv.getWidth() / 2
3、设置缩放还有一个特性;
sa.setFillAfter(true); //设置缩放的填充结束(也就是设置保持缩放结束后的最后的位置)。

public void scale1(View v){
        ScaleAnimation  sa = new ScaleAnimation(0.5f,2,0.1f,3, iv.getWidth()/2, iv.getHeight()/2);
        sa.setDuration(2000);
        sa.setRepeatCount(2);
        sa.setRepeatMode(Animation.REVERSE);
        //设置动画充满前的位置
        sa.setFillBefore(true);   //默认的就是填充动画结束前的位置。
        iv.startAnimation(sa);

    }

缩放相对于自己的:
ScaleAnimation sa = new ScaleAnimation(0.5f, 2, 0.1f, 3,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Animation.RELATIVE_TO_SELF, 0.5f:表示缩放点的x坐标
* iv的真实x + 0.5 * iv宽


设置透明度:alpha;
AlphaAnimation al = new AlphaAnimation(fromAlpha, toAlpha) (透明度动画)transparent :全透明的。
0:全透明; 1:完全不透明。

public void alpha(View v){
        AlphaAnimation  al = new AlphaAnimation(0, 1);  //从透明到完全不透明。
        al.setDuration(2000);
        iv.startAnimation(al);
    }

旋转动画:Rotate;
RotateAnimation ra = new RotateAnimation(fromDegrees, toDegrees, pivotX, pivotY) 从哪个中心点旋转的角度范围。

    public void rotate(View v){
//      RotateAnimation  ra = new RotateAnimation(0,180);
//      RotateAnimation ra = new RotateAnimation(20, 90, iv.getWidth()/2, iv.getHeight()/2);
        RotateAnimation ra = new RotateAnimation(20, 180, Animation.RELATIVE_TO_SELF, 1,
       Animation.RELATIVE_TO_SELF, 1);   //旋转的位置从自己的右下角开始(1,1 )   从自己的中心位置是 (0.5f , 0.5f)
        ra.setDuration(2000);
        ra.setRepeatCount(2);
    ra.setFillAfter(true);
        ra.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ra);
    }

可以将平移,缩放,alpha透明度,已经旋转动画一起播放。如果在AnimationSet集合中设置的播放的 shareInterpolator校对器为false意思是按动画各自own的方式播放。(播放这些动画之前选、要将这些动画单独播放一遍否则报状态异常)

public void fly(View v) {
        AnimationSet as = new AnimationSet(false);
        as.addAnimation(ra);
        as.addAnimation(al);
        as.addAnimation(sa2);
        as.addAnimation(ta);

        iv.startAnimation(as);

    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值