Android 属性动画,补间动画,xml资源动画详解

  • Bitmap
  1. isRecycle()  判断对象是否回收
  2. recycle()回收自己
  3. createBitmap(source,x,y,width,height)从原位图source,从x,y 开始挖出width,height 并创建新的bitmap对象

  • BitmapDrawable里面封装的图片就是Bitmap
  1. BitmapDrawable drawable = new BitmapDrawable();
  2. 拿到Bitmap对象  Bitmap bitmap = drawable.getBitmap();

  • BitmapFactory
  1. decodeByteArray() 从字节数组解析Bitmap
  2. decodeFile(String path)从文件解析Bitmap
  3. decodeStream(InputStream is)从输入流解析创建Bitmap

  • 帧动画
  1.  FrameAnimation
  2.  多张图片快速切换,形成动画效果
  3. drawable文件夹不放图片,只放资源文件
  4.  ImageView显示图片可以设置内容(src),也可以设置背景(background)

  • 步骤
  1. 在drawable下创建资源文件
       

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/g1" android:duration="200" />
    <item android:drawable="@drawable/g2" android:duration="200" />
    <item android:drawable="@drawable/g3" android:duration="200" />
    <item android:drawable="@drawable/g4" android:duration="200" />
    <item android:drawable="@drawable/g5" android:duration="200" />
    <item android:drawable="@drawable/g6" android:duration="400" />
    <item android:drawable="@drawable/g7" android:duration="400" />
    <item android:drawable="@drawable/g8" android:duration="400" />
    <item android:drawable="@drawable/g9" android:duration="400" />
</animation-list>

  1. 在Java代码中调用
         

package cqupt.com.frame;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private AnimationDrawable animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.iv_image);
        imageView.setBackgroundResource(R.drawable.frame_animation);
        animation = (AnimationDrawable) imageView.getBackground();
    }

    public void startAnistartAnimation(View view) {
       animation.start();
    }
}


  • 补间动画
  1. TraslateAnimation
      

iv = (ImageView) findViewById(R.id.iv)
TranslateAnimation ta = new TranslateAnimation(-100,100,0,0);

ta.setDuration(2000);
iv.startAnimation(ta);

    
     * -100:表示动画的水平方向的初始坐标
     * iv原始x -100
     * 100:表示动画的水平方向的结束坐标
     * iv的原始x + 100


TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,-1.5f,Animation.RELATIVE_TO_SELF,1.5f,
        Animation.RELATIVE_TO_SELF,-1.5f,Animation.RELATIVE_TO_SELF,1.5f);

ta.setDuration(2000);
iv.startAnimation(ta);


* -1.5f:表示动画的水平方向的初始坐标
    * iv的原始x - iv宽度 * 1.5
* 1.5f:表示动画的水平方向的结束坐标
    * iv的原始x + iv宽度 * 1.5

属性动画的位移:

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv,"translationX",-100,100);
objectAnimator.setDuration(2000);
objectAnimator.start();

利用资源文件创建动画:
1.在anima文件下,创建tanslate_animation文件

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXDelta="-100"
    android:fromYDelta="0"
    android:shareInterpolator="false"
    android:toXDelta="100"
    android:toYDelta="0"
    />
2. 在Java文件中加载:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.tanslate_animation);
iv.startAnimation(animation);

  1. ScaleAnimation
           
ScaleAnimation sa = new ScaleAnimation(0.5f, 1, 0.5f, 1,iv.getWidth()/2,iv.getHeight()/2);
sa.setDuration(2000);
iv.startAnimation(sa);

0.5f, 1, 0.5f, 1 比例
iv.getWidth()/2,iv.getHeight()/2以左上点为基准

属性动画:

// iv.setScaleX();
 ObjectAnimator oa = ObjectAnimator.ofFloat(iv,"scaleX",0.5f,1);
 oa.setDuration(2000);
 oa.start();
xml资源文件:
res/anima资源文件

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.5"
    android:toXScale="0.5"
    android:pivotX="0"
    android:pivotY="0"
    android:fromYScale="1"
    android:toYScale="1"
    />

