与Horizo​​ntalScrollView简单的图片库 [英] Simple image gallery with HorizontalScrollView

查看:198
本文介绍了与Horizo​​ntalScrollView简单的图片库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写有水平滚动的图片库。图片必须编程补充说。
我使用自定义的水平滚动视图处理和添加图像,如下code:

I'm writing an image gallery with horizontal scrolling. Images must be added programatically. I use a custom horizontal scroll view to process and add images as in the following code:

    public void setViewList(Integer linearLayoutId, Integer[] imageIdList,
                            Activity activity) {

    displayMetrics = ImageUtils.getDisplayMetric(activity);

    LinearLayout ll = (LinearLayout) findViewById(linearLayoutId);
    for (Integer imgId : imageIdList) {
        ImageView imageView = new ImageView(getContext());

        imageView.setImageResource(imgId);
        imageView.setScaleType(ImageView.ScaleType.MATRIX);

        Integer[] displayMetrics = ImageUtils.getDisplayMetric(activity);
        ImageUtils.scaleImage(imageView, displayMetrics[0], displayMetrics[1]);

        Integer[] dstDimension = ImageUtils.createDimension();
        ImageUtils.getImageSize(imageView, dstDimension);
        getImageSizeList().add(dstDimension);

        ll.addView(imageView);
    }

}

正如你可以看到我与扩展使用图像下面的方法(调用 ImageUtils.scaleImage(ImageView的,displayMetrics [0],displayMetrics [1])

    public static void scaleImage(ImageView imView, int screenWidth, int screenHeight) {
    Drawable temp = imView.getDrawable();

    Bitmap imBitmap = ((BitmapDrawable)temp).getBitmap();

    int imWidth = imBitmap.getWidth();
    int imHeight = imBitmap.getHeight();

    float xScale = ((float) screenWidth) / imWidth;
    float yScale = ((float) screenHeight) / imHeight;

    float scale = xScale <= yScale ? xScale : yScale;
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.postScale(scale, scale);

    Bitmap scBitmap = Bitmap.createBitmap(imBitmap, 0, 0, imWidth, imHeight, scaleMatrix, true);

    BitmapDrawable scDrBitmap = new BitmapDrawable(imView.getResources(), scBitmap);
    imView.setImageDrawable(scDrBitmap);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
    );

    layoutParams.gravity = Gravity.CENTER;

    imView.setLayoutParams(layoutParams);
}

我的main.xml布局是pretty简单:

My main.xml layout is pretty simple:

<?xml version="1.0" encoding="utf-8"?>
<android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/horizontalScrollView"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <LinearLayout android:orientation="horizontal"
                  android:id="@+id/mainLayout"
                  android:layout_gravity="center"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"/>
</android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView>

凡android.lessons.custom_horizo​​ntal_scroll.CustomHorizo​​ntalScrollView是实现一个简单的自定义Horizo​​ntalScrollView的。

Where the android.lessons.custom_horizontal_scroll.CustomHorizontalScrollView is implementation of a simple custom HorizontalScrollView.

有关测试我使用三星Galaxy S3和图像如下决议:(1)1290 * 990(2)1221 * 900。它看起来像什么:

For testing I use Samsung Galaxy S3 and images with the following resolution: (1) 1290*990 (2) 1221*900. What it looks like:

在很多情况下,一切都显示正常,但不时我得到错误的结果:第一个图像的应用将屏幕用下面的启动时间,我没有为什么会发生任何想法。

In many cases everything is displayed fine but from time to time I get the wrong result: the first image divides a screen with the following one at app start time and I don't have any idea why it happens.

感谢您的帮助。

推荐答案

这个问题与利用重力解决= Gravity.FILL_HORIZONTAL | Gravity.CENTER_VERTICAL,我编程设置在图像视图(方法ImageUtils.scaleImage)。

The problem was resolved with use gravity = Gravity.FILL_HORIZONTAL | Gravity.CENTER_VERTICAL, that i set programmatically on a image view (method ImageUtils.scaleImage).

这篇关于与Horizo​​ntalScrollView简单的图片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