ValueAnimator的初步使用(动画属性)

来源:互联网 发布:seo必备工具 编辑:程序博客网 时间:2024/05/28 06:07

http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73b67848c5425938448e435061e5a24feed27234403d9917a6605a51f48afad762438507ce1db95d31ccabbe33f5efb3040760bf04305a51fb8ca3632b12487299db86997ad834484aea3c4a95244be54120b81e7fb5c1715ba78811e2692a78e49654866b8fa4360e8297c3eeb5357b737ee9044797082e1ac2f5bb720c7606180df41a74d61a262d7086b5553a13ca6795231479658268f534b04859b2df02b095753bc5fc7bd&p=9866e70c85cc43ff57ed97794a5e96&newp=c4759a45d5c41ffa2afbc12d021494231610db2151ddd701298ffe0cc4241a1a1a3aecbf21271503d2c07e6507af425be0f33171330834f1f689df08d2ecce7e71dd552225&user=baidu&fm=sc&query=android+%CA%F4%D0%D4%B6%AF%BB%AD+%D4%F5%C3%B4%C8%C3%D0%A7%B9%FB%BD%E1%CA%F8%BF%AA%CA%BC%C1%ED%D2%BB%B8%F6&qid=ebba0d3000004236&p1=4

1.布局文件

<ImageView        android:id="@+id/id_ball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher" />    <ImageView        android:layout_below="@+id/id_ball"        android:id="@+id/mBlueBall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="verticalRun"            android:text="垂直" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="paowuxian"            android:text="抛物线" /><Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="fadeOut"            android:text="ddgdfdfd" />    </LinearLayout>

2.Mainactivity中的使用

public class MainActivity extends AppCompatActivity {  int mScreenHeight=500;    ImageView mBlueBall;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);       mBlueBall = (ImageView) findViewById(R.id.mBlueBall);    }//    public void rotateyAnimRun(final View view)//    {//        ObjectAnimator anim = ObjectAnimator////                .ofFloat(view, "zhy", 1.0F,  0.0F)////                .setDuration(500);////        anim.start();////    }//    public void propertyValuesHolder(View view)//    {//        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,//                0f, 1f);//        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,//                0, 1f);//        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,//                0, 1f);//        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();//    }//public void verticalRun(View view)//{//    ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight//            - mBlueBall.getHeight());//    animator.setTarget(mBlueBall);//    animator.setDuration(1000).start();//}    /**     * 自由落体     * @param view     */    public void verticalRun( View view)    {        ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight                - mBlueBall.getHeight());        animator.setTarget(mBlueBall);        animator.setDuration(1000).start();//      animator.setInterpolator(value)        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()        {            @Override            public void onAnimationUpdate(ValueAnimator animation)            {                mBlueBall.setTranslationY((Float) animation.getAnimatedValue());            }        });    }    /**     * 抛物线     * @param view     */    public void paowuxian(View view)    {        ValueAnimator valueAnimator = new ValueAnimator();        valueAnimator.setDuration(3000);        valueAnimator.setObjectValues(new PointF(0, 0));        valueAnimator.setInterpolator(new LinearInterpolator());        valueAnimator.setEvaluator(new TypeEvaluator<PointF>()        {            // fraction = t / duration            @Override            public PointF evaluate(float fraction, PointF startValue,                                   PointF endValue)            {                // x方向200px/s ,则y方向0.5 * 10 * t                PointF point = new PointF();                point.x = 200 * fraction * 3;                point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);                return point;            }        });        valueAnimator.start();        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()        {            @Override            public void onAnimationUpdate(ValueAnimator animation)            {                PointF point = (PointF) animation.getAnimatedValue();                mBlueBall.setX(point.x);                mBlueBall.setY(point.y);            }        });    }    public void fadeOut(View view)    {        ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f);        anim.addListener(new Animator.AnimatorListener()        {            @Override            public void onAnimationStart(Animator animation)            {            }            @Override            public void onAnimationRepeat(Animator animation)            {                // TODO Auto-generated method stub            }            @Override            public void onAnimationEnd(Animator animation)            {                ViewGroup parent = (ViewGroup) mBlueBall.getParent();                if (parent != null)                    parent.removeView(mBlueBall);            }            @Override            public void onAnimationCancel(Animator animation)            {                // TODO Auto-generated method stub            }        });        anim.start();    }}

原创粉丝点击