嗨,我正在开发一个将加载图像的应用程序,我希望将每个图像缩放到最大可能的尺寸,例如,如果图像是横向图像(如果宽度大于高度)我想拉伸宽度填充手机的宽度并缩放高度以保持其纵横比。如果高度大于宽度,即纵向图像,则应缩放图像以适合手机的高度,然后应调整宽度以保持纵横比,我在使其正常工作时遇到了一些麻烦,这就是我到目前为止,我们将不胜感激任何帮助。
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;
}
});