车速表动画的Andr​​oid [英] Speedometer Animation Android

查看:189
本文介绍了车速表动画的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有被圆放置在它类似于与放置在其类似于0,最后图象上类似于200或速度计的最后值的位置的位置的第一图像的速度计的刻度盘8的图像。
现在我想旋转被固定在基座和从所述第一图像移动到last.While针转动针头,因为它移过图像时,图像的变化。
针从第一图像移动到最后一个和移回第一图像。!
已经删除了传感器的code作为我没有想和删除背景和主要块增加了一个名为新方法反向()扭转不能运动找到我我走错了。它只是执行循环一次出来。

 公共最后一类温度计扩展视图
{
    私有静态最后弦乐TAG = Thermometer.class.getSimpleName();
    私人处理程序处理程序;    //绘图工具    私人涂料scalePaint;
    私人RectF scaleRect;
    私人涂料logoPaint;
    私人位图的标志;
    私人矩阵logoMatrix;
    私人浮动logoScale;    私人涂料手绘;
    私人路径handPath;
    私人涂料handScrewPaint;    私人涂料backgroundPaint;
    //结束绘图工具    私人位图的背景; //保存缓存的静态部分    //规模配置
    私有静态最终诠释totalNicks = 100;
    私有静态最终浮动degreesPerNick = 360.0f / totalNicks;
    私有静态最终诠释centerDegree = 40; //一个在顶部中心(12
                                                //点​​钟)
    私有静态最终诠释minDegrees = -30;
    私有静态最终诠释maxDegrees = 110;    //手动力学 - 都是在华氏度pssed角前$ P $
    私人布尔handInitialized = FALSE;
    私人浮动handPosition = minDegrees;
    私人浮动handTarget = minDegrees;
    私人浮动handVelocity = 0.0;
    私人浮动handAcceleration = 0.0;
    私人长期lastHandMoveTime = -1L;    公共温度计(上下文的背景下)
    {
        超级(上下文);
        在里面();
    }    公共温度计(上下文的背景下,ATTRS的AttributeSet)
    {
        超(背景下,ATTRS);
        在里面();
    }    公共温度计(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
    {
        超(背景下,ATTRS,defStyle);
        在里面();
    }
    @覆盖
    保护无效onRestoreInstanceState(Parcelable状态)
    {
        束束=(包)状态;
        Parcelable超级大国= bundle.getParcelable(超级大国);
        super.onRestoreInstanceState(覆);        handInitialized = bundle.getBoolean(handInitialized);
        handPosition = bundle.getFloat(handPosition);
        handTarget = bundle.getFloat(handTarget);
        handVelocity = bundle.getFloat(handVelocity);
        handAcceleration = bundle.getFloat(handAcceleration);
        lastHandMoveTime = bundle.getLong(lastHandMoveTime);
    }    @覆盖
    保护Parcelable的onSaveInstanceState()
    {
        Parcelable超级大国= super.onSaveInstanceState();        束状态=新包();
        state.putParcelable(超级大国,超状态);
        state.putBoolean(handInitialized,handInitialized);
        state.putFloat(handPosition,handPosition);
        state.putFloat(handTarget,handTarget);
        state.putFloat(handVelocity,handVelocity);
        state.putFloat(handAcceleration,handAcceleration);
        state.putLong(lastHandMoveTime,lastHandMoveTime);
        返回状态;
    }    私人无效的init()
    {
        处理器=新的处理程序();
        initDrawingTools();
    }
    私人无效initDrawingTools()
    {
        //线性渐变是有点歪斜求实        logoPaint =新的油漆();
        logoPaint.setFilterBitmap(真);
        LOGO = BitmapFactory.de codeResource(的getContext()getResources(),R.drawable.logo);
        logoMatrix =新的Matrix();
        logoScale =(1.0F / logo.getWidth())* 0.3f;        logoMatrix.setScale(logoScale,logoScale);        手绘=新的油漆();
        handPaint.setAntiAlias​​(真);
        handPaint.setColor(0xff392f2c);
        handPaint.setShadowLayer(0.01F,-0.005f,-0.005f,0x7f000000);
        handPaint.setStyle(Paint.Style.FILL);        handPath =新路径();
        handPath.moveTo(0.5F,0.5F + 0.2F);
        handPath.lineTo(0.5F - 0.010f,0.5F + 0.2F - 0.007f);
        handPath.lineTo(0.5F - 0.002f,0.5F - 0.32f);
        handPath.lineTo(0.5F + 0.002f,0.5F - 0.32f);
        handPath.lineTo(0.5F + 0.010f,0.5F + 0.2F - 0.007f);
        handPath.lineTo(0.5F,0.5F + 0.2F);
        handPath.addCircle(0.5F,0.5F,0.025f,Path.Direction.CW);        handScrewPaint =新的油漆();
        handScrewPaint.setAntiAlias​​(真);
        handScrewPaint.setColor(0xff493f3c);
        handScrewPaint.setStyle(Paint.Style.FILL);        backgroundPaint =新的油漆();
        backgroundPaint.setFilterBitmap(真);
    }    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec)
    {
        Log.d(TAG,宽度规格:+ MeasureSpec.toString(widthMeasureSpec));
        Log.d(TAG,高度规范:+ MeasureSpec.toString(heightMeasureSpec));        INT widthMode = MeasureSpec.getMode(widthMeasureSpec);
        INT widthSize = MeasureSpec.getSize(widthMeasureSpec);        INT heightMode = MeasureSpec.getMode(heightMeasureSpec);
        INT heightSize = MeasureSpec.getSize(heightMeasureSpec);        INT chosenWidth = chooseDimension(widthMode,widthSize);
        INT chosenHeight = chooseDimension(heightMode,heightSize);        INT chosenDimension = Math.min(chosenWidth,chosenHeight);        setMeasuredDimension(chosenDimension,chosenDimension);
    }    私人诠释chooseDimension(INT模式,诠释大小)
    {
        如果(==模式|| MeasureSpec.AT_MOST ==模式MeasureSpec.EXACTLY)
        {
            返回的大小;
        }
        其他
        {//(模式== MeasureSpec.UNSPECIFIED)
            得到的回报preferredSize();
        }
    }    //如果没有指定大小
    私人诠释的get preferredSize()
    {
        返回300;
    }    私人无效drawScale(帆布油画)
    {
        canvas.drawOval(scaleRect,scalePaint);        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        的for(int i = 0; I< totalNicks ++ I)
        {
            浮Y1 = scaleRect.top;
            浮Y2 = Y1 - 0.020f;
            canvas.drawLine(0.5F,Y1,0.5F,Y2,scalePaint);
            如果(I%5 == 0)
            {
                int值= nickToDegree(I)
                如果(价值> = minDegrees和放大器;&安培;价值< = maxDegrees)
                {
                    字符串的valueString = Integer.toString(值);
                    canvas.drawText(的valueString,0.5F,Y2 - 0.015f,scalePaint);
                }
            }
            canvas.rotate(degreesPerNick,0.5F,0.5F);
        }
        canvas.restore();
    }    私人诠释nickToDegree(INT缺口)
    {
        INT rawDegree =((尼克和LT; totalNicks / 2)缺口:(尼克 - totalNicks))* 2;
        INT shiftedDegree = rawDegree + centerDegree;
        返回shiftedDegree;
    }    私人浮动degreeToAngle(浮度)
    {
        回报(学位 - centerDegree)/ 2.0F * degreesPerNick;
    }    私人无效drawLogo(帆布油画)
    {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.translate(0.5F - logo.getWidth()* logoScale / 2.0F,0.5F - logo.getHeight()* logoScale / 2.0F);
        INT颜色= 00000000;
        浮位置= getRelativeTemperaturePosition();
        如果(位置℃下)
        {
            色| =(INT)((0XF0)* - 位); // 蓝色
        }
        其他
        {
            色| =((INT)((0XF0)*位置))<< 16; //红
        }
        Log.d(TAG,***+ Integer.toHexString(彩色));
        LightingColorFilter logoFilter =新LightingColorFilter(0xff338822,颜色);
        logoPaint.setColorFilter(logoFilter);        canvas.drawBitmap(标志,logoMatrix,logoPaint);
        canvas.restore();
    }    私人无效drawHand(帆布油画)
    {
        如果(handInitialized)
        {
            浮handAngle = degreeToAngle(handPosition);
            canvas.save(Canvas.MATRIX_SAVE_FLAG);
            canvas.rotate(handAngle,0.5F,0.5F);
            canvas.drawPath(handPath,手绘);
            canvas.restore();
            canvas.drawCircle(0.5F,0.5F,0.01F,handScrewPaint);
        }
    }    私人无效drawBackground(帆布油画)
    {
        如果(背景== NULL)
        {
            Log.w(TAG,背景没有创建);
        }
        其他
        {
            canvas.drawBitmap(背景,0,0,backgroundPaint);
        }
    }    @覆盖
    保护无效的onDraw(帆布油画)
    {
        drawBackground(画布);
        浮规模=(浮点)的getWidth();
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(秤,秤);
        drawLogo(画布);
        drawHand(画布);
        canvas.restore();
        moveHand();
    }    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh)
    {
        Log.d(TAG,大小改变为+ W +的x+ H);
        regenerateBackground();
    }    私人无效regenerateBackground()
    {
        //释放旧的位图
        如果(背景!= NULL)
        {
            background.recycle();
        }        背景= Bitmap.createBitmap(的getWidth()的getHeight(),Bitmap.Config.ARGB_8888);
        帆布backgroundCanvas =新的Canvas(背景);
        浮规模=(浮点)的getWidth();
        backgroundCanvas.scale(秤,秤);    }    私人布尔handNeedsToMove()
    {
        返回Math.abs(handPosition - handTarget)GT; 0.01F;
    }    私人无效moveHand()
    {
        setHandTarget(maxDegrees);
        如果(!handNeedsToMove())
        {
            返回;
        }
        如果(lastHandMoveTime!= -1L)
        {
            长currentTime的= System.currentTimeMillis的();
            浮增量=(currentTime的 - lastHandMoveTime)/ 1500.0f;
            浮动方向= Math.signum(handVelocity);
            如果(Math.abs(handVelocity)LT; 90.0f)
            {
                handAcceleration = 5.0F *(handTarget - handPosition);
            }
            其他
            {
                handAcceleration = 0.0;
            }
            handPosition + = handVelocity *三角洲;
            handVelocity + = handAcceleration *三角洲;
            如果((handTarget - handPosition)*方向< 0.01F *方向)
            {
                handPosition = handTarget;
                handVelocity = 0.0;
                handAcceleration = 0.0;
                lastHandMoveTime = -1L;
            }
            其他
            {
                Log.i(内部,其他方向循环);
                lastHandMoveTime = System.currentTimeMillis的();
            }
            无效();
        }
        其他
        {
            Log.i(内部,方向率先晋级决赛圈其他);
            lastHandMoveTime = System.currentTimeMillis的();
            moveHand();
        }
        如果(handPosition == maxDegrees)
        {
            相反();
        }
    }    公共无效反向()
    {
        handAcceleration = 1.0F;
        Log.i(手速,Float.toString(handVelocity));
        Log.i(内部,下一个循环);
        setHandTarget(minDegrees);
        如果(!handNeedsToMove())
        {
            返回;
        }
        如果(lastHandMoveTime!= -1L)
        {
            长currentTime的= System.currentTimeMillis的();
            浮增量=(currentTime的-lastHandMoveTime)/ 1500.0f;
            浮动方向= Math.signum(handVelocity);
            如果(Math.abs(handVelocity)LT; 90.0f)
            {
                handAcceleration = 5.0F *(handPosition + handTarget);
                Log.i(手加速,Float.toString(handAcceleration));
            }
            其他
            {
                handAcceleration = 0.0;
            }
            handPosition - = handVelocity *三角洲;
            handVelocity - = handAcceleration *三角洲;
            如果((handPosition + handTarget)*方向< 0.01F *方向)
            {
                handPosition = handTarget;
                handVelocity = 0.0;
                handAcceleration = 0.0;
                lastHandMoveTime = -1L;
            }
            其他
            {
                lastHandMoveTime = System.currentTimeMillis的();
            }
            无效();
        }
        其他
        {
            lastHandMoveTime = System.currentTimeMillis的();
            相反();
        }
    }    私人浮动getRelativeTemperaturePosition()
    {
        如果(handPosition< centerDegree)
        {
            返回 - (centerDegree - handPosition)/(浮点)(centerDegree - minDegrees);
        }
        其他
        {
            回报(handPosition - centerDegree)/(浮点)(maxDegrees - centerDegree);
        }
    }    私人无效setHandTarget(浮点温度)
    {
        如果(温度< minDegrees)
        {
            温度= minDegrees;
        }
        否则,如果(温度> maxDegrees)
        {
            温度= maxDegrees;
        }
        handTarget =温度;
        handInitialized = TRUE;
        无效();
    }
}


