动画

总:

1.通过xml定义文件动画和通过Java代码定义动画。下面介绍的是通过XML定义

2.在res定义anim文件,在里面创建动画xml

动画有:

1.旋转动画 : rotate

2.缩放动画 : scale

3.透明度动画 : alpha

4.移位动画 : translate

5.复合动画


1.旋转布局

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="180"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="6000"
    android:fillBefore="true">
</rotate>

2.缩放动画 

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="6000"
    android:fillBefore="true">

</scale>

3.透明度动画

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.9"
    android:toAlpha="0.1"
    android:duration="4000"
    android:fillAfter="true">

</alpha>

4.移位动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-50%p"
    android:toXDelta="150%p"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:duration="6000"
    android:fillBefore="true">

</translate>

5.复合动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="6000"
    android:fillAfter="true">

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>

    <scale
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"/>

    <rotate
        android:fromDegrees="0"
        android:toDegrees="720"
        android:pivotX="50%"
        android:pivotY="50%"
        />

</set>

主要属性及说明:

repeatCount: 重复次数

repeatMode:重复模式,restart默认(重启),reverse反向

fromDegrees:为动画起始时物件的角度

toDegrees:为动画结束时物件旋转的角度

pivotX,pivotY 为动画相对于物件的X、Y坐标的开始位.说明:以上两个属性值从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置。

startOffset:为动画皮偏移时间

duration:为动画的持续时间

fillAfter:动画结束后,是否保持现状

fillBefore:动画结束后,是否恢复到未动画之前

fromXScale,fromYScale:一开始就是动画的几倍(缩放)

toXScale,toYScale:到动画的几倍

fromAlpha:一开始动画的透明度(透明度)

toAlpha:到动画的透明度

fromXDelta,fromYDelta:动画起始的位置(移动)

toXDelta,to YDelta:动画结束的位置



6.java文件,为图片设置透明度动画

   ImageView imgb;
    Animation ma1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_main);

        imgb = (ImageView) this.findViewById(R.id.img1);
        ma1 = AnimationUtils.loadAnimation(this, R.anim.my_alpha);
        imgb.startAnimation(ma1);
    }

7.使用动画监听


public class MyActivity extends Activity implements Animation.AnimationListener {

    ImageView imgb;
    Animation ma1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_main);

        imgb = (ImageView) this.findViewById(R.id.img1);
        ma1 = AnimationUtils.loadAnimation(this, R.anim.my_alpha);
        ma1.setAnimationListener(this);
        imgb.startAnimation(ma1);
    }
   //动画监听,实现,onAnimationStart,onAnimationRepeat,onAnimationEnd三个方法
  @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        //动画结束时结束欢迎界面并转到软件的主界面
        Intent intent = new Intent("com.rj141.sb.imganimation02.MainActivity");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        this.startActivity(intent);
        this.finish();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

}

8.MAinActivity界面,布局定义按钮和图片

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button rotate,scale,alpha,translate,set;
    ImageView img;
    Animation mr,ms,ma,mt,mset;
    Intent it;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rotate=(Button)this.findViewById(R.id.rotate);
        rotate.setOnClickListener(this);
        scale=(Button)this.findViewById(R.id.scale);
        scale.setOnClickListener(this);
        alpha=(Button)this.findViewById(R.id.alpha);
        alpha.setOnClickListener(this);
        translate=(Button)this.findViewById(R.id.translate);
        translate.setOnClickListener(this);
        set=(Button)this.findViewById(R.id.set);
        set.setOnClickListener(this);
        img=(ImageView)this.findViewById(R.id.img);

        mr= AnimationUtils.loadAnimation(this,R.anim.my_rotate);
        ms= AnimationUtils.loadAnimation(this,R.anim.my_scale);
        ma= AnimationUtils.loadAnimation(this,R.anim.my_alpha);
        mt= AnimationUtils.loadAnimation(this,R.anim.my_translate);
        mset= AnimationUtils.loadAnimation(this,R.anim.my_set);

    }


    public void onClick(View v){
        int id=v.getId();
        switch (id){
            case R.id.rotate:
                img.startAnimation(mr);
                break;
            case R.id.alpha:
                img.startAnimation(ma);
                break;
            case R.id.scale:
                img.startAnimation(ms);
                break;
            case R.id.translate:
                img.startAnimation(mt);
                break;
            case R.id.set:
                img.startAnimation(mset);
                break;
        }
    }

}

  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值