Android - Imageview片段滞后

时间:2014-10-13 10:55:38

标签: java android android-fragments imageview

您好我有一个使用片段的相应标签的滑动视图。第一个视图只包含一个大约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页或其他方式时,动画似乎非常滞后。

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(包含一些带有信息的textviews)的转换是滞后的。有人可以帮助我吗?文件大小是否会促进滞后?

1 个答案:

答案 0 :(得分:1)

您的图像尺寸并不重要,您的图像分辨率很重要,因为为了以ARGB_8888格式显示图像,每个像素将占用4个字节,因此快速分析将产生:

不同尺寸的图片:

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

并且在您的计算机中,所有这些可能都不到1 MB。那你该怎么办?你必须缩小你的图像,然后分配到你的图像视图,看看

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);
}

Loading Large Bitmaps Efficiently

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