解决方案

我已经解决了手工显示和移动插入这主要活动:

 查看termometro =(查看)findViewById(R.id.termometro);
    termometro.setLayerType(View.LAYER_TYPE_SOFTWARE,NULL);

它有硬件加速做

I have 8 images which are placed circularly on the dial which resembles a speedometer with first image placed at the position which is similar to 0 and the last image on the position similar to 200 or the last value of the speedometer. Now i want to rotate the needle which is fixed at the base and moves from the first image to last.While the needle rotates,as it moves over the images,the images changes. The needle moves from first image to the last and moves back to first image.! Have removed the sensor's code as i didnt wanted and remove the background and major chunk added a new method called reverse() for reversing the movement not able to find where i am going wrong. it just execute the loop once and comes out.

public final class Thermometer extends View  
{
    private static final String TAG = Thermometer.class.getSimpleName();
    private Handler handler;

    // drawing tools

    private Paint scalePaint;
    private RectF scaleRect;


    private Paint logoPaint;
    private Bitmap logo;
    private Matrix logoMatrix;
    private float logoScale;

    private Paint handPaint;
    private Path handPath;
    private Paint handScrewPaint;

    private Paint backgroundPaint;
    // end drawing tools

    private Bitmap background; // holds the cached static part

    // scale configuration
    private static final int totalNicks = 100;
    private static final float degreesPerNick = 360.0f / totalNicks;
    private static final int centerDegree = 40; // the one in the top center (12
                                                // o'clock)
    private static final int minDegrees = -30;
    private static final int maxDegrees = 110;

