2012-06-09 54 views
0

我有一个功能,我用它来从可绘制文件夹返回位图。 (我不使用可绘制的DPI文件夹,浪费时间)屏幕尺寸百分比计算器返回错误尺寸

无论如何,该函数检索位图,位图以视图端口大小的指定百分比形式返回。

目前它返回比例如接地元件的指定的百分比少:

百分比应该是480个像素的宽度的100%。显然这应该是480,但它返回400?我必须在这里失去了一些简单的数学出什么好歹的代码如下:(?还我应该使用createscaledbitmap)

public Bitmap getBitmapSized(String name, int percentage, int screen_dimention, int frames, int rows) 
{ 
    _tempInt = _context.getResources().getIdentifier(name, "drawable", _context.getPackageName()); 
    _tempbitmap = (BitmapFactory.decodeResource(_context.getResources(), _tempInt, _BM_options)); 

    _bmWidth = _tempbitmap.getWidth()/frames; 
    _bmHeight = _tempbitmap.getHeight()/rows; 

    _newWidth = (screen_dimention/100) * percentage; 
    _newHeight = (_newWidth/_bmWidth) * _bmHeight; 

    //Round up to closet factor of total frames (Stops juddering within animation) 
    _newWidth = _newWidth * frames; 

    //Output the created item 
    Log.w("Screen Width: ", Integer.toString(screen_dimention)); 
    Log.w(name, "Item"); 
    Log.w(Integer.toString((int)_newWidth), "new width"); 
    Log.w(Integer.toString((int)_newHeight), "new height"); 

    //Create new item and recycle bitmap 
    Bitmap newBitmap = Bitmap.createScaledBitmap(_tempbitmap, (int)_newWidth, (int)_newHeight, false); 
    _tempbitmap.recycle(); 
    System.gc(); 

    return newBitmap; 
} 

回答

3
_newWidth = (screen_dimention/100) * percentage; 

做整数除法。

你可能想

_newWidth = (screen_dimention/100.0) * percentage; 

,或者如果_newWidth实际上应该被截断成整数,你可能想

_newWidth = (screen_dimention * percentage)/100; 

以后有截断。

+0

感谢这个修复它,我知道这是简单的东西,只是无法弄清楚。 –