How to scale small imageview to fill large scrollview?

て烟熏妆下的殇ゞ 提交于 2020-01-17 04:04:00

问题


How do you create a layout with a screen-filling scrollview that has a small image in an imageview scaled to fit the whole scrollview and be scrollable? eg. A 720x1280 display has a full-screen scrollview (fill_parent). Inside is a linearlayout (fill_parent). In that is a 300x900 bitmap in an imageview that is upscaled to 720x2160, fills the screen width and exceeds the vertical bounds, and can be scrolled up/down in the scrollview.

The solution should work for all screen sizes to support multiple devices from mobiles to tablets.


回答1:


This works for me:

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

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

        <ImageView
            android:layout_width="720dp"
            android:layout_height="2160dp"
            android:src="@drawable/image"
            android:scaleType="fitXY"/>

    </LinearLayout>

</ScrollView>

If you need to make this work for any screen size do it programmatically:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    imageView.getLayoutParams().width = width;
}



回答2:


if scroll view height and width is fill parent than if background is set with an small image it will automatically expand in your layout



来源:https://stackoverflow.com/questions/16858492/how-to-scale-small-imageview-to-fill-large-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!