    // hand dynamics -- all are angular expressed in F degrees
    private boolean handInitialized = false;
    private float handPosition = minDegrees;
    private float handTarget = minDegrees;
    private float handVelocity = 0.0f;
    private float handAcceleration = 0.0f;
    private long lastHandMoveTime = -1L;

    public Thermometer(Context context) 
    {
        super(context);
        init();
    }

    public Thermometer(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        init();
    }

    public Thermometer(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
        init();
    }


    @Override
    protected void onRestoreInstanceState(Parcelable state) 
    {
        Bundle bundle = (Bundle) state;
        Parcelable superState = bundle.getParcelable("superState");
        super.onRestoreInstanceState(superState);

        handInitialized = bundle.getBoolean("handInitialized");
        handPosition = bundle.getFloat("handPosition");
        handTarget = bundle.getFloat("handTarget");
        handVelocity = bundle.getFloat("handVelocity");
        handAcceleration = bundle.getFloat("handAcceleration");
        lastHandMoveTime = bundle.getLong("lastHandMoveTime");
    }

    @Override
    protected Parcelable onSaveInstanceState() 
    {
        Parcelable superState = super.onSaveInstanceState();

        Bundle state = new Bundle();
        state.putParcelable("superState", superState);
        state.putBoolean("handInitialized", handInitialized);
        state.putFloat("handPosition", handPosition);
        state.putFloat("handTarget", handTarget);
        state.putFloat("handVelocity", handVelocity);
        state.putFloat("handAcceleration", handAcceleration);
        state.putLong("lastHandMoveTime", lastHandMoveTime);
        return state;
    }

