gpt4 book ai didi

Android将图像缩放到最大尺寸

转载 作者:行者123 更新时间:2023-11-29 00:38:57 25 4
gpt4 key购买 nike

您好,我正在开发一个将加载图像的应用程序,我希望将每个图像缩放到它的最大可能尺寸,例如,如果图像是横向图像(如果宽度大于高度),我想拉伸(stretch)宽度以填充手机的宽度并缩放高度以保持其纵横比。如果高度大于宽度,即肖像图像,图像应该缩放以适合手机的高度,然后宽度应该调整以保持纵横比我在让它工作时遇到了一些麻烦这就是我的'到目前为止,我们将不胜感激任何帮助。

            final ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setBackgroundResource(android.R.color.black);
//TODO need to get actual size of drawable not view size which is 0
ViewTreeObserver vto = i.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int w = i.getMeasuredWidth();
int h = i.getMeasuredHeight();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if(w <= h){
//TODO need to think about landscape
h = height - convertDpToPixel(50, context);
w = w*(width);
}else{
h = h*(height);
w = width;
}
//TODO set imageview to w and h

return true;
}
});

最佳答案

获取显示宽度,高度

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;

用于调整 ImageView 的大小

ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);

关于Android将图像缩放到最大尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772157/

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