Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
旋转手机时,Android图像会被拉伸_Android - Fatal编程技术网

旋转手机时,Android图像会被拉伸

旋转手机时,Android图像会被拉伸,android,Android,我使用Html.fromHtml()方法获取图像。因此,我使用的是URLImageParser和URLDrawable类,定义如下: 为了在保持纵横比的同时将图像缩放到屏幕的宽度,我采用了URLImageParser中的两种方法。更改的方法如下: @Override protected void onPostExecute(Drawable result) { DisplayMetrics dm = new DisplayMetrics();

我使用Html.fromHtml()方法获取图像。因此,我使用的是URLImageParser和URLDrawable类,定义如下:

为了在保持纵横比的同时将图像缩放到屏幕的宽度,我采用了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);
    }

我的问题是,当手机保持纵向时,图像看起来很好。但是,当我旋转手机时,图像的宽度变为手机的新宽度,但是高度没有改变,因此图像看起来失真

有没有办法在手机旋转时重置高度


可以肯定的是,在某些地方检测屏幕旋转是一个愚蠢的错误,您可以尝试以下方法:

@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();
    }
}

这是

@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();
    }
}