简体   繁体   English

Android-Imageview片段滞后

[英]Android - Imageview Fragment laggy

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. 第一个视图仅包含约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>

Now when I swipe from Page 1 to Page 2 or the other Way around the animation seems really laggy. 现在,当我从页面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;
    }
}
}

So the transition from page1(contains only the imageview) to page2(contains a few textviews with information) is laggy. 因此,从page1(仅包含imageview)到page2(包含一些带有信息的textview)的转换比较滞后。 Can someone help me there? 有人可以帮我吗? And does the filesize promote the lagging? 文件大小是否会导致延迟?

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 : 图像大小并不重要,图像分辨率也很重要,因为为了以ARGB_8888格式显示图像,每个像素将占用4个字节,因此快速分析将得出:

image with different size: 大小不同的图片:

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

and in your computer all of them might be less than 1 MB. 并且在您的计算机中所有这些文件可能都小于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);
}

Loading Large Bitmaps Efficiently 有效地加载大位图

you can use decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) to loading your image. 您可以使用decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)加载图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM