package com.app.fengactivity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
/**
* @author 作者 E-mail:
* @version 创建时间:2016-6-2 下午6:21:05
* 类说明
*/
public class ToucheCircle extends ImageView {
private Context context;
private long downtime;//按下时间
private long uptime;//松开时间
private float startx;//开始x
private float starty;//开始Y
private float endx;//结束x
private float endy;//结束Y
public ToucheCircle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.context = context;
}
public ToucheCircle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
}
public ToucheCircle(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
System.out.println( event.getX()+"="+event.getY()+"ACTION_DOWN");
downtime=System.currentTimeMillis();
startx=event.getX();
starty=event.getY();
clearAnimation();
break;
case MotionEvent.ACTION_MOVE:
System.out.println( event.getX()+"="+event.getY()+"ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
System.out.println( event.getX()+"="+event.getY()+"ACTION_UP");
uptime=System.currentTimeMillis();
endx=event.getX();
endy=event.getY();
playAnimation();
// Toast.makeText(context, getSpeed()+" ", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void playAnimation() {
RotateAnimation rotateAnimation=new RotateAnimation(0,90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
DecelerateInterpolator dec=new DecelerateInterpolator();
rotateAnimation.setFillAfter(true);
rotateAnimation.setInterpolator(dec);
rotateAnimation.setDuration(2000);
startAnimation(rotateAnimation);//启动动画用start不用set
// this.setAnimation(rotateAnimation);
}
// public void startAnimation(Animation animation) {
// animation.setStartTime(Animation.START_ON_FIRST_FRAME);
// setAnimation(animation);
// invalidateParentCaches();
// invalidate(true);
// }
public double getLength()
{
double length = 0;
if(startx!=0&&starty!=0&&endx!=0&&endy!=0&&downtime!=0&&uptime!=0)
length =Math.sqrt((startx-endx)*(startx-endx)+(starty-endy)*(starty-endy));
return length;
}
//获得你移动的速度,手速越快,速度越大
public double getSpeed()
{
return getLength()/(uptime-downtime);
}
}
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
/**
* @author 作者 E-mail:
* @version 创建时间:2016-6-2 下午6:21:05
* 类说明
*/
public class ToucheCircle extends ImageView {
private Context context;
private long downtime;//按下时间
private long uptime;//松开时间
private float startx;//开始x
private float starty;//开始Y
private float endx;//结束x
private float endy;//结束Y
public ToucheCircle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.context = context;
}
public ToucheCircle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
}
public ToucheCircle(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
System.out.println( event.getX()+"="+event.getY()+"ACTION_DOWN");
downtime=System.currentTimeMillis();
startx=event.getX();
starty=event.getY();
clearAnimation();
break;
case MotionEvent.ACTION_MOVE:
System.out.println( event.getX()+"="+event.getY()+"ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
System.out.println( event.getX()+"="+event.getY()+"ACTION_UP");
uptime=System.currentTimeMillis();
endx=event.getX();
endy=event.getY();
playAnimation();
// Toast.makeText(context, getSpeed()+" ", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void playAnimation() {
RotateAnimation rotateAnimation=new RotateAnimation(0,90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
DecelerateInterpolator dec=new DecelerateInterpolator();
rotateAnimation.setFillAfter(true);
rotateAnimation.setInterpolator(dec);
rotateAnimation.setDuration(2000);
startAnimation(rotateAnimation);//启动动画用start不用set
// this.setAnimation(rotateAnimation);
}
// public void startAnimation(Animation animation) {
// animation.setStartTime(Animation.START_ON_FIRST_FRAME);
// setAnimation(animation);
// invalidateParentCaches();
// invalidate(true);
// }
public double getLength()
{
double length = 0;
if(startx!=0&&starty!=0&&endx!=0&&endy!=0&&downtime!=0&&uptime!=0)
length =Math.sqrt((startx-endx)*(startx-endx)+(starty-endy)*(starty-endy));
return length;
}
//获得你移动的速度,手速越快,速度越大
public double getSpeed()
{
return getLength()/(uptime-downtime);
}
}