Android:imageview使用可缩放读取资产缩放到非常小的尺寸

时间:2014-08-19 02:53:35

标签: android android-layout imageview

我正在尝试实现一个图库,我正在使用带有LinearLayout的Horizo​​ntalScrollView,并将图像动态地放入LinearLayout。我的图像是从assets文件夹的子目录中读取的。 我有不同大小的图像。

我的布局xml文件如下:

<RelativeLayout 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"
    android:padding="20dp"
    tools:context="com.microsoft.stc.indoormonkey.view.ShopDetailActivity">

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="15dp"
        >

        <LinearLayout
            android:id="@+id/layout_infoContainer"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:orientation="horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1"
                android:src="@drawable/z_001"
                android:adjustViewBounds="true"
                android:layout_marginRight="2dp"
                />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:src="@drawable/z_002"
                android:adjustViewBounds="true"
                android:layout_marginRight="2dp"
                />
        </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>

我使用两个静态ImageViews进行测试,以查看获取正确视图所需的参数。我将两个测试图像文件复制到res文件夹中以便在xml中访问。

但是如果我以编程方式设置布局参数:

private ImageView newImageView(Drawable drawable) {
    ImageView imageView = new ImageView(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
    layoutParams.setMargins(0, 0, 2, 0);
    imageView.setLayoutParams(layoutParams);

    imageView.setAdjustViewBounds(true);
    imageView.setImageDrawable(drawable);
    return imageView;
}

图像有点缩放到非常小的尺寸,如this。您可以看到静态添加的图像正确显示但其他图像没有显示。

我在newImageView函数中使用的可绘制文件是从assets文件夹中读取的。实际上,如果我通过以下方式更改drawable文件:

imageView.setImageDrawable(getResources().getDrawable(R.drawable.z_001));

它显示正确。 所以我认为问题必须与我的可绘制文件相关

我正在阅读并在活动的onCreate函数中设置图像,读取代码为:

AssetManager assetManager = getResources().getAssets();
try {
    InputStream inputStream = assetManager.open(fileName);
    drawable = Drawable.createFromStream(inputStream, null);
} catch (IOException e) {
    e.printStackTrace();
    Log.e("ERROR", "Invalid drawable file path!");
}

任何建议都会有所帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

我无法运行代码ATM,因此它不是一次性答案,但这三者中的一个应该可以解决问题。

首先,检查您是否有适合放置在-ldpi,-mdpi,-hdpi和/或-xhdpi文件夹中的drawable的资源,或者它是否在common / drawables /中。框架可能从不同的文件夹加载,如果它在/ drawables /.

中,则不能正确地对其进行sc

第二种方法是尝试为ImageView设置android:scaleType。对于图片库,它通常是android:scaleType="centerInside"

如果所有其他方法都失败了,只需手动完成,那么您就拥有了所有控件:

Bitmap myImage= BitmapFactory.decodeResource(getResources(), R.drawable.z_001);

//apply some method to calculate scale factor for width based on resolved bitmap size and imageview size or screen size or whatever
float scaleX = calculateWidth(imageView, myImage);
//apply some method to calculate scale factor for height based on resolved bitmap size and imageview size or screen size or whatever 
float scaleY = calculateHeight(imageView, myImage);

// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scaleX, scaleY);

Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, myImage.getWidth(), myImage.getHeight(), matrix, true);

BitmapDrawable result = new BitmapDrawable(scaledBitmap);
imageView.setImageDrawable(result);

// If you wish to resize imageView to match scaled drawable 
int width = scaledBitmap.getWidth();
int height = scaledBitmap.getHeight();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams(); 
params.width = width;
params.height = height;
imageView.setLayoutParams(params);

希望这会有所帮助。干杯!