【问题标题】:Android - Imageview Fragment laggyAndroid - Imageview 片段滞后
【发布时间】:2014-10-13 10:55:38
【问题描述】:

您好,我有一个滑动视图,其中包含使用片段的相应选项卡。第一个视图仅包含大约 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(包含一些带有信息的 textview)的转换是滞后的。有人可以帮我吗?文件大小是否会促进滞后?

【问题讨论】:

    标签: java android android-fragments imageview


    【解决方案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)来加载你的图片。

    【讨论】:

    • 谢谢,这似乎是一个可行的解决方案。唯一的事情是我希望图像填充操作栏下方的视图。这意味着每个设备的宽度和高度都会不同。您是否建议读出屏幕的尺寸,然后将它们与 decodesampledbitmap* 一起使用?
    • 是的,获取ImageView 的尺寸,然后使用decodeSampledBitmapFromResource 设置图像。
    • 好吧,我都做了。通过 ImageView.setBitmapImage 设置图像。但是从 fragment1 到 fragment2 的过渡仍然滞后。有什么建议吗?
    • 你的图片有多大?将int inSampleSize = 1; 更改为int inSampleSize = 4; 或更高,如8、16、...乘以2。
    • 谢谢 :D 我把它改成了 4 就解决了这个问题。不再落后。至少在我的 HTC 一台 m8 上。 insamplesize 到底是什么?我的图片是 1920x1080 和大约 2mb,在 jpeg 中。
    猜你喜欢
    • 2019-08-05
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多