Android 为什么我的图像不能在每个安卓移动设备上正确缩放?我甚至试过DPI
我正在编写我的第一个android应用程序。我使用的是表面视图和画布。为了绘制图像,我使用以下代码:canvas.drawBitmap(位图,左,上,绘制)。问题是,在某些手机上,图片无法完全覆盖某些屏幕。我正在使用DP像素,但它仍然不起作用。我试着研究每件事,但没有任何效果。下面是我的代码Android 为什么我的图像不能在每个安卓移动设备上正确缩放?我甚至试过DPI,android,user-interface,scale,screen-size,android-screen-support,Android,User Interface,Scale,Screen Size,Android Screen Support,我正在编写我的第一个android应用程序。我使用的是表面视图和画布。为了绘制图像,我使用以下代码:canvas.drawBitmap(位图,左,上,绘制)。问题是,在某些手机上,图片无法完全覆盖某些屏幕。我正在使用DP像素,但它仍然不起作用。我试着研究每件事,但没有任何效果。下面是我的代码 public class MyClassMain extends SurfaceView implements Runnable { Bitmap menuScreenBac
public class MyClassMain extends SurfaceView implements Runnable
{
Bitmap menuScreenBackground;
public int dpToPx (int dp) //Used for all pixel values to make em into DP VALUES
{
DisplayMetrics displayMetrics = getContext ().getResources ().getDisplayMetrics ();
int px = Math.round (dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
public Bitmap getResizedBitmap (Bitmap bm, int newWidth, int newHeight) //RESIZES THE IMAGES
{
int width = bm.getWidth ();
int height = bm.getHeight ();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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 (bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
//Remember not to resize Inside loop
}
public void run ()
{
// TODO Auto-generated method stub
while (isRunning)
{
if (!ourHolder.getSurface ().isValid ())
continue;
canvas = ourHolder.lockCanvas ();
Paint paint = new Paint ();
menuScreenPic = BitmapFactory.decodeResource (getResources (), R.drawable.menuscreen);
menuScreenPic = getResizedBitmap (menuScreenPic, dpToPx(320), dpToPx(533), paint );//This is like my background picture. MAIN PROBLEM: This does fully cover my phone, but not other phones, it needs to strech out on my phone and other people phone.
canvas.drawBitmap (menuScreenPic, 0, 0, paint);//This is where i draw it.
ourHolder.unlockCanvasAndPost (canvas);
}
}
}
}
我什么都试过了。我使用DP而不仅仅是像素。但我的问题是(我认为)屏幕大小不同。我甚至试着在我的res文件夹中放入适当大小的图像(mdpi、hdpi、xhdpi等)
游戏在我的手机上完美展现(Galaxy s1)。它不完全适合我朋友的手机(Nexus 4)。在Nexus4上,它就像没有伸展到全屏一样。在我妈妈的手机上,它也在做同样的事情,而不是全屏幕显示
有简单的解决办法吗
dpToPx
应该类似于context.getResources().getDisplayMetrics().density*dp
您确定这是主要问题吗?