一、RotatView

package com.sun.shine.myrotation.view;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;public class RotatView extends View {float o_x;float o_y;Handler handler;int delayedTime = 20;static final int play = 0;static final int stop = 1;double currentTime = 0;int width;int height;double maxwidth;Bitmap rotatBitmap;public RotatView(Context context, AttributeSet attrs) {super(context, attrs);init();}private void init() {vRecord = new VRecord();handler = new Handler() {@Overridepublic void handleMessage(Message msg) {double detaTime = System.currentTimeMillis() - currentTime;switch (msg.what) {case play: {if (isClockWise) {speed = speed - a * detaTime;if (speed <= 0) {return;} else {handler.sendEmptyMessageDelayed(play, delayedTime);}} else {speed = speed + a * detaTime;if (speed >= 0) {return;} else {handler.sendEmptyMessageDelayed(play, delayedTime);}}addDegree((float)(speed * detaTime + (a * detaTime * detaTime) / 2));// if (a < a_max) {// a = (float)(a + a_add*detaTime);// System.out.println("a:"+a);// }currentTime = System.currentTimeMillis();invalidate();break;}case stop: {speed = 0;handler.removeMessages(play);}}super.handleMessage(msg);}};initSize();}public void setRotatBitmap(Bitmap bitmap) {rotatBitmap = bitmap;initSize();postInvalidate();}public void setRotatDrawableResource(int id) {BitmapDrawable drawable = (BitmapDrawable)getContext().getResources().getDrawable(id);setRotatDrawable(drawable);}public void setRotatDrawable(BitmapDrawable drawable) {rotatBitmap = drawable.getBitmap();initSize();postInvalidate();}private void initSize() {if (rotatBitmap == null) {// throw new NoBitMapError("Error,No bitmap in RotatView!");return;}width = rotatBitmap.getWidth();height = rotatBitmap.getHeight();maxwidth = Math.sqrt(width * width + height * height);o_x = o_y = (float)(maxwidth / 2);// ȷ��Բ�����}private void addDegree(float added) {deta_degree += added;if (deta_degree > 360 || deta_degree < -360) {deta_degree = deta_degree % 360;}}@Overrideprotected void onDraw(Canvas canvas) {Matrix matrix = new Matrix();matrix.setTranslate((float)width / 2, (float)height / 2);matrix.preRotate(deta_degree);matrix.preTranslate(-(float)width / 2, -(float)height / 2);matrix.postTranslate((float)(maxwidth - width) / 2, (float)(maxwidth - height) / 2);canvas.drawBitmap(rotatBitmap, matrix, paint);super.onDraw(canvas);}Paint paint = new Paint();@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);setMeasuredDimension((int)maxwidth, (int)maxwidth);}float down_x;float down_y;float target_x;float target_y;float up_x;float up_y;float current_degree;float up_degree;float deta_degree;double lastMoveTime = 0;public static final float a_min = 0.001f;public static final float a_add = 0.001f;float a = a_min;public static final float a_max = a_min * 5;double speed = 0;VRecord vRecord;boolean isClockWise;@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubif (rotatBitmap == null) {throw new NoBitMapError("Error,No bitmap in RotatView!");}switch (event.getAction()) {case MotionEvent.ACTION_DOWN: {down_x = event.getX();down_y = event.getY();current_degree = detaDegree(o_x, o_y, down_x, down_y);vRecord.reset();// handler.sendEmptyMessage(stop);a = a_max;break;}case MotionEvent.ACTION_MOVE: {down_x = target_x = event.getX();down_y = target_y = event.getY();float degree = detaDegree(o_x, o_y, target_x, target_y);float dete = degree - current_degree;if (dete < -270) {dete = dete + 360;} else if (dete > 270) {dete = dete - 360;}lastMoveTime = System.currentTimeMillis();vRecord.add(dete, lastMoveTime);addDegree(dete);current_degree = degree;postInvalidate();break;}case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP: {a = a_min;double lastupTime = System.currentTimeMillis();double detaTime = lastupTime - lastMoveTime;up_x = event.getX();up_y = event.getY();up_degree = detaDegree(o_x, o_y, up_x, up_y);speed = speed + vRecord.getSpeed();if (speed > 0) {speed = Math.min(VRecord.max_speed, speed);} else {speed = Math.max(-VRecord.max_speed, speed);}// System.out.println("speed:" + speed);if (speed > 0) {isClockWise = true;// v = 1;} else {isClockWise = false;// v = -1;}currentTime = System.currentTimeMillis();handler.sendEmptyMessage(0);break;}}return true;}float detaDegree(float src_x, float src_y, float target_x, float target_y) {float detaX = target_x - src_x;float detaY = target_y - src_y;double d;if (detaX != 0) {float tan = Math.abs(detaY / detaX);if (detaX > 0) {if (detaY >= 0) {d = Math.atan(tan);} else {d = 2 * Math.PI - Math.atan(tan);}} else {if (detaY >= 0) {d = Math.PI - Math.atan(tan);} else {d = Math.PI + Math.atan(tan);}}} else {if (detaY > 0) {d = Math.PI / 2;} else {d = -Math.PI / 2;}}return (float)((d * 180) / Math.PI);}static class NoBitMapError extends RuntimeException {/*** */private static final long serialVersionUID = 1L;public NoBitMapError(String detailMessage) {super(detailMessage);}}static class VRecord {int addCount;public static final int length = 15;double[][] record = new double[length][2];public void add(double detadegree, double time) {for (int i = length - 1; i > 0; i--) {record[i][0] = record[i - 1][0];record[i][1] = record[i - 1][1];}record[0][0] = detadegree;record[0][1] = time;addCount++;}public static final double max_speed = 8;public double getSpeed() {if (addCount == 0) {return 0;}int maxIndex = Math.min(addCount, length) - 1;if ((record[0][1] - record[maxIndex][1]) == 0) {return 0;}double detaTime = record[0][1] - record[maxIndex][1];double sumdegree = 0;for (int i = 0; i < length - 1; i++) {sumdegree += record[i][0];// System.out.println(record[i][0]);}// System.out.println("----------");// System.out.println(sumdegree);// System.out.println(detaTime);double result = sumdegree / detaTime;if (result > 0) {return Math.min(result, max_speed);} else {return Math.max(result, -max_speed);}// System.out.println("v=" + result);}public void reset() {addCount = 0;for (int i = length - 1; i > 0; i--) {record[i][0] = 0;record[i][1] = 0;}}}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (rotatBitmap != null) {rotatBitmap.recycle();rotatBitmap = null;}}
}

二、MainActivity

package com.sun.shine.myrotation;import com.sun.shine.myrotation.view.RotatView;import android.os.Bundle;
import android.app.Activity;public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RotatView rotatView=(RotatView)findViewById(R.id.myRotatView);rotatView.setRotatDrawableResource(R.drawable.cycle);}}

android开发旋转罗盘、风车相关推荐

  1. android旋转动画惯性,android 之旋转罗盘 风车 开发

    我要介绍的是一个 能旋转的view,说这个view能旋转有点不切实际,那是视觉效果,其实是对图片的旋转.目前它只支持图片.你可以把它认为是一个能响应手势旋转的View. 它的功能有: 1.会响应手势旋 ...

  2. android 之旋转罗盘 风车 开发 Android 随手指旋转图片

    原帖地址 源码下载地址 我要介绍的是一个 能旋转的view,说这个view能旋转有点不切实际,那是视觉效果,其实是对图片的旋转.目前它只支持图片.你可以把它认为是一个能响应手势旋转的View. 它的功 ...

  3. Android开发实践:屏幕旋转的处理

    最近开发Android Camera相关的程序,被屏幕旋转搞得头大,一方面得考虑屏幕旋转后布局的变化,另一方面得搞清楚屏幕的旋转方向.角度与Camera的Preview角度的关系.本来通过重载Acti ...

  4. android旋转不重绘,Android自定义view仿微信刷新旋转小风车

    本文实例为大家分享了Android仿微信刷新旋转小风车 具体代码,供大家参考,具体内容如下 不太会录像,没办法,智能截图了 不多说了,直接上代码 package com.shipneg.demoysp ...

  5. android 三星调用拍照功能吗,Android 开发手机(三星)拍照应用照片旋转问题解决办法...

    Android 开发手机(三星)拍照应用照片旋转问题解决办法 最近解决了一个令我头疼好久的问题,就是三星手机拍照图片旋转的问题,项目中有上传图片的功能,那么涉及到拍照,从相册中选择图片,别的手机都ok ...

  6. Android旋转视频工具类,Android开发实现的IntentUtil跳转多功能工具类【包含视频、音频、图片、摄像头等操作功能】...

    本文实例讲述了Android开发实现的IntentUtil跳转多功能工具类.分享给大家供大家参考,具体如下: 说明:此工具类是本人开发中总结下来的,还有其它的跳转亲给我留言,希望大家一起把这个工具类打 ...

  7. android+动画+锯齿,Android_rotate--animation 动画旋转两图片,消除动画锯齿现象 android 开发:动画旋转两图片 - 下载 - 搜珍网...

    Android+动画旋转两图/ Android+动画旋转两图/.classpath Android+动画旋转两图/.project Android+动画旋转两图/.settings/ Android+ ...

  8. Android开发必备(干货源码放送大)

    Android源码大放送(实战开发必备) 文件夹 PATH 列表 │  javaapk.com文件列表生成工具.bat │  使用说明.txt │  免费下载更多源码.url │  目录列表.txt ...

  9. Android开发常见问题及解答

    搭建Android开发环境时容易出现什么问题?有哪些地方需要注意? 1.环境搭建过程中,往往会出现安装配置或者环境变量配置遗漏或者放置地方不匹配,导致后续找不到位置,因此需要考虑好安装各个软件的步骤及 ...

最新文章

  1. 函数 —— popen() fscanf() sprintf() 执行shell命令并获取结果
  2. Spring中如何使用注解来配置Bean?有哪些相关的注解?
  3. aes密文长度_RSA加密密文可变(一句话说明)
  4. Apollo进阶课程㊵丨Azure仿真平台使用
  5. 精益 React 学习指南 (Lean React)- 4.1 react 代码规范
  6. Ambari Server 架构
  7. red5 + java+flex free download_Flex 4 +Red5 流媒体使用研究2——Flex4的VideoPlayer控件应用...
  8. 九个实用的Word转PDF的方法,为你解决格式转换的问题
  9. Opencascade TopoDS_Shape Orientation
  10. 卡刷刷机包出现 Error 7
  11. 使用赫夫曼编码进行解码
  12. 怎么设置linux系统定时关机,Linux系统定时关机
  13. backtracking及其应用2
  14. Android系统初识
  15. python代码编辑器android_三款可以在安卓手机上运行Python代码的软件
  16. 自媒体必备2大运营工具+5款剪辑软件,一次性解决,让你事半功倍
  17. 论文笔记(十四):PoseRBPF: A Rao–Blackwellized Particle Filter for 6-D Object Pose Tracking
  18. 测试-开发的恩怨情仇
  19. 科研工具干货合集!干到缺水!愣着干嘛,收藏啊!
  20. 超级详细的膝盖关节结构讲解

热门文章

  1. Nokelock:一款千疮百孔的蓝牙挂锁
  2. (十二)Mybatis的缓存机制
  3. 2015 中国大数据技术大会 PPT 尝鲜
  4. WPF MVVMLight Treeview中的SelectedItem
  5. hexo搭建Gitcafe博客(专栏)
  6. 快速排序——一趟快排
  7. 苹果:无须用户同意,App 涨价时自动续订,网友吐槽“App Store 毒性越来越大”...
  8. 大批量数据写入mysql报错 #1436 Thread stack overrun
  9. Box Shadow(阴影)-Css3写法示例
  10. JVM入门(位置、体系结构、类加载器、双亲委派机制、沙箱安全机制、Native、PC寄存器、方法区、堆(新生区{伊甸园区、幸存区}、养老区、永久区)、OOM、GC算法、JMM)