扩展的ImageView到设备的宽度宽度、设备、ImageView

2023-09-12 11:18:03 作者:傲娇の仙女

在我的Andr​​oid应用我有一个显示具有以下尺寸图像的活动 244 x 330 。 我想显示完整的设备宽的图像。

In my Android App I have a Activity which show images which have following size 244 x 330. I want to show those images in full device width.

我的布局文件看起来是这样的:

My layout file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical">

            <ImageView android:id="@+id/news_image" 
                android:layout_height="fill_parent" 
                android:layout_width="fill_parent"
                android:layout_marginLeft="18dip"
                android:layout_marginRight="18dip"
                android:background="#aaaaaa" />


    </LinearLayout>

</ScrollView>

我试图设置的ImageView的ScaleType但没有ScalingType这将缩放ImageView的比例。 我怎样才能将图像缩放比例,以适应整个屏幕在横向模式和肖像模式?

I tried to set the ScaleType of the ImageView but there is no ScalingType which scales the ImageView proportional. How can I scale the image proportionally to fit the whole screen in landscape mode AND portrait mode?

基本上我想要的是像ScaleType.CENTER_CORP但也设置了比例高度图像,这样我可以看到这一切和图像的不只是一个组成部分。

Basically what I want is something like ScaleType.CENTER_CORP but which also sets the proportional height for the image so I can see all of it and not just a part of the image.

修改,因为我知道我混淆你我的怪异的追问。

我想用一个形象展现给你。 这是我得到的那一刻与我的布局。我想根据需要缩放图像一样大,填补了整个灰色地带。我怎样才能实现这个目标?

I want to show it to you with a image. This is what I get at the moment with my layout. I want to fill the whole grey area by scaling the image as big as needed. How can I accomplish that?

当我设置了 ScaleType CENTER_CROP 我得到这个

When I set the ScaleType to CENTER_CROP I get this

但是这不是我想要的,因为你没有看到整个图像只是一部分从中心。

but this is not what I want cause you are not seeing the whole image just a part from the center.

这就是我希望它是:

我希望这可以帮助你了解我试图完成。 任何人都知道该怎么做?

I hope this helps you to understand what I'm trying to accomplish. Anyone knows how to do that?

编辑2:

它看起来怪异的有点混乱,我想要显示的图像,在更大的高度大于屏幕尺寸,但由于我使用的是滚动型在我的例子的布局,这应该是确定,用户可以滚动,如果他想看到的不显示区域。

It might look weird and little confusing that I'm trying to display a image which is bigger in the height than the screen size but since I'm using a ScrollView in my example layout this should be ok and the user could scroll if he want to see the not displayed area.

希望这有助于理解我想要做的事情。

Hope this helps to understand what I'm trying to do.

推荐答案

我想真的每个 ScaleType 在我的的ImageView FILL_PARENT WRAP_CONTENT ,但没有一次成功。我也尝试了所有我发现在谷歌,但没有为我工作或者使想出了一些我自己的。

I tried really every ScaleType in my ImageView with fill_parent and wrap_content but no of them worked. I also tried everything what I found on Google but nothing worked for me either so came up with something on my own.

显然,ImageView的不结垢我的形象像我想扩大规模,所以我不得不缩减它在我自己的。缩放位图后,我将设置新的位图图像源的ImageView的。此工程pretty的好,看起来很不错的G1和摩托罗拉里程碑2。

It was clear that the ImageView is not scaling my image like I wanted to be scaled so I had to scale it on my own. After scaling the bitmap I would set the new Bitmap as the image source to the ImageView. This works pretty good and looks very good on the G1 and on the Motorola Milestone 2.

和这里是我的code所有作品

And here is all pieces of my code

布局:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/news_wrapper">

            <ImageView android:id="@+id/news_image" 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_marginLeft="18dip"
                android:layout_marginRight="18dip"
                android:background="#aaaaaa" />

    </LinearLayout>

</ScrollView>

活动

public class ScalingImages extends Activity {

    private ImageView imageView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_margin);

        this.imageView = (ImageView)findViewById(R.id.news_image);

        // The image is coming from resource folder but it could also 
        // load from the internet or whatever
        Drawable drawable = getResources().getDrawable(R.drawable.img);
        Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

        // Get scaling factor to fit the max possible width of the ImageView
        float scalingFactor = this.getBitmapScalingFactor(bitmap);

        // Create a new bitmap with the scaling factor
        Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);

        // Set the bitmap as the ImageView source
        this.imageView.setImageBitmap(newBitmap);
    }

    private float getBitmapScalingFactor(Bitmap bm) {
        // Get display width from device
        int displayWidth = getWindowManager().getDefaultDisplay().getWidth();

        // Get margin to use it for calculating to max width of the ImageView
        LinearLayout.LayoutParams layoutParams = 
            (LinearLayout.LayoutParams)this.imageView.getLayoutParams();
        int leftMargin = layoutParams.leftMargin;
        int rightMargin = layoutParams.rightMargin;

        // Calculate the max width of the imageView
        int imageViewWidth = displayWidth - (leftMargin + rightMargin);

        // Calculate scaling factor and return it
        return ( (float) imageViewWidth / (float) bm.getWidth() );
    }
}

的Util类

Util class

public class Util {

    public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
        int scaleHeight = (int) (bm.getHeight() * scalingFactor);
        int scaleWidth = (int) (bm.getWidth() * scalingFactor);

        return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
    }

}

如果有一个更好或更准确的方式来完成同样的比例请让我知道,因为我无法相信,这样一个平凡的事情是如此难以完成的任务。

If there is an better or more accurate way to accomplish the same scaling please let me know because I can't believe that such a trivial thing is so hard to accomplish.

我真的希望能看到一个更好的方式来做到这一点。

I'm really hoping to see a better way to do this.

感谢您的阅读。

 
精彩推荐
图片推荐