动画Animation 和 xml 解析Animation

##view Animation

#####新增放大动画

          ScaleAnimation animation = new ScaleAnimation(8.5f, 9, 1, 1,
                            Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f);
                    animation.setDuration(150);
                    animation.setFillAfter(true);
                    image3.setAnimation(animation);
public class MainActivity extends AppCompatActivity {
    private Button mButton;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView= (ImageView) findViewById(R.id.imageview);
        mButton= (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // AlphaAnimation animation=new AlphaAnimation(1.0f,0.0f);//透明动画
                int width= View.MeasureSpec.getSize(getWallpaperDesiredMinimumWidth());//c测量屏幕的宽度
                TranslateAnimation animation=new TranslateAnimation(-mImageView.getMeasuredWidth(), 0,0,0);
                 // RotateAnimation animation=new RotateAnimation(0,-360);//默认中旋转点事0,0点,-180为逆时针旋转180度
                ScaleAnimation animation=new ScaleAnimation(1,1.5f,0.5f,1);
                // 1开始时图片的X的大小的倍数,1.5f为把图片X的大小放大1.5倍后面两个参数为图片Y的大小
                animation.setDuration(10000);
                //mImageView.setAnimation(animation);
                mImageView.startAnimation(animation);
            }
        });
    }
}

//activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始动画"/>
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</LinearLayout>

图片透明动画
这里写图片描述
图片平移动画
这里写图片描述
图片旋转动画
这里写图片描述
图片放大动画
这里写图片描述
##AnimationSet

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView= (ImageView) findViewById(R.id.imageview);
        mButton= (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AnimationSet set=new AnimationSet(false);
                AlphaAnimation animation1=new AlphaAnimation(1.0f,0.0f);//透明动画
                TranslateAnimation animation2=new TranslateAnimation(0,mImageView.getMeasuredWidth(),0,0);
               RotateAnimation animation3=new RotateAnimation(0,-360,100,100);//默认中旋转点事0,0点,-180为逆时针旋转180度
               ScaleAnimation animation4=new ScaleAnimation(1,1.5f,0.5f,1);
                // 1开始时图片的X的大小的倍数,1.5f为把图片X的大小放大1.5倍后面两个参数为图片Y的大小
                animation1.setDuration(5000);
                animation2.setDuration(5000);
                animation3.setDuration(5000);
                animation4.setDuration(5000);
                set.addAnimation(animation1);
                set.addAnimation(animation2);
                set.addAnimation(animation3);
                set.addAnimation(animation4);

                //mImageView.setAnimation(animation);
                mImageView.startAnimation(set);
            }
        });
    }

这里写图片描述
##xml 中的annimation
在res下新建一个anim的xml文件

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

  </rotate>
</set>
  Animation animation= AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_rotate);
                animation.setDuration(5000);
                mImageView.startAnimation(animation);

这里写图片描述
##xml Animation
先在res中创建一个animation xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
//设置动画效果cycle_interpolator循环效果
 android:interpolator="@android:anim/cycle_interpolator">


  <rotate
      android:duration="5000"
      android:fromDegrees="0"
      android:toDegrees="180"
      android:pivotX="200"
      android:pivotY="200"
      android:repeatCount="2">

  </rotate>
    <scale
        android:duration="3000"
        android:fromXScale="1"
        android:toXScale="2"
        android:fromYScale="1"
        android:toYScale="2"
        //多少秒后开始运行这个动画
       android:startOffset="10000"></scale>
</set>

在主界面中调用

 Animation animation= AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_rotate);
                mImageView.startAnimation(animation);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值