一个 ScrollView 内部的 ImageView

4

我正在开发一个安卓项目。在这个项目中,我需要处理ScrollView来滚动图片。但是当我在ScrollView里使用ImageView时,给我的图片不适合UI布局。所以我想把ScrollView转换成ImageView。在安卓中有没有这样的方法呢?请帮帮我。

6个回答

17

我找到了一个好的解决方案,在你的ImageView参数中,只需添加-

android:adjustViewBounds="true"

3

试试这个:

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

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_launcher" 
                android:contentDescription="@string/describe_something_here"/>

        </RelativeLayout>

    </ScrollView>

希望这能帮到你。

1

如果您想将ImageView适配到模拟器屏幕中,为什么要使用ScrollView呢?您可以像这样在模拟器屏幕中缩放您的ImageView。

 LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);

希望能有所帮助


谢谢回复。但我正在进行的项目需要使用滚动视图,因为它将被广泛使用。 - user1184851
请告诉我方便的方法。 - user1184851

-2

你好,我认为你可以这样解决你的问题

<anyLayout>
   <ScrollView>
        <RelativeLayout>

            <ImageView/>

         </RelativeLayout>

   </ScrollView>

</anyLayout>

滚动视图在布局内工作。


我使用与上述相同的方法,但我的图像分辨率超出了屏幕范围。但我必须使其完全适合模拟器屏幕。因此,我尝试使用android:background属性,并在.java文件中尝试将滚动视图转换为图像视图。如果有任何方法,请告诉我。提前致谢。 - user1184851

-2
<ScrollView>
        <RelativeLayout>

            <ImageView/>

         </RelativeLayout>

   </ScrollView>

这将对你有用


根据您之前的评论,不要在XML中设置图像,而是在类文件中使用setbackgroundresource。 - Donald

-3

我找到了一个非常简单的解决方案,你只需要添加

android:src="@drawable/hayhouse"

在布局的xml文件中,将其返回到您的ImageView。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接