    private void init() 
    {
        handler = new Handler();
        initDrawingTools();
    }


    private void initDrawingTools() 
    {
        // the linear gradient is a bit skewed for realism

        logoPaint = new Paint();
        logoPaint.setFilterBitmap(true);
        logo = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.logo);
        logoMatrix = new Matrix();
        logoScale = (1.0f / logo.getWidth()) * 0.3f;

        logoMatrix.setScale(logoScale, logoScale);

        handPaint = new Paint();
        handPaint.setAntiAlias(true);
        handPaint.setColor(0xff392f2c);
        handPaint.setShadowLayer(0.01f, -0.005f, -0.005f, 0x7f000000);
        handPaint.setStyle(Paint.Style.FILL);

        handPath = new Path();
        handPath.moveTo(0.5f, 0.5f + 0.2f);
        handPath.lineTo(0.5f - 0.010f, 0.5f + 0.2f - 0.007f);
        handPath.lineTo(0.5f - 0.002f, 0.5f - 0.32f);
        handPath.lineTo(0.5f + 0.002f, 0.5f - 0.32f);
        handPath.lineTo(0.5f + 0.010f, 0.5f + 0.2f - 0.007f);
        handPath.lineTo(0.5f, 0.5f + 0.2f);
        handPath.addCircle(0.5f, 0.5f, 0.025f, Path.Direction.CW);

