android之动画、xml实现动画res\anim

----补间动画(View动画)------------

原理:动画不会改变控件真实的坐标;

  //透明动画
    public void alphaAnimate(){

        ImageView iv=findViewById(R.id.imagev);
        AlphaAnimation aa=new AlphaAnimation(1.0f,0.0f);
        aa.setDuration(2000);//动画执行的时间
        aa.setRepeatCount(1);//动画重复的次数.,默认一次
        aa.setRepeatMode(Animation.RESTART);//设置模式

iv.startAnimation(aa);
    }

    //旋转动画
    public void rotateAnimate(){
        ImageView iv=findViewById(R.id.imagev);
        RotateAnimation ra=new RotateAnimation(0,360);//参数一,开始的角度,参数二,结束的角度

        ra.setDuration(2000);//动画执行的时间
        ra.setRepeatCount(1);//动画重复的次数.,默认一次
        ra.setRepeatMode(Animation.RESTART);//设置模式
        iv.startAnimation(ra);
    }

    //缩放动画
    public void scaleAnimate(){
        ImageView iv=findViewById(R.id.imagev);
        ScaleAnimation sa=new ScaleAnimation(1.0f,2.0f,1.0f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//参数:开始的缩放比例,结束的缩放比例,相对于自己的中心

        sa.setDuration(2000);//动画执行的时间
        sa.setRepeatCount(1);//动画重复的次数.,默认一次
        sa.setRepeatMode(Animation.RESTART);//设置模式
        iv.startAnimation(sa);
    }

    //平移动画'
    public  void translateAnimate(){
        ImageView iv=findViewById(R.id.imagev);
        TranslateAnimation tra=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f);//相对于父控件移动
    tra.setDuration(2000);
    tra.setFillAfter(true);//当动画结束后停留在结束的位置上
        iv.startAnimation(tra);
    }

    //组合动画---一起执行
    public void togetherAnimate(){
        ImageView iv=findViewById(R.id.imagev);
        AnimationSet aset=new AnimationSet(true);//创建动画合集
        RotateAnimation ra=new RotateAnimation(0,360);//参数一,开始的角度,参数二,结束的角度

        ra.setDuration(2000);//动画执行的时间
        ra.setRepeatCount(1);//动画重复的次数.,默认一次
        ra.setRepeatMode(Animation.RESTART);//设置模式

        TranslateAnimation tra=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.2f);//相对于父控件移动
        tra.setDuration(2000);
        tra.setFillAfter(true);//当动画结束后停留在结束的位置上
aset.addAnimation(ra);//添加动画
        aset.addAnimation(tra);//添加动画

        iv.startAnimation(aset);

    }

-------xml创建View动画-------

补间动画,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。

View animation只能应用于View对象,而且只支持一部分属性,如支持缩放旋转而不支持背景颜色的改变。

在View Animation(Tween Animation)中,其改变的是View的绘制效果,真正的View的属性保持不变,比如无论你在对话中如何缩放Button的大小,Button的有效点击区域还是没有应用动画时的区域,其位置与大小都不变。

用XML定义的动画放在/res/anim/文件夹内。

 

 

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromAlpha="1.0f"
    android:toAlpha="0.5f"
    android:repeatModel="reverse"
    android:duration="2000"
    android:repeatMode="reverse"
    >


<alpha>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="300"
    android:duration="2000"
android:repeatCount="1"
    >


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

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="300"
        android:duration="2000"
        android:repeatCount="1"
        >


    </rotate>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="1.0f"
        android:toAlpha="0.5f"
        android:repeatModel="reverse"
        android:duration="2000"
        android:repeatMode="reverse">


    <alpha>

</set>

应用动画:

  1. ImageView spaceshipImage = (ImageView)findViewById(R.id.spaceshipImage);

  2. Animation hyperspaceJumpAnimation=AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump;

  3. spaceshipImage.startAnimation(hyperspaceJumpAnimation);

 

 

----------属性动画-----

它更改的是对象的实际属性。在Property Animation中,改变的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。而且Property Animation不止可以应用于View,还可以应用于任何对象。

会改变控件的真实坐标

 //创建属性动画
    public void proportyAnimate(){
        ImageView iv=findViewById(R.id.imagev);
        ObjectAnimator oa=ObjectAnimator.ofFloat(iv,"translationX",10,50,20,100);
        oa.setDuration(2000);
        oa.start();//开始动画




        //缩放
        ObjectAnimator ra=ObjectAnimator.ofFloat(iv,"rotation",0,180,90,360);
        ra.setDuration(2000);
        ra.start();//开始动画



        //alpha
        ObjectAnimator aa=ObjectAnimator.ofFloat(iv,"alpha",0,0.5f,0,1,0,1);


        //scale
        ObjectAnimator sa=ObjectAnimator.ofFloat(iv,"scaleY",0.1f,2,2,2);


        iv.setTranslationX(30);
    }
  //*************往集合中添加动画**********
        AnimatorSet set =new AnimatorSet();
        set.setTarget(iv);
        set.playSequentially(oa,ra,aa,sa);//一个一个执行
        set.playTogether(oa,ra,aa,sa);//一起执行

        set.start();
