• android——ObjectAnimator动画


    在新的android sdk中谷歌为我们提供了新的动画实现方式。化繁为简。将以前的animation动画进一步封装,使用起来更加方便。

    先来看XML文件:

    <RelativeLayout 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" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="40dp" >
    
            <ImageView
                android:id="@+id/rect"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/rect" />
            <ImageView 
                android:id="@+id/star"
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:background="@drawable/star"
                />
        </LinearLayout>
    
        <ImageView
            android:id="@+id/b"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/b" />
    
        <ImageView
            android:id="@+id/c"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/c" />
    
        <ImageView
            android:id="@+id/d"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/d" />
    
        <ImageView
            android:id="@+id/e"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/e" />
    
        <ImageView
            android:id="@+id/a"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:background="@drawable/a" />
    
    </RelativeLayout>

    然后看看activity文件

    package com.example.testanimator;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.animation.ObjectAnimator;
    import android.animation.TimeInterpolator;
    import android.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.view.animation.FastOutLinearInInterpolator;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.AccelerateDecelerateInterpolator;
    import android.view.animation.AccelerateInterpolator;
    import android.view.animation.AnticipateInterpolator;
    import android.view.animation.BounceInterpolator;
    import android.view.animation.CycleInterpolator;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener {
        private ImageView a, b, c, d, e;
        private int res[] = { R.id.a, R.id.b, R.id.c, R.id.d, R.id.e };
        private List<ImageView> listImg = new ArrayList<ImageView>();
        private boolean isStart = false;
        private float y;
        private float x;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            for (int i = 0; i < res.length; i++) {
                ImageView img = (ImageView) findViewById(res[i]);
                listImg.add(img);
                img.setOnClickListener(this);
            }
            x = listImg.get(0).getPivotX();
            y = listImg.get(0).getPivotY();
        }
    
        private void startAnimator() {
    
            isStart = true;
            for (int i = 0; i < listImg.size(); i++) {
                ObjectAnimator.ofFloat(listImg.get(i), "translationY", y, y - 400 + i * 100).setDuration(1000).start();
                ObjectAnimator.ofFloat(listImg.get(i), "translationX", x, x + i * 100).setDuration(1000).start();
            }
        }
    
        private void closeAnimator() {
            isStart = false;
            for (int i = 0; i < listImg.size(); i++) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(listImg.get(i), "translationY", y - 400 + i * 100, y);
    
                animator.setDuration(1000);
                animator.setStartDelay(1000);
                animator.setInterpolator(new BounceInterpolator()); //设置插值器
                animator.start();
            }
    
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float startx ,starty;
            float endx ,endy ;
    
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
    
                break;
            case MotionEvent.ACTION_MOVE:
    
                break;
            default:
                break;
            }
            return super.onTouchEvent(event);
        }
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
            case R.id.a:
                if (isStart) {
                    closeAnimator(); //还原位置
                } else {
                    startAnimator(); //开始动画移动位置和焦点
                }
                break;
    
            default:
                break;
            }
    
        }
    
    }
  • 相关阅读:
    hadoop面试
    常用脚本
    1.kafka
    2.flink
    java面试
    Hbase 项目
    linux 2>&1 和 &的意思
    Jetbrains系列产品重置试用方法
    解决GitHub网页githubusercontent地址无法访问问题
    stylus 成熟的写法
  • 原文地址:https://www.cnblogs.com/shoneworn/p/9029451.html
Copyright © 2020-2023  润新知