先上代码
xml代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#D4D0C8" tools:context="com.tianxia.hnq.donghuatext.MainActivity"> <ImageView android:id="@+id/image002" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:src="@mipmap/ic_launcher" ></ImageView> </RelativeLayout>activity:
package com.tianxia.hnq.donghuatext;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.relayout)
RelativeLayout relayout;
@BindView(R.id.image002)
ImageView image002;
private int h;//屏幕高度
private int h001;//控件高度
private int statusBarHeight1;//状态栏高度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
h = getResources().getDisplayMetrics().heightPixels;
//获取状态栏高度
//获取status_bar_height资源的ID
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
//根据资源ID获取响应的尺寸值
statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
h001 = (int) image002.getHeight();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch (action) {
case MotionEvent.ACTION_UP:
//属性动画效果
final ImageView image003 = new ImageView(this);
image003.setImageResource(R.mipmap.ic_launcher);
image003.setVisibility(View.VISIBLE);
relayout.addView(image003);
//x轴动画
// image003.setX(x);
//image003.setY(y - statusBarHeight1);
ObjectAnimator animator = ObjectAnimator.ofFloat(image003, "x", x, 0);
animator.setDuration(1000);
animator.setInterpolator(new LinearInterpolator());
//Y轴动画
ObjectAnimator animator1 = ObjectAnimator.ofFloat(image003, "y", y -
statusBarHeight1, h - h001 - statusBarHeight1);
animator1.setDuration(1000);
animator1.setInterpolator(new AccelerateInterpolator());
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.i("donghua", "onAnimationStart");
}
@Override
public void onAnimationEnd(Animator animation) {
relayout.removeView(image003);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(image002, "ScaleX", 1.0f, 0.6f,1.0f,0.8f,1.0f);
animator2.setDuration(250);
animator2.setInterpolator(new DecelerateInterpolator());
ObjectAnimator animator3 = ObjectAnimator.ofFloat(image002, "ScaleY", 1.0f, 0.6f,1.0f,0.8f,1.0f);
animator3.setDuration(250);
animator3.setInterpolator(new DecelerateInterpolator());
AnimatorSet set=new AnimatorSet();
set.play(animator2).with(animator3);
set.start();
}
@Override
public void onAnimationCancel(Animator animation) {
Log.i("donghua", "onAnimationCancel");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.i("donghua", "onAnimationRepeat");
}
});
AnimatorSet set = new AnimatorSet();
set.playTogether(animator, animator1);
set.start();
break;
}
return super.onTouchEvent(event);
}
}