        handScrewPaint = new Paint();
        handScrewPaint.setAntiAlias(true);
        handScrewPaint.setColor(0xff493f3c);
        handScrewPaint.setStyle(Paint.Style.FILL);

        backgroundPaint = new Paint();
        backgroundPaint.setFilterBitmap(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    {
        Log.d(TAG, "Width spec: " + MeasureSpec.toString(widthMeasureSpec));
        Log.d(TAG, "Height spec: " + MeasureSpec.toString(heightMeasureSpec));

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int chosenWidth = chooseDimension(widthMode, widthSize);
        int chosenHeight = chooseDimension(heightMode, heightSize);

        int chosenDimension = Math.min(chosenWidth, chosenHeight);

        setMeasuredDimension(chosenDimension, chosenDimension);
    }

    private int chooseDimension(int mode, int size) 
    {
        if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) 
        {
            return size;
        } 
        else 
        { // (mode == MeasureSpec.UNSPECIFIED)
            return getPreferredSize();
        }
    }

    // in case there is no size specified
    private int getPreferredSize() 
    {
        return 300;
    }

    private void drawScale(Canvas canvas) 
    {
        canvas.drawOval(scaleRect, scalePaint);

        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        for (int i = 0; i < totalNicks; ++i) 
        {
            float y1 = scaleRect.top;
            float y2 = y1 - 0.020f;
            canvas.drawLine(0.5f, y1, 0.5f, y2, scalePaint);
            if (i % 5 == 0) 
            {
                int value = nickToDegree(i);
                if (value >= minDegrees && value <= maxDegrees) 
                {
                    String valueString = Integer.toString(value);
                    canvas.drawText(valueString, 0.5f, y2 - 0.015f, scalePaint);
                }
            }
            canvas.rotate(degreesPerNick, 0.5f, 0.5f);
        }
        canvas.restore();
    }

    private int nickToDegree(int nick) 
    {
        int rawDegree = ((nick < totalNicks / 2) ? nick : (nick - totalNicks)) * 2;
        int shiftedDegree = rawDegree + centerDegree;
        return shiftedDegree;
    }

    private float degreeToAngle(float degree) 
    {
        return (degree - centerDegree) / 2.0f * degreesPerNick;
    }

    private void drawLogo(Canvas canvas) 
    {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.translate(0.5f - logo.getWidth() * logoScale / 2.0f,0.5f - logo.getHeight() * logoScale / 2.0f);
        int color = 0x00000000;
        float position = getRelativeTemperaturePosition();
        if (position < 0) 
        {
            color |= (int) ((0xf0) * -position); // blue
        } 
        else 
        {
            color |= ((int) ((0xf0) * position)) << 16; // red
        }
        Log.d(TAG, "*** " + Integer.toHexString(color));
        LightingColorFilter logoFilter = new LightingColorFilter(0xff338822,color);
        logoPaint.setColorFilter(logoFilter);

        canvas.drawBitmap(logo, logoMatrix, logoPaint);
        canvas.restore();
    }

    private void drawHand(Canvas canvas) 
    {
        if (handInitialized) 
        {
            float handAngle = degreeToAngle(handPosition);
            canvas.save(Canvas.MATRIX_SAVE_FLAG);
            canvas.rotate(handAngle, 0.5f, 0.5f);
            canvas.drawPath(handPath, handPaint);
            canvas.restore();
            canvas.drawCircle(0.5f, 0.5f, 0.01f, handScrewPaint);
        }
    }

