Animator xml解析Animator 和第三种图片点击方式

来源:互联网 发布:嵌入式系统与人工智能 编辑:程序博客网 时间:2024/06/09 23:42

animator

直接用animator写动画

//scaleX scaleY alpha rotateX rotateY translationX translationY translationZ       ObjectAnimator.ofFloat(mImageView,"scaleX",0.0f,1.0f).setDuration(3000).start();

xml 解析animator

要注意的是在Android studio和eclipse中创建时不一样:
Android studio中创建animation文件和animator时建在不同的文件中
而eclipse中只需要创建一个文件就行了,在它里面可以放animator和animation
先在res中创建一个animator文件、
这里写图片描述
在animator中写动画效果

?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">  <objectAnimator      android:duration="1000"      android:propertyName="scaleX"      android:valueFrom="0.0"      android:valueTo="1.0"></objectAnimator>    <objectAnimator        android:duration="1000"        android:propertyName="scaleY"        android:valueFrom="0.0"        android:valueTo="1.0"        android:startOffset="1000"></objectAnimator></set>

在主界面中调用xml中的文件

 Animator animator=AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator);        animator.setTarget(mImageView);        animator.start();

第三种图片点击方式

就是在写ImageView时就写一个点击方法然后在主界面中调用
这样就点击图片就行了

<ImageView        android:id="@+id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        //写一个点击方法        android:onClick="startAnimator"        android:src="@mipmap/ic_launcher"/>

把写的点击方法在主界面中调用

  public void startAnimator(View view){        //scaleX scaleY alpha rotateX rotateY translationX translationY translationZ        //ObjectAnimator.ofFloat(mImageView,"scaleX",0.0f,1.0f).setDuration(3000).start();        Animator animator=AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator);        animator.setTarget(mImageView);        animator.start();    }

两种方法的整体代码

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) {//                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);               Animation animation= AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation_rotate);                mImageView.startAnimation(animation);            }        });    }    public void startAnimator(View view){        //scaleX scaleY alpha rotateX rotateY translationX translationY translationZ        //ObjectAnimator.ofFloat(mImageView,"scaleX",0.0f,1.0f).setDuration(3000).start();        Animator animator=AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator);        animator.setTarget(mImageView);        animator.start();    }}

这里写图片描述

0 0
原创粉丝点击