0
votes

i have a class that extends from View. in onDraw(Canvas canvas) method of this class i put multiple bitmap together and display it as canvas. i think that onDraw method executes repetitively.i have to check if one of bitmap doesn't exist in SDcard download section of my code downloads it and it will happen continuously. my code is here:

protected void onDraw(Canvas canvas) 
{
    super.onDraw(canvas);

    canvas.drawARGB(100, 255, 255, 0);
    canvas.translate(posX, posY);
    canvas.scale(imageScale,imageScale);
    int b = 0;
    for (int a = 0 ; a < 5 ;a++)
    {
        for(int i =0 ; i<4 ; i++)
        {
            if(maptile != null){
                canvas.drawBitmap(maptile,(256*i )  ,(256*a) , null);
                downloading  = false;
                b++;
                }else
                {

                    canvas.drawBitmap(toop,(toop.getWidth()*i )  ,(toop.getHeight()*a) , null);
                    Log.i("Canvas loop", "Showing");
                }

        }   
    }
} 

my proble is that i can't check SDcard continuously during onDraw method. in fact i want to check if bitmap is in my storage,so display it. if there is not,display another bitmap until that specific bitmap will be download. what should i do to reach this goal????

EDIT: i create a thread that runs from my constructor in this class. yhis thread check the SDcard.whenever my specific file downloaded in SDcard,set this file as bitmap. but when i want to redraw my view to display my new bitmap,the app crashes. what's your idea.what is the problem??

public class SDChecker  implements Runnable
{

    @Override
    public void run() 
    {
        while( true)
        {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            File file = new File(Environment.getExternalStorageDirectory()+ "/GreatMap","tile0.jpg");
            if (file.exists())
            {
                maptile= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/GreatMap/tile0.jpg");
                invalidate();
                Log.i("Create      bit           map", "Bitmap    create");
                Toast.makeText(getContext(), "loadmap tile", Toast.LENGTH_SHORT);
                IMGnotExisting = false;

            }
            else
            {
                IMGnotExisting = true;
            }

        }

    }

}

after invalidate() the onDraw() method will be call. so we have maptile bitmap.this time it has to draw maptile bitmap. but it doesn't and app crashes .

1

1 Answers

0
votes

Your view's onDraw method gets called only after a call to invalidate().

Simply initialize the bitmap inside the view to the "default" one and draw it without any check. Then, once your bitmap is ready, set it as the view's bitmap (create a setter) and call view.invalidate(). The view will redraw with the new bitmap.

Remember to recycle the old "default" bitmap to save resources.