2011-07-21 25 views
0

我得到了一些代碼,它根據圖像的大小繪製圖像。但我想將圖像拉伸至全屏。我嘗試了很多,但沒有什麼幫助。任何人都可以爲我做這個嗎?在此先感謝如何修改此代碼以將圖像拉伸至全屏在android

public Bitmap getBitmap(int width, int height, int index) { 
     Bitmap b = Bitmap.createBitmap(width, height, 
       Bitmap.Config.ARGB_8888); 
     b.eraseColor(0xF000FFFF); 
     Canvas c = new Canvas(b); 
     Drawable d = getResources().getDrawable(mBitmapIds[index]); 

     int margin = 1; 
     int border =1 ; 
     Rect r = new Rect(margin, margin, width - margin, height - margin); 

     int imageWidth = r.width() - (border * 2); 
     int imageHeight = imageWidth * d.getIntrinsicHeight() 
       /d.getIntrinsicWidth(); 
     if (imageHeight > r.height() - (border * 2)) { 
      imageHeight = r.height() - (border * 2); 
      imageWidth = imageHeight * d.getIntrinsicWidth() 
        /d.getIntrinsicHeight(); 
     } 

     r.left += ((r.width() - imageWidth)/2) - border; 
     r.right = r.left + imageWidth + border + border; 
     r.top += ((r.height() - imageHeight)/2) - border; 
     r.bottom = r.top + imageHeight + border + border; 

     Paint p = new Paint(); 
     p.setColor(0xFFC0C0C0); 
     c.drawRect(r, p); 
     r.left += border; 
     r.right -= border; 
     r.top += border; 
     r.bottom -= border; 

     d.setBounds(r); 
     d.draw(c); 
     return b; 
    } 
+0

嘗試設置爲背景圖像而不是src圖像它會拉伸但可以使圖像變形 – ingsaurabh

回答

0

檢查出你的縮略圖這個.................的onClick方法,你可以在全屏模式下啓動一個新的活動:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN); 

,並通過所述圖像URI或東西,其指示圖像源到新的活動:在FullImage Activity類

Intent intent = new Intent(YouActivity.this, FullImage.class); 
intent.putExtra("imageUri", R.drawable.yourImage); // or the path to your image. 

ImageView icon = (ImageView) findViewById(R.id.myImage); 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inTempStorage = new byte[3*1024]; 
Bitmap ops = BitmapFactory.decodeFile(path, options); // instead path you can get an image from previous activity. 
icon.setImageBitmap(ops);