自定义ImageView,点击可旋转

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);
   }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值