必威体育Betway必威体育官网
当前位置:首页 > IT技术

android开发旋转罗盘、风车

时间:2019-08-06 07:10:00来源:IT技术作者:seo实验室小编阅读:76次「手机版」
 

旋转罗盘

一、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() {

            @Override
            public 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;
        }

    }

    @Override
    protected 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();

    @Override
    protected void onmeasure(int widthmeasurespec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.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;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if (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;
            }
        }
    }

    @Override
    protected 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 {

    @Override
    public void onCreate(Bundle savedinstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RotatView rotatView=(RotatView)findViewById(R.id.myRotatView);
        rotatView.setRotatDrawableResource(R.drawable.cycle);
    }

}

相关阅读

css实现鼠标上移图标旋转效果

鼠标上移图标旋转效果在企业的项目中经常会使用到,特别是顶部导航栏,比如:接下来就是要使用css实现鼠标上移图标旋转效果。<!DOCTYPE

电子设计竞赛控制组——完整旋转倒立摆程序

以前也想过要写博客,但是却一直没有付诸于实践,作为第一篇原创,我还是选择将以前电赛时的作品拿出来,毕竟当初可是花费了好多心血的,汗

如何让Word2003中文本框任意旋转

许多时候我们在Word中制作流程图、公章等各种图形的时候都会在图形里面插入&ldquo;文本框&rdquo;,然后在里面输入文字。如果图形旋

AE怎么制作旋转万花筒特效?

以前很喜欢万花筒,旋转的万花筒效果是一个比较好玩的效果,想要使用AE中制作这个旋转的万花筒的效果动画,下面我们就来看看ae制作万花

旋转木马哪里有卖!旋转木马多少钱一台?

大型旋转木马游乐设备,是一款更新换代比较快的产品,现在转马设备已经两极分化为豪华转马和简易转马两类,跟飞椅一样,大型旋转木马也是

分享到:

栏目导航

推荐阅读

热门阅读