Android中属性动画8----ObjectAnimator.ofFloat实现抛物线运动

ObjectAnimator.ofFloat也能实现抛物线运动,但效果没有https://blog.csdn.net/zhaihaohao1/article/details/80942102 好
开始讲:
我测试过了,这种只能在自己的父布局中移动
效果图:
Android中属性动画8----ObjectAnimator.ofFloat实现抛物线运动_第1张图片
Main9Activity中

package com.zhh.myapplication.test4;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.orhanobut.logger.Logger;
import com.zhh.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class Main9Activity extends Activity {
    private ImageView ivTupian;
    private Button btnQidian;
    private Button btnZhongdian;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main9);
        Logger.t("111").d(">>>>" + getClass().getName());
        initView();
        myOnclick();
    }

    private void myOnclick() {
        btnQidian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                donghua();
            }
        });
    }

    private void initView() {
        ivTupian = (ImageView) findViewById(R.id.ivTupian);
        btnQidian = (Button) findViewById(R.id.btnQidian);
        btnZhongdian = (Button) findViewById(R.id.btnZhongdian);

    }

    private void donghua() {

//      拿到起点的绝对坐标
        int[] location = new int[2];
        btnQidian.getLocationInWindow(location);
        int x = location[0];
        int y = location[1];

//      拿到终点控件的绝对坐标
        int [] locationEnd = new int[2];
        btnZhongdian.getLocationInWindow(locationEnd);
        int xEnd = locationEnd[0];
        int yEnd = locationEnd[1];

//      x轴上移动的距离
        float tvJuliX = xEnd-x;
//      y轴上移动的距离
        float tvJuliY = yEnd-y;

//     设置动画,只能在自己的父布局中平移
//     ivTupian自己的相对坐标系中运动(自己的起点就是0,0坐标)
//      0f 开始的坐标,tvJuliX表示移动的距离,整体表示的意思是ivTupian从0开始在X坐标上移动tvJuliX距离。
        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(ivTupian, "translationX", 0f, tvJuliX);
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(ivTupian, "translationY", 0f, 100,200,300,tvJuliY);
        AnimatorSet animatorSet = new AnimatorSet();
//      同时播放
        animatorSet.playTogether(objectAnimatorX, objectAnimatorY);
//      动画用时100ms
        animatorSet.setDuration(500);
//      开始动画
        animatorSet.start();
    }


}

activity_main9中

<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"
    tools:context="com.zhh.myapplication.test4.Main9Activity"
    android:orientation="vertical"
    >
    
    
    <ImageView
        android:id="@+id/ivTupian"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"
        />

    <Button
        android:id="@+id/btnQidian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="起点"
        android:layout_alignParentRight="true"
        />
    <Button
        android:id="@+id/btnZhongdian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="终点"
        android:layout_marginTop="400dp"

        />

RelativeLayout>

完了

源码下载
TestLayout—–动画
https://download.csdn.net/download/zhaihaohao1/10525191

你可能感兴趣的:(动画)