java文件中加载:

Animation am = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
iv.startAnimation(am);

  1. AlphaAnimation

AlphaAnimation aa = new AlphaAnimation(.1f,1);
aa.setDuration(2000);
iv.startAnimation(aa);

  1. RotateAnimation

RotateAnimation ra = new RotateAnimation(45, 360,iv.getWidth()/2,iv.getHeight()/2);
ra.setDuration(2000);
iv.startAnimation(ra);

  1. AnimationSet  动画集
 
AnimationSet as = new AnimationSet(false);
as.addAnimation(ta);
as.addAnimation(sa);
as.addAnimation(aa);
iv.startAnimation(as);
       
      在资源文件中实现:
     

<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="0.5"
android:toXScale="0.5"
android:pivotX="0"
android:pivotY="0"
android:fromYScale="1"
android:toYScale="1"
/>

 在Java文件中加载:


Animation as = AnimationUtils.loadAnimation(this, R.anim.animation_set);
iv.startAnimation(as);




  • 属性动画

  1. ValueAnimation
          用于没有set方法,设置动画
          步骤:
           一   通过ValueAnimation的ofInt(), ofFloat(),ofObject()静态方法创建ValueAnimation
           二   调ValueAnimation的方法设置时间,插入器,重复次数
           三  addUpdateListener注册监听器
           四   valueAnimator.getAnimatedValue()拿到渐变值使用
          
private void toggle() {
if(mIsOpen){
//关闭
int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);//0
final int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);//0
desc.measure(width,height);//等效measure(0,0)
descHeight = desc.getMeasuredHeight();
//      Toast.makeText(UIUtils.getContext(), ""+descHeight, Toast.LENGTH_SHORT).show();
        //播放动画

int start = descHeight;
        int end = 0;
doAnimator(start, end);
}else{
//打开
int start = 0;
        int end = descHeight;
doAnimator(start,end);
}
mIsOpen = !mIsOpen;
}

private void doAnimator(int start, int end) {
    ValueAnimator animator = ValueAnimator.ofInt(start,end);
animator.setDuration(500);
animator.start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int heightTemp = (int) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams params = desc.getLayoutParams();
params.height = heightTemp;
desc.setLayoutParams(params);
}
    });

  1. ObjectAnimation(ValueAnimation的子类)
        步骤  
                 通过ObjectAnimation的ofInt(), ofFloat(),ofObject()静态方法创建ValueAnimation
                 调ObjectAnimation的方法设置时间,插入器,重复次数
                 直接start()
      一. 


ObjectAnimator imageAnimal = ObjectAnimator.ofFloat(image, "translationX", 0, 720, 360, 0);
imageAnimal.setDuration(3000);
imageAnimal.start();
     二 . 较为特殊的一个:

setBackgroundColor(Color);
ObjectAnimator tvAnimal = ObjectAnimator.ofObject(tv,"backgroundColor",new ArgbEvaluator(),Color.RED,Color.BLACK,Color.GREEN);
tvAnimal.setDuration(3000);
tvAnimal.setRepeatCount(ValueAnimator.INFINITE);
tvAnimal.setRepeatMode(ValueAnimator.REVERSE);
tvAnimal.start();
x


  • 通过xml文件创建动画
  1. 补间动画放在res/anima 文件,属性动画放在res/animator下
  2. 使用AnimationUtil.loadAnimation()
  3. 相应的对象调用startAnimaiton()
  4. ObjectAnimation对应xml标签是<ObjectAnimator
          ValueAnimation对应的xml标签是<animator>.
         在Java文件中加载动画资源
         

// 加载动画资源
ObjectAnimator colorAnim = (ObjectAnimator) AnimatorInflater
      .loadAnimator(MainActivity.this, R.animator.color_anim);
colorAnim.setEvaluator(new ArgbEvaluator());
// 对该View本身应用属性动画
colorAnim.setTarget(this);
// 开始指定动画
colorAnim.start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值