android动画效果

本文详细介绍了Android中视图动画的实现方式,包括透明度、旋转、移动和缩放等基本动画类型,并展示了如何通过XML文件和代码实现这些动画效果。此外,还介绍了如何将多种动画效果组合使用以及如何监听动画的开始、结束和重复事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Animation


    //透明度

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000"
        >
    </alpha>

    public void alpha(View v) {
        //使用代码
        /*AlphaAnimation alpha = new AlphaAnimation(0,3);
        alpha.setDuration(1500);
        v.startAnimation(alpha);*/
        //使用xml文件
        v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha));
    }

    //旋转

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2500">
    </rotate>

    public void rotate(View v){
        //绝对的坐标值
        //RotateAnimation ra = new RotateAnimation(0,360,190,205);
        //相对自身的坐标值
        /*RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ra.setDuration(2000);
        v.startAnimation(ra);*/
        //xml文件加载
        //文件中直接写数值,当做绝对坐标值,百分比,相对于自身的坐标
        v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
    }

    //移动

    <translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="150"
    android:toYDelta="100"
    android:duration="2000">
    </translate>

    public void translate(View v){
        //这里的200是相对于现在的增量
        /*TranslateAnimation ta = new TranslateAnimation(0,200,0,200);
        ta.setDuration(2000);
        v.startAnimation(ta);*/

        //文件加载
        v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate));

    }

    //缩放

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

    public void scale(View v){

        //ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1);
        //ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1,100,20);
       /* ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        sa.setDuration(1000);
        v.startAnimation(sa);*/

        v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale));
    }


    //多个效果叠加,使用animationset

    <set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:shareInterpolator="true"
    >
      <alpha
          android:fromAlpha="0"
          android:toAlpha="1">
      </alpha>
      <rotate
          android:fromDegrees="0"
          android:toDegrees="360"
          android:pivotX="50%"
          android:pivotY="50%">
      </rotate>
    </set>

    public void moreAnimation(View v){
        //是否共用动画补间,执行属性,加速,匀速还是减速.
        /*AnimationSet as = new AnimationSet(true);
        as.setDuration(1000);

        AlphaAnimation alpha = new AlphaAnimation(0,1);
        alpha.setDuration(1000);
        TranslateAnimation ta = new TranslateAnimation(0,200,0,200);
        ta.setDuration(1000);
        RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ra.setDuration(1000);
        ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        sa.setDuration(1000);

        as.addAnimation(alpha);
        //as.addAnimation(ta);
        as.addAnimation(ra);
        as.addAnimation(sa);
        v.startAnimation(as);*/

        //文件加载
        v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim));
    }


    //监听动画效果的开始,结束,重复

    public void listenAnimation(View v){

        Animation a = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim);
        a.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //Toast.makeText(MainActivity.this,"animation start", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Toast.makeText(MainActivity.this,"animation end", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //Toast.makeText(MainActivity.this,"animation repeat", Toast.LENGTH_SHORT).show();
            }
        });
        v.startAnimation(a);
    }

    import android.view.animation.Animation;
    import android.view.animation.Transformation;

    /**
     * Created by sample on 16-10-22.
     */

    public class CustomAnimation extends Animation {

    //初始化的一些操作
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {

        System.out.println("initialize");
        super.initialize(width, height, parentWidth, parentHeight);
    }

    //补间时间:假如设置为1 会慢慢从0-1,一直执行applyTransformation()函数.
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        //需要设置的效果应该都在这里

        //System.out.println(interpolatedTime);

        //t.getMatrix().setTranslate(200*interpolatedTime,200*interpolatedTime);

        //设置周期变化移动
        //t.getMatrix().setTranslate((float)Math.sin(interpolatedTime*10)*10,(float)Math.sin(interpolatedTime*10)*10);

        //设置旋转
        t.getMatrix().setRotate(interpolatedTime*180,0.5f,0.5f);

        //设置透明度
        //t.setAlpha(interpolatedTime);

        super.applyTransformation(interpolatedTime, t);
    }



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值