图像未缩放至全屏

时间:2013-06-02 06:50:48

标签: android android-canvas android-image

我想在Canvas中绘制之前解码图像。我遵循的程序与http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

中提到的相同

问题:

图像仅缩放到屏幕的70%。如果我错过任何参数,你能告诉我吗?

    public  Bitmap getAssetImage(Context context, String filename) throws IOException {

         //Decode image size
         final BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true; 
         BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.backgroundhomepage, options); 
         //The new size we want to scale to
         final int REQUIRED_WIDTH= (int) dWidth;
         final int REQUIRED_HIGHT= (int) dHeight;
         //Find the correct scale value. It should be the power of 2.
         int inSampleSize=1;
            if (options.outHeight > REQUIRED_HIGHT || options.outWidth > REQUIRED_WIDTH) {

                // Calculate ratios of height and width to requested height and width
                final int heightRatio = Math.round((float) options.outHeight / (float) REQUIRED_HIGHT);
                final int widthRatio = Math.round((float) options.outWidth / (float) REQUIRED_WIDTH);

                // Choose the smallest ratio as inSampleSize value, this will guarantee
                // a final image with both dimensions larger than or equal to the
                // requested height and width. 
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
            }
            /*
         while(options.outWidth/inSampleSize/2>=REQUIRED_WIDTH && options.outHeight/inSampleSize/2>=REQUIRED_HIGHT)
             inSampleSize*=2; */
         //Decode with inSampleSize
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inSampleSize=inSampleSize;    
         o2.inJustDecodeBounds = false;
         return BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.backgroundhomepage, o2);
    }  




    public void run() {
        // TODO Auto-generated method stub
//      Log.i("Notice", "In run of mybringback"); 
        if(backgoundImage == null){ 
            try {Log.i("MyBringBack", "In run of mybringback.. getting the image of background"); 
                backgoundImage = getAssetImage(getApplicationContext(),"backgroundhomepage");  
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        ourHolder = getHolder();
        while (isRunning) {
//          Log.i("DragDrop", "ourHolder.getSurface().isValid()" +  ourHolder.getSurface().isValid() );
            if (!ourHolder.getSurface().isValid()){
                continue;
            } 
            canvas = ourHolder.lockCanvas();    
            screenCenterX = dWidth / 2;
            screenCenterY = dHeight / 2; 
            canvas.drawBitmap(backgoundImage, 0, 0, null);   
            if (imagePublishDone) {
                if(!welcomeDone){ 
                    message = "Drop your wish to panda";
                    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
                    welcomeDone=true;
                }
                moveImageInEllipticalPath();
            } else {
                initialImagePublish();
            }

            centreReached = false;
            ourHolder.unlockCanvasAndPost(canvas);
        }
    } 

1 个答案:

答案 0 :(得分:0)

     final int REQUIRED_WIDTH= Screen_width;
     final int REQUIRED_HIGHT= screen_height;

首先计算屏幕尺寸

 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();
 display.getSize(size);
 int screen_width = size.x;
 int screen_height = size.y;