gpt4 book ai didi

不同分辨率缩放布局的Android调整大小按钮

转载 作者:行者123 更新时间:2023-11-29 01:52:12 25 4
gpt4 key购买 nike

这是否适用于所有 Android 设备?我有一个 imageview 作为背景,我根据 android 设备的分辨率调整它的大小。

private Bitmap getResizedBitmap() 
{
Bitmap resizedBitmap;
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.background_3ball);
x640 = bmp.getWidth();
y960 = bmp.getHeight();

if(screenRatio > ratioOrginal)
{
imageWidth=(int)((double)y960*widthMain/heightMain);
imageHeight=y960;
int x = (x640-imageWidth)/2;
resizedBitmap = Bitmap.createBitmap(bmp,x,0, imageWidth, imageHeight);
}
else if(screenRatio < ratioOrginal)
{
imageWidth=x640;
imageHeight=(int)((double)x640*heightMain/widthMain);
int y = (y960-imageHeight)/2;
resizedBitmap = Bitmap.createBitmap(bmp,0,y, imageWidth, imageHeight);
}
else
{
return bmp;
}



tw.setText(widthMain + "\n" + heightMain + "\n" + imageWidth + "\n" + imageHeight);

return resizedBitmap;
}

private void SetBackground()
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
widthMain = metrics.widthPixels;
heightMain = metrics.heightPixels;
screenRatio = ((double)heightMain) / widthMain;

Bitmap resizedBitmap = getResizedBitmap();

imageBackground.setImageBitmap(resizedBitmap);
}

使用 dp 或其他东西会使图像看起来很糟糕。例如,我在背景中有一个圆圈,在自动缩放时它看起来像一个椭圆

 for example my image has dimensions of 640x960.
ratioOrginal is 960:640=1.5
if a device is mdpi and having dimension of 320x480
screenRatio=480/320=1.5 and it is okay.
but if it is mdpi and having dimension of 480x800
screenRatio=900/480 is NOT 1.5.
both are mdpi, but the ratio is not standard.
I can not understand how people do this
in supporting multiple screens there is one xml for mdpi's
how it get's different resolutions.

最佳答案

我建议使用 ScaleType相反。

Android 推荐使用 dp 并为不同的屏幕尺寸创建不同的布局文件。

Supporting Multiple Screens

关于不同分辨率缩放布局的Android调整大小按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17233058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com