【问题标题】:Image is not scaled to full screen图像未缩放到全屏
【发布时间】:2013-06-02 06:50:47
【问题描述】:

我正在尝试在画布中绘制图像之前对其进行解码。我遵循的过程与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);
        }
    } 

【问题讨论】:

  • 听起来可能是密度问题。 developer.android.com/reference/android/graphics/…
  • 如何解决?
  • 我不确定这是否是问题所在。您将图像放在哪个密度文件夹中?该图像的尺寸是多少?您目前在什么设备上进行测试?
  • 设备 - Sony xperia mini(高 - 320 宽 - 480),图片尺寸为(高 - 787 宽 - 1280)

标签: android android-canvas android-image


【解决方案1】:
     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;

【讨论】:

    猜你喜欢
    • 2013-08-14
    • 2012-05-20
    • 2016-05-10
    • 2018-10-29
    • 2015-07-20
    • 2011-05-25
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多