如果第二次启动,Android应用程序无法正确显示图形

时间:2015-02-02 16:26:01

标签: android canvas graphics

首先我应该说这是我制作的第一个Android应用程序,它的方式远远超出我的经验......所以手头有这个:

我有一个应用程序,在Android 4.1上用作汽车车速表。它从arduino,通过usb连接获取一些信息(逗号解析的字符串),并以一种奇特的方式显示它。一般来说,应用程序工作正常。一切都正确显示,应该显示在哪里。

问题出现了,当我出于某些原因退出,并重新启动应用程序...如果我这样做,所有应该"始终显示的信息"像KM / hr,燃料,转速,没有在全部显示。但其他信息,如转向灯或各种汽车信号,都完美展示......(这意味着应用程序成功获取并翻译了USB中的序列号。)任何想法,我做错了什么,或者如何解决这个问题??

负责图形的代码在这里:

     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(null);         
     setContentView(new MyView(this));
      .
      .
      }

        public class MyView extends View {
        public MyView(Context  context) {
           super(context);
    }


       @Override
       protected void onDraw(Canvas canvas) {
         try{    
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);     


        //BACKGROUD///             //ALWAYS SHOWN
        canvas.drawBitmap(Backgroundbmp,0, 0, null); 
        //WARNINGS//               //ALWAYS SHOWN
        if (HighBeam==0){
            canvas.drawBitmap(High_Beam,300,490, null);

        }
        if (EngineCheck==0){
            canvas.drawBitmap(Engine_Check,570, 490, null);

        }
        if (CoolantLevel==0){
            canvas.drawBitmap(coolant_Level,400, 490, null);

        }

        if (Oil==0){
            canvas.drawBitmap(Oil_pressure,670, 490, null);

        }

        //FLASH//



        if (Rflash==0){
            canvas.drawBitmap(Lflash_icon,250,80,null);     

        }


        if (Lflash==0){
            canvas.drawBitmap(Rflash_icon,697,80,null);

        }




        //DATE-TIME//              //ALWAYS SHOWN


        Calendar c = Calendar.getInstance();
        String formattedDate = df.format(c.getTime());  
        paint.setColor(Color.parseColor("#FFFFFF"));
        paint.setTextSize (100) ;        
        canvas.drawText(formattedDate,900,70,paint);

        // ODOMETER - -  TRIP ;           //NOT SHOWN IN SECOND START

        paint.setTextSize (30) ;  
        canvas.drawText(Odometer,510,390,paint);
        canvas.drawText(TripA,930,164,paint);
        canvas.drawText(TripB,930,216,paint);

        //MPG  //
        canvas.drawText(currentMPG+" Lt/h",160,160,paint);
        canvas.drawText(overallMPG+" Lt/h",140,205,paint);

        //FUEL-TEMP//
        paint.setTextSize (35) ;  
        canvas.drawText(FUEL+" Lt",745,450,paint);
        canvas.drawText(TEMP+" °C",285,450,paint);

        // SPEED //
        paint.setColor(Color.parseColor("#56BAEA"));
        paint.setTextSize (220) ;   
        String speed = Integer.toString(rawSPEED);
        canvas.drawText(speed,520,270,paint);

        paint.setStyle(Paint.Style.STROKE);                          

        //      RPM  ////
        paint.setStrokeWidth(35);            
        paint.setColor(Color.parseColor("#25DBFF"));

        int angle = (int) (0.0225*rawRPM) ; 
        //1            
        oval.set(305,63, 725,482); //(left ,top, right, bot )
        canvas.drawArc(oval,90, angle, false, paint);
        //2
        paint.setColor(Color.parseColor("#89ECFF"));
        paint.setStrokeWidth(20);
        oval.set(300,58, 725,487);
        canvas.drawArc(oval,90, angle, false, paint);

                  if (rawRPM>=4500){
                      //1
                      paint.setColor(Color.parseColor("#22FF2A"));
                      paint.setStrokeWidth(35);
                      oval.set(305,63, 725,482); //(left ,top, right, bot )
                      canvas.drawArc(oval,191, angle-101, false, paint);
                      //2
                      paint.setColor(Color.parseColor("#5CFF62"));
                      paint.setStrokeWidth(20);
                      oval.set(300,58, 725,487);
                      canvas.drawArc(oval,191, angle-101, false, paint);

                  }
                  if (rawRPM>6000){
                      //1
                      paint.setColor(Color.parseColor("#FF0000"));
                      paint.setStrokeWidth(35);
                      oval.set(305,63, 725,482); //(left ,top, right, bot )
                      canvas.drawArc(oval,230, angle-140, false, paint);
                      //2
                      paint.setColor(Color.parseColor("#FF2525"));
                      paint.setStrokeWidth(20);
                      oval.set(300,58, 725,487);
                      canvas.drawArc(oval,230, angle-140, false, paint);

                  }
            //3
         paint.setColor(Color.parseColor("#FFFFFF"));
         paint.setStrokeWidth(2);
         oval.set(291,50, 738,496);
         canvas.drawArc(oval,90, angle, false, paint);

         // FUEL//
        int  tempFUEL=FuelBar;
        //1 
        paint.setStrokeWidth(3);
        if (rawFUEL>=30.0){
            tempFUEL = FuelBar -30 ;
            float ignore =(float)(-0.116*tempFUEL+ 7) ;
            paint.setColor(Color.parseColor("#3782F2"));            
            float[] fa= {835,423,830,522 ,840,425,835,522 ,845,427,840,522 ,850,429,845,522 ,855,431,850,522 ,860,434,855,522 ,865,436,860,522};    // left,right +5   top = +2
            canvas.drawLines(fa,4*(int)ignore,28-4*(int)ignore,paint) ; //(left ,top, right, bot )
            tempFUEL=30;
        }
        //2
        float ignore =(float)(-0.83*tempFUEL+25) ;
        paint.setColor(Color.parseColor("#8EFFFF"));
        float[] fb= {870,438,865,522 ,875,440,870,522 ,880,442,875,522 ,885,444,880,522 ,890,446,885,522 ,895,449,890,522 ,900,451,895,522 ,905,454,900,522 ,910,456,905,522 ,915,459,910,522 ,920,461,915,522 ,925,463,920,522 ,930,466,925,522 ,935,469,930,522 ,940,471,935,522 ,945,473,940,522 ,950,475,945,522 ,955,478,950,522 ,960,480,955,522 ,965,482,960,522 ,970,485,965,522 ,975,487,970,522 ,980,489,975,522 ,985,492,980,522 ,990,494,985,522 ,};    // left,right +5   top = +2
        canvas.drawLines(fb,4*(int)ignore,fb.length-4*(int)ignore ,paint) ; //(left ,top, right, bot )
        //3
        paint.setStrokeWidth(2);
        paint.setColor(Color.parseColor("#000000"));
        canvas.drawLine(833,422,992,494,paint);
        paint.setStrokeWidth(3);


        //TEMP///
        int tempTEMP = rawTEMP;
        paint.setStrokeWidth(3);      
        //1
        if (rawTEMP>=90){
        tempTEMP= tempTEMP-90;
        ignore = (float)(-0.12*(float)tempTEMP +6);
        paint.setColor(Color.parseColor("#3782F2"));
        float[] Tb= {187,424,192,522, 182,426,187,522, 177,428,182,522, 172,430,177,522, 167,432,172,522, 162,434,167,522, };    // left,right +5   top = +2
        canvas.drawLines(Tb,4*(int)ignore,Tb.length - 4*(int)ignore,paint) ; //(left ,top, right, bot )
        tempTEMP=90;
        }
        //2
        ignore= (float)(-0.288*(float)tempTEMP +26);
        paint.setColor(Color.parseColor("#8EFFFF"));
        float[] Ta= {157,436,162,522,  152,438,157,522,  147,440,152,522, 142,442,147,522, 137,444,142,522, 132,446,137,522, 127,448,132,522, 122,451,127,522, 117,453,122,522, 112,455,117,522, 107,457,112,522, 102,460,107,522, 97,461,102,522, 92,462,97,522, 87,465,92,522, 82,470,87,522, 77,472,82,522, 72,474,77,522, 67,476,72,522, 62,479,67,522, 57,481,62,522, 52,483,57,522, 47,485,52,522,  42,487,47,522, 37,489,42,522 ,  32,491,37,522 };    // left,right +5   top = +2
        canvas.drawLines(Ta,4*(int)ignore,Ta.length - 4*(int)ignore,paint) ; //(left ,top, right, bot )

        //3
        paint.setColor(Color.parseColor("#000000"));
        paint.setStrokeWidth(2);            
        canvas.drawLine(30,492,189,420,paint);


    }catch  (Exception e) {      }
        invalidate(); 



    }

2 个答案:

答案 0 :(得分:0)

尝试从onResume调用onDraw?

答案 1 :(得分:0)

感谢你的帮助!我解决了我的问题,毕竟,它与正确的onResume和onDestroy()无关...正如我所提到的,在第二次启动之后,一些图形仍在显示...在我的代码中,它是图形高于“里程表”draw.text ..原因是,里程表值是从文本文件中获取的,在第一次关闭后可能会损坏...我删除了该行,并且现在该程序按预期工作!