就是通过动画的方式改变对象的属性了,我们首先需要了解几个属性:
- Duration动画的持续时间,默认300ms。
- time interpolation:时间差值,如LinearInterpolator、AccelerateDecelerateInterpolator,定义动画的变化率。
- Repeat count and behavior:重复次数、以及重复模式;
- Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。
- Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。
相关的类:
- ObjectAnimator 动画的执行类
- ValueAnimator 动画的执行类
- AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
- AnimatorInflater 用户加载属性动画的xml文件
- TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
- TimeInterpolator 时间插值。
总的来说,属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。
下面是代码:
package com.lanxin.testanim2; import android.animation.Animator; import android.animation.AnimatorInflater; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.content.Context; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.AlphaAnimation; import android.view.animation.AnimationSet; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private static final String TAG = "TestAnim2"; Button but1,but2,but3,but4; TextView tv; ImageView iv; Context mContext; ObjectAnimator oa; AnimationSet as; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); findid(); init(); } private void init() { but4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tv.setY(-100); PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x",0); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y",0); oa = ObjectAnimator.ofPropertyValuesHolder(tv,pvhX,pvhY); oa.setDuration(3000); oa.setInterpolator(new AccelerateDecelerateInterpolator()); oa.start(); RotateAnimation ra = new RotateAnimation(0,359,iv.getWidth()/2,iv.getHeight()/2); ra.setDuration(800); ra.setRepeatCount(-1); ra.setFillAfter(false); ra.setInterpolator(new LinearInterpolator()); as = new AnimationSet(true); as.addAnimation(ra); //as.setFillAfter(false); //as.setRepeatMode(1); //as.setRepeatCount(-1); iv.startAnimation(as); } }); but3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animator set = AnimatorInflater.loadAnimator(mContext, R.animator.test1); set.setTarget(tv); set.start(); } }); but2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlphaAnimation aa; if(tv.getAlpha() == 0){ aa = new AlphaAnimation(0f,1f); }else { aa = new AlphaAnimation(1f,0f); } aa.setDuration(2000); as = new AnimationSet(true); as.setFillBefore(true); as.setStartOffset(500); as.addAnimation(aa); tv.startAnimation(as); //移动动画 TranslateAnimation ta = new TranslateAnimation(0,0,-100,0); ta.setDuration(3000); as = new AnimationSet(true); as.setFillBefore(true); as.addAnimation(ta); tv.startAnimation(as); } }); but1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(tv.getAlpha() != 0){ oa = ObjectAnimator.ofFloat(tv,"alpha", 0.0f); but1.setText(getString(R.string.but1_1)); }else{ oa = ObjectAnimator.ofFloat(tv, "alpha", 1.0f); but1.setText(getString(R.string.but1)); } oa.setDuration(2000); oa.start(); Log.i(TAG,"setOnClickListener"); } }); } private void findid() { but1 = (Button)findViewById(R.id.but1); but2 = (Button)findViewById(R.id.but2); but3 = (Button)findViewById(R.id.but3); but4 = (Button)findViewById(R.id.but4); tv = (TextView)findViewById(R.id.tv); iv = (ImageView)findViewById(R.id.load); mContext = this; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
这里是代码下载网址:链接:http://pan.baidu.com/s/1mijKv12 密码:cvna