Android-Imageview片段滞后 [英] Android - Imageview Fragment laggy

查看:77
本文介绍了Android-Imageview片段滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用片段的相应标签的滑动视图.第一个视图仅包含完整的高清图像,大小约为1.91mb.

Hi I have a swipe view with corresponding tabs using fragments. The first view only contains a full hd image in the size of about 1.91mb.

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/my_picture"
    android:src="@drawable/image1" />

</LinearLayout>

现在,当我从第1页滑到第2页或以其他方式在动画周围滑动时,

Now when I swipe from Page 1 to Page 2 or the other Way around the animation seems really laggy.

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;

        switch(position) {
        case 0:
            fragment = new Tab_one();
            break;
        case 1:
            fragment = new Tab_two();
            break;
        case 2:
            fragment = new Tab_three();
            break;
        default:
            fragment = new Tab_one();
            break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}
}

因此,从page1(仅包含imageview)到page2(包含一些带有信息的textview)的转换比较滞后.有人可以帮我吗?文件大小是否会导致延迟?

So the transition from page1(contains only the imageview) to page2(contains a few textviews with information) is laggy. Can someone help me there? And does the filesize promote the lagging?

推荐答案

您的图片大小并不重要,您的图片分辨率也很重要,因为为了以例如ARGB_8888格式显示图片,每个像素将占用4个字节,因此快速分析将得出:

Your image size is not important, your image resolution is important because in order to show an image for example in ARGB_8888 format each pixel will take 4 bytes so quick analyses will yield :

具有不同尺寸的图像:

image: 1024 * 768 * 4 = 2MB 
image: 1920 * 1080* 4 = 6MB
image: 1280 * 720 * 4 = 3MB

,并且在您的计算机中所有这些文件可能都小于1 MB.那你该怎么办?您必须按比例缩小图片,然后分配给图片视图,看看

and in your computer all of them might be less than 1 MB. so what should you do? you must scale down your image and then assign to your image view, take a look at

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

有效地加载大型位图

您可以使用decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)加载图像.

这篇关于Android-Imageview片段滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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