我使用Html.fromHtml()方法获取图像。所以我使用的URLImageParser和URLDrawable类如下所示:Android HTML ImageGetter as AsyncTask。
我已经调整了两个来自URLImageParser的方法,以便在保持宽高比的同时将图像缩放到屏幕的宽度。改变的方法如下:
@Override
protected void onPostExecute(Drawable result) {
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
urlDrawable.setBounds(0, 0, 0+width, 0+height);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
// Pre ICS
URLImageParser.this.container.setEllipsize(null);
}
和
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
drawable.setBounds(0, 0, 0 + width, 0
+ height);
return drawable;
} catch (Exception e) {
return null;
}
}
我的问题是,当手机处于纵向方向时,图像看起来很好。但是,当我旋转手机时,图像的宽度会变为手机的新宽度,但高度不会改变,因此图像看起来会扭曲。
手机旋转时有没有办法重置高度?
很确定这在某个地方是个愚蠢的错误
答案 0 :(得分:0)
要检测屏幕旋转,您可以尝试以下方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}