PropertyValuesHolder学习
package com.loaderman.customviewdemo; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button mButton; private TextView mTv, mTv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTv = (TextView)findViewById(R.id.tv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofFloat("Rotation", 60f, -60f, 40f, -40f, -20f, 20f, 10f, -10f, 0f); PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("alpha", 0.1f, 1f, 0.1f, 1f); ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(mTv, rotationHolder, alphaHolder);\\实例入口 animator.setDuration(3000); animator.start(); } }); } }
PropertyValuesHolder的ofFloat和ofInt函数的基本用法
package com.loaderman.customviewdemo; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.AccelerateInterpolator; public class MainActivity extends AppCompatActivity { private MyTextView mTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTv = findViewById(R.id.mTv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { /** * ofObject使用 */ PropertyValuesHolder charHolder = PropertyValuesHolder.ofObject("CharText",new CharEvaluator(),new Character('A'),new Character('Z')); ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(mTv, charHolder); animator.setDuration(3000); animator.setInterpolator(new AccelerateInterpolator()); animator.start(); } }); } }
package com.loaderman.customviewdemo; import android.content.Context; import android.util.AttributeSet; public class MyTextView extends android.support.v7.widget.AppCompatTextView { public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } public void setCharText(Character character){ setText(String.valueOf(character)); } }
package com.loaderman.customviewdemo; import android.animation.TypeEvaluator; public class CharEvaluator implements TypeEvaluator<Character> { public Character evaluate(float fraction, Character startValue, Character endValue) { int startInt = (int)startValue; int endInt = (int)endValue; int curInt = (int)(startInt + fraction *(endInt - startInt)); char result = (char)curInt; return result; } }
PropertyValuesHolder的ofobject函数的基本用法
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!