//xml的方式可以创建按属性动画----在res文件夹下创建animator文件夹
<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android">

<objectAnimator
    android:duration="2000"
    android:propertyName="translationX"
    android:valueFrom="10"
    android:valueTo="100"


    ></objectAnimator>

</animator>

 

---------帧动画------在AndroidStudio中强制规定带animation-list节点xml文件必须放在res/drawable文件下,帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。

 

//帧动画
    public void animate(){
        //动画分帧动画(drawable Animation )、view动画(view Animation)、属性动画(proprty额Animaion)
       final ImageView imageAnimate=(ImageView) findViewById(id.image_animate);
        imageAnimate.setBackgroundResource(R.drawable.dr_animation);//设置背景资源
        AnimationDrawable rocketAnimate= (AnimationDrawable) imageAnimate.getBackground();
rocketAnimate.start();
    }
drawable文件夹下创建的dr_animation.xml.                   <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    //:oneshot="true"动画只显示一次,
    //duration="200"是图片显示的时长
    <item
        android:drawable="@drawable/ic_launcher1"
        android:duration="200" />
    <item
        android:drawable="@drawable/ic_launcher1"
        android:duration="200" />
    <item
        android:drawable="@drawable/ic_launcher1"
        android:duration="200" />
    <item
        android:drawable="@drawable/ic_launcher1"
        android:duration="200" />
</animation-list>

 

==========xml中实现动画activity跳转========

从左向右弹出:

用XML定义的动画放在/res/anim/文件夹内,创建:选中anim文件夹-》右键New-》animate Resource File;

从左向右弹出

在 res目录创建anim目录, 然后在目录创建动画的xml文件:out_to_left.xml (从左边退出动画) 、in_from_right.xml(从右边进入动画) 
out_to_right.xml(从右边退出动画)、in_from_left.xml(从左边进入动画)

in_from_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-100%p"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toXDelta="0%p" >
 
</translate>

in_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toXDelta="0%p" >
 
</translate>

out_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p"
    android:interpolator="@android:anim/accelerate_interpolator" >
 
</translate>

out_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0%p"
    android:toXDelta="100%p"
    android:interpolator="@android:anim/accelerate_interpolator" >
 
</translate>

在style.xml中设置主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:windowAnimationStyle">@style/fade</item>
</style>
<style name="fade" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/in_from_right</item>
    <item name="android:activityOpenExitAnimation">@anim/out_to_left</item>
    <item name="android:activityCloseEnterAnimation">@anim/in_from_left</item>
    <item name="android:activityCloseExitAnimation">@anim/out_to_right</item>
</style>

 

从下向上弹出

in_from_bottom:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromYDelta="100%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0%p" >

    </translate>
    <alpha
        android:duration="50"
        android:fromAlpha="1.0"
        android:toAlpha="1.0" />
</set>

in_from_top:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromYDelta="100%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0%p" >

    </translate>
</set>

out_to_bottom:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromYDelta="0%p"
        android:toYDelta="0%p"   //这两两个都是之为0,表示原来的页面不移动
        android:interpolator="@android:anim/accelerate_interpolator" >

    </translate>
</set>

out_to_top:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromYDelta="0%p"
        android:toYDelta="0%p"  //这两两个都是之为0,表示原来的页面不移动
        android:interpolator="@android:anim/accelerate_interpolator" >

    </translate>
</set>

可以在style .xml中设置主题样式,其中item设置动画,参考本文上面的内容:从左往右的动画, 在manifest.xml中吧主题设置给activity。

也可以(使用activity的overridePendingTransition方法,)

fragment中:
getActivity().overridePendingTransition(R.anim.in_from_bottom,R.anim.in_from_top);//两个参数分别是新页面进的动画上一个页面出的xml动画

activity中:

overridePendingTransition(R.anim.in_from_bottom,R.anim.in_from_top);

页面返回的时候,在finish()后面使用  overridePendingTransition(R.anim.in_from_top,R.anim.out_to_top);

在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初 始化列表数据、向服务器发送请求获取数据等等。同样,使用startActivity(intent)方法从一个Activity进入到新的 Activity时,这个过程中也会出现短暂的黑屏。这个问题的存在是由Android应用框架决定的,但的确很影响用户体验。

自定义一个主题
<style name="myautodefine" parent="AppTheme.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
</style>

在manifest.xml中的activity中设置主题为自定义的主题。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值