2013-08-19 224 views
0

實際上,我試圖創建一個簡單的應用程序與壁紙。android縮放位圖到屏幕尺寸

我想縮放我的圖像到用戶設備的屏幕大小。 我用這樣的代碼波紋管:

/* 

    DisplayMetrics metrics = new DisplayMetrics(); 

    getWindowManager().getDefaultDisplay().getMetrics(metrics); 

    int height = metrics.heightPixels; 
    int width = metrics.widthPixels; 

*/ 


Display metrics = getWindowManager().getDefaultDisplay(); 

int height = metrics.getHeight(); 
int width = metrics.getWidth(); 
float fourToThree; 
int wymiar; 
Bitmap mbitmap; //definicja zmiennej przechowującej bitmapę 
try { 

    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), cardBase.cardImages[position]); 
    //UP - zapisanie obrazu z tablicy do zmiennej bitmapy 
    /*if(height>=width) { 
     fourToThree = height * 0.75f; //height 
     wymiar = (int)Math.round(fourToThree); 
     mbitmap = Bitmap.createScaledBitmap(myBitmap, height, wymiar, true); 
    } else {  
     fourToThree = height * 0.75f; 
     wymiar = (int)Math.round(fourToThree); 
     mbitmap = Bitmap.createScaledBitmap(myBitmap, width, wymiar, true); 
    }*/ 
    mbitmap = Bitmap.createScaledBitmap(myBitmap, width, height, true); 
    myWallpaper.setBitmap(mbitmap); 
    Toast.makeText(SelectedCard.this, "Wallpaper changed", Toast.LENGTH_LONG).show(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
    Toast.makeText(SelectedCard.this, "Sorry, an error occurred", Toast.LENGTH_LONG).show(); 
} 

我嘗試很多其他方式,但仍然有我的形象大則設備屏幕:(

我測試它virtual phone,我試圖創建圖像DIT 160 dpi作爲設備屏幕,它不工作太

誰能告訴我,我怎麼能縮放我的圖像(JPG - 320×480像素,300 dpi)設置爲設備上的牆紙?

任何想法?

謝謝:)

Ps。遺憾的文字錯誤,英語是我的第二語言,P


好吧,我有類似的東西:)

imgView.setOnClickListener(新View.OnClickListener({

 @Override 
     public void onClick(View v) { 
      WallpaperManager myWallpaper = WallpaperManager.getInstance(getApplicationContext()); 
      try 
      /*{ 
       myWallpaper.setResource(HdImageBase.HdImages[position]); 
       Toast.makeText(ImageMini.this, "Wallpaper changed", Toast.LENGTH_LONG).show(); 
      }*/ 
      { 
       DisplayMetrics metrics = new DisplayMetrics(); 

       getWindowManager().getDefaultDisplay().getMetrics(metrics); 

       int height = metrics.heightPixels; 
       int width = metrics.widthPixels; 
       float fourToThree; 
       int wymiar; 
       Bitmap mbitmap; //definicja zmiennej przechowującej bitmapę 

       Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), cardBase.cardImages[position]); 

       mbitmap = Bitmap.createScaledBitmap(myBitmap, width, height, true); 
       myWallpaper.setBitmap(mbitmap); 

       Toast.makeText(SelectedCard.this, "Wallpaper changed \n" + width + "\n" + height, Toast.LENGTH_LONG).show(); 
       } 
      catch (IOException e) { 
       e.printStackTrace(); 
       Toast.makeText(SelectedCard.this, "Sorry, an error occurred", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

寬度和高度值是正確的320×480但我設置爲壁紙的圖像仍然更大,那麼我的設備scren。

經過測試後,我的真正的手機LG L5新的androi d(不知道哪個版本)。圖像被設置爲壁紙正確(在肖像模式下 - 所有5個「捲簾」都沒有縮放的圖像)。

我如何在其他設備上測試它? 意思是...這是壁紙肖像模式只適用於新的Android版本?

+0

謝謝你們,但我說的是圖像的尺寸變化這將被設置爲手機中的壁紙。我不想把它放到ImageView :)例如。點擊其中一個圖像後,我將圖像設置爲手機壁紙(在主屏幕上 - 「桌面」),我有一系列圖像(280 x 320)。像在這個應用程序: https://play.google.com/store/apps/details?id=com.stobolewski.animalswalpapers&hl=pl – Gwynbleid

回答

0

試一下這個可能工作: ImageView的屬性:android:scaleType=「fitXY」

+0

好吧,我有這樣的事情: – Gwynbleid

+0

但它的工作? – KOTIOS

+0

看3帖子(我添加新的評論我的問題:/)。 – Gwynbleid

6

你可以用這個尺度位..

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 

Bitmap bitmapOrg = new BitmapDrawable(getResources(), new ByteArrayInputStream(imageThumbnail)).getBitmap(); 

int width = bitmapOrg.getWidth(); 
int height = bitmapOrg.getHeight(); 

float scaleWidth = metrics.scaledDensity; 
float scaleHeight = metrics.scaledDensity; 

// create a matrix for the manipulation 
Matrix matrix = new Matrix(); 
// resize the bit map 
matrix.postScale(scaleWidth, scaleHeight); 

// recreate the new Bitmap 
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); 
+0

智能答案嗯.... bahut sahi chite –

+0

哈哈哈謝謝@ShashankSingh –

+0

Welocme gori ... :) aur nayi局k裏耶lakh lakh badhaniya –

0

有2種方式來擴展位,以適應屏幕:

1st:

下面的函數繪製指定的位圖,縮放/自動轉換填補了目標矩形:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Rect src, dst; 
    int widthOfBitmapSrc, heightOfBitmapSrc; 

    widthOfBitmapSrc = mBitmapSrc.getWidth(); 
    heightOfBitmapSrc = mBitmapSrc.getHeight(); 
    src = new Rect(0, 0, widthOfBitmapSrc, heightOfBitmapSrc);//size of bitmap 

    dst = new Rect(0,0,this.getWidth(),this.getHeight());//size of this view 

    canvas.drawBitmap(mBitmapSrc, src, dst, null);//scale bitmap from src to dst 
} 

看到此鏈接:http://developer.android.com/reference/android/graphics/Canvas.html

第二:

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

    int widthOfNewBitmap, heightOfNewBitmap; 

    widthOfNewBitmap = this.getWidth();//width of this view 
    heightOfNewBitmap = this.getHeight();//height of this view 

    Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmapSrc, widthOfNewBitmap,heightOfNewBitmap,true); 

    canvas.drawBitmap(mBitmapSrc, 0, 0, null);//copy bitmap to canvas of this view without scale 
}