    private void drawBackground(Canvas canvas) 
    {
        if (background == null) 
        {
            Log.w(TAG, "Background not created");
        } 
        else 
        {
            canvas.drawBitmap(background, 0, 0, backgroundPaint);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
        drawBackground(canvas);
        float scale = (float) getWidth();
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(scale, scale);
        drawLogo(canvas);
        drawHand(canvas);
        canvas.restore();
        moveHand();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) 
    {
        Log.d(TAG, "Size changed to " + w + "x" + h);
        regenerateBackground();
    }

    private void regenerateBackground() 
    {
        // free the old bitmap
        if (background != null) 
        {
            background.recycle();
        }

        background = Bitmap.createBitmap(getWidth(), getHeight(),Bitmap.Config.ARGB_8888);
        Canvas backgroundCanvas = new Canvas(background);
        float scale = (float) getWidth();
        backgroundCanvas.scale(scale, scale);

    }

    private boolean handNeedsToMove() 
    {
        return Math.abs(handPosition - handTarget) > 0.01f;
    }

    private void moveHand() 
    {
        setHandTarget(maxDegrees);
        if (!handNeedsToMove()) 
        {
            return;
        }
        if (lastHandMoveTime != -1L) 
        {
            long currentTime = System.currentTimeMillis();
            float delta = (currentTime - lastHandMoveTime) / 1500.0f;
            float direction = Math.signum(handVelocity);
            if (Math.abs(handVelocity) < 90.0f) 
            {
                handAcceleration = 5.0f * (handTarget - handPosition);
            } 
            else 
            {
                handAcceleration = 0.0f;
            }
            handPosition += handVelocity * delta;
            handVelocity += handAcceleration * delta;
            if ((handTarget - handPosition) * direction < 0.01f * direction) 
            {
                handPosition = handTarget;
                handVelocity = 0.0f;
                handAcceleration = 0.0f;
                lastHandMoveTime = -1L;
            } 
            else 
            {
                Log.i("inside ","direction else loop");
                lastHandMoveTime = System.currentTimeMillis();
            }
            invalidate();
        }   
        else 
        {
            Log.i("inside ","direction first final else loop");
            lastHandMoveTime = System.currentTimeMillis();
            moveHand();
        }
        if(handPosition==maxDegrees)
        {
            reverse();
        }
    }

    public void reverse()
    {
        handAcceleration=1.0f;
        Log.i("Hand Velocity",Float.toString(handVelocity));
        Log.i("Inside","next loop");
        setHandTarget(minDegrees);
        if (!handNeedsToMove()) 
        {
            return;
        }
        if (lastHandMoveTime != -1L) 
        {
            long currentTime = System.currentTimeMillis();
            float delta = (currentTime -lastHandMoveTime) / 1500.0f;
            float direction = Math.signum(handVelocity);
            if (Math.abs(handVelocity) <90.0f) 
            {
                handAcceleration = 5.0f * (handPosition+handTarget);
                Log.i("Hand Acceleration",Float.toString(handAcceleration));
            }       
            else 
            {
                handAcceleration = 0.0f;
            }   
            handPosition -= handVelocity * delta;
            handVelocity -= handAcceleration *delta;
            if ((handPosition + handTarget) * direction < 0.01f * direction) 
            {
                handPosition = handTarget;
                handVelocity = 0.0f;
                handAcceleration = 0.0f;
                lastHandMoveTime =-1L; 
            } 
            else 
            {
                lastHandMoveTime = System.currentTimeMillis();
            }   
            invalidate();
        } 
        else 
        {
            lastHandMoveTime = System.currentTimeMillis();
            reverse();
        }   
    }

    private float getRelativeTemperaturePosition() 
    {
        if (handPosition < centerDegree) 
        {
            return -(centerDegree - handPosition)/ (float) (centerDegree - minDegrees);
        } 
        else 
        {
            return (handPosition - centerDegree)/ (float) (maxDegrees - centerDegree);
        }
    }

    private void setHandTarget(float temperature) 
    {
        if (temperature < minDegrees) 
        {
            temperature = minDegrees;
        } 
        else if (temperature > maxDegrees) 
        {
            temperature = maxDegrees;
        }
        handTarget = temperature;
        handInitialized = true;
        invalidate();
    }
}

解决方案

I have solved hand showing and moving inserting this in main activity:

    View termometro = (View)findViewById(R.id.termometro);
    termometro.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

It has to do with hardware acceleration

这篇关于车速表动画的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