package com.example.propertyanimsecdemo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
/**
* 贝尔曲线数
*/
private static final int BEZIER_COUNT = 10;
/**
* 4点决定三次贝尔曲线
*/
private static final int POINT_COUNT = 4;
/**
* 移动物体
*/
private Button[] mViews = new Button[BEZIER_COUNT];
/**
* 三次贝塞尔曲线起始点,2控制点,终点
*/
private Point[] mPoints = new Point[POINT_COUNT];
/**
* 屏幕宽
*/
private int w_screen;
/**
* 屏幕高
*/
private int h_screen;
// /**
// * 贝塞尔曲线
// */
// private BezierCurveView[] mBezierCurveViews=new
// BezierCurveView[BEZIER_COUNT];
/**
* 贝塞尔曲线组
*/
private List<Point[]> mList = new ArrayList<Point[]>();
private FrameLayout mFrameLayout;
private LayoutParams mParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFrameLayout = (FrameLayout) findViewById(R.id.frameLayout);
DisplayMetrics dm = getResources().getDisplayMetrics();
w_screen = dm.widthPixels;
h_screen = dm.heightPixels;
Log.i("ee", "屏幕尺寸:宽度 = " + w_screen + "高度 = " + h_screen + "密度 = " + dm.densityDpi);
int[] emiles={R.drawable.yuanbao,R.drawable.ee_11,
R.drawable.ee_24,R.drawable.ee_26,R.drawable.ee_27
,R.drawable.ee_3,R.drawable.ee_31
,R.drawable.ee_32,R.drawable.ee_33
,R.drawable.ee_34};
for (int i = 0; i < BEZIER_COUNT; i++) {
addPointAndView(i,emiles[i]);
}
Button button = new Button(MainActivity.this);
button.setText("表情帝");
button.setBackgroundResource(R.drawable.chat_send_btn_normal);
mParams = new LayoutParams(Utils.dip2px(this, 80), Utils.dip2px(this, 50));
mFrameLayout.addView(button, mParams);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
for (int count = 0; count < BEZIER_COUNT; count++) {
getFPoints();
// mBezierCurveViews[count].setmPoints(mPoints);
mViews[count].setX(mPoints[0].x);
mViews[count].setY(mPoints[0].y);
MyAnimator animator = new MyAnimator(mPoints, MainActivity.this, mViews[count]);
animator.startAnimator();
}
}
});
}
/**
* 随机产生四点
*/
private void getFPoints() {
for (int i = 0; i < POINT_COUNT; i++) {
Point point = new Point();
point.x = new Random().nextInt( w_screen - 20);
if (i == 0) {
point.y = 0;// 控制起点在屏幕最上方
} else if (i == 3) {
point.y = h_screen;// 控制终点在屏幕最下方
} else {
point.y = new Random().nextInt( h_screen);
}
mPoints[i] = point;
Log.d("ee", i + ":(" + point.x + "." + point.y);
}
}
/**往布局上添加物体
* @param count
* @param resource
*/
private void addPointAndView(int count, int resource) {
<pre class="html" name="code" style="font-size: 13.3333339691162px;"> getFPoints();
// BezierCurveView mBezierCurveView=new// BezierCurveView(MainActivity.this);// mFrameLayout.addView(mBezierCurveView);// mBezierCurveView.setAlpha(0);// mBezierCurveView.setmPoints(mPoints);Button mView = new Button(MainActivity.this);mView.setBackgroundResource(resource);mParams = new LayoutParams(Utils.dip2px(this, 30), Utils.dip2px(this, 30));mFrameLayout.addView(mView, mParams);mView.setX(mPoints[0].x);mView.setY(mPoints[0].y - 50);// mBezierCurveViews[count]=mBezierCurveView;mViews[count] = mView;}}
package com.example.propertyanimsecdemo;
import java.util.Random;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;
import android.view.View;
public class MyAnimator {
/**
* 三次贝塞尔曲线起始点,2控制点,终点
*/
private Point[] mPoints;
private Context context;
private View mView;
/**
* 动画
*/
private ValueAnimator mAnimator;
public MyAnimator(Point[] mPoints, Context context, View mView) {
super();
this.mPoints = mPoints;
this.context = context;
this.mView = mView;
}
/**
* 添加属性动画
*/
public void startAnimator() {
mAnimator = ValueAnimator.ofObject(new BezierEvaluator(), new PointF(mPoints[0].x,mPoints[0].y),new PointF( mPoints[3].x,mPoints[3].y));
mAnimator.setDuration(new Random().nextInt(2000)+1000);
mAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF pointF = (PointF)animation.getAnimatedValue();
mView.setX(pointF.x);
mView.setY(pointF.y);
}
});
mAnimator.setTarget(mView);
mAnimator.setRepeatCount(1);
mAnimator.start();
}
class BezierEvaluator implements TypeEvaluator<PointF>{
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
Log.d("ee", "fraction:"+fraction+" startValue:("+startValue.x+"."+startValue.y+") endValue:("+endValue.x+"."+endValue.y+")");
final float t = fraction;
float oneMinusT = 1.0f - t;
PointF point = new PointF();
PointF point0 = (PointF)startValue;
PointF point1 = new PointF();point1.set(mPoints[1].x, mPoints[1].y);
PointF point2 = new PointF();point2.set(mPoints[2].x, mPoints[2].y);
PointF point3 = (PointF)endValue;
point.x = oneMinusT * oneMinusT * oneMinusT * (point0.x)
+ 3 * oneMinusT * oneMinusT * t * (point1.x)
+ 3 * oneMinusT * t * t * (point2.x)
+ t * t * t * (point3.x);
point.y = oneMinusT * oneMinusT * oneMinusT * (point0.y)
+ 3 * oneMinusT * oneMinusT * t * (point1.y)
+ 3 * oneMinusT * t * t * (point2.y)
+ t * t * t * (point3.y);
return point;
}
}
}