Android 带有 ImageView 的 ViewPager

Android ViewPager with ImageViews

我有一个 viewpager,它包含 4 个框架布局,每个框架布局都有不同的图像作为背景。我已经尝试了几种加载和调整图像大小的方法,但是当您滑动时 viewpager 仍然很慢;因为它必须相应地更改背景图像。

QuizUp 等其他应用程序和大多数 Google 自己的应用程序在教程中都有图像,但它们非常流畅。有什么办法可以达到同样的效果;在优化 viewpager 性能的同时将图像保留为背景?

这是我的 viewpager 适配器;

private class ScreenSlidePagerAdapter extends PagerAdapter {

    Context context;
    LayoutInflater layoutInflater;

    public ScreenSlidePagerAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView text1 = null, text2 = null, text3 = null, text4 = null;
        ImageView background;
        Button begin;

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = null;

        switch (position) {
            case 0:
                layout = layoutInflater.inflate(R.layout.lifestyle, container, false);
                text1 = (TextView) layout.findViewById(R.id.text3);
                text2 = (TextView) layout.findViewById(R.id.text4);
                text3 = (TextView) layout.findViewById(R.id.text5);
                background = (ImageView) layout.findViewById(R.id.background);

                background.setImageResource(R.drawable.tlifestyle);
                break;
            case 1:
                layout = layoutInflater.inflate(R.layout.sports, container, false);
                text1 = (TextView) layout.findViewById(R.id.text3);
                text2 = (TextView) layout.findViewById(R.id.text4);
                text3 = (TextView) layout.findViewById(R.id.text5);
                background = (ImageView) layout.findViewById(R.id.background);

                background.setImageResource(R.drawable.tsports);
                break;
            case 2:
                layout = layoutInflater.inflate(R.layout.events, container, false);
                text1 = (TextView) layout.findViewById(R.id.text1);
                text2 = (TextView) layout.findViewById(R.id.text2);
                text3 = (TextView) layout.findViewById(R.id.text3);
                text4 = (TextView) layout.findViewById(R.id.text4);
                background = (ImageView) layout.findViewById(R.id.background);

                background.setImageResource(R.drawable.tevents);
                break;
            case 3:
                layout = layoutInflater.inflate(R.layout.intro_to_categories, container, false);
                text1 = (TextView) layout.findViewById(R.id.welcomeText);
                text2 = (TextView) layout.findViewById(R.id.findText);
                text3 = (TextView) layout.findViewById(R.id.dont_stress);
                text4 = (TextView) layout.findViewById(R.id.dont_stress_2);
                begin = (Button) layout.findViewById(R.id.begin);

                Typeface typeface = Typeface.createFromAsset(getAssets(), "mpashofont.otf");
                begin.setTypeface(typeface);

                begin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(AppTutorial.this, AppSetup.class));
                    }
                });
                break;
        }

        Typeface typeface = Typeface.createFromAsset(getAssets(), "mpashofont.otf");
        assert text1 != null && text2 != null && text3 != null;
        text1.setTypeface(typeface);
        text2.setTypeface(typeface);
        text3.setTypeface(typeface);
        if (text4 != null) {
            text4.setTypeface(typeface);
        }

        container.addView(layout);

        return layout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((FrameLayout) object);
    }
}

我猜根据你的适配器代码 我可以看到您有 3 个文本视图和 1 个背景图像用于所有不同的膨胀布局。

并且您要在每次滑动时创建字体,因此请将此字体初始化代码放入您的构造函数中

最好根据位置更改数据。

像这样

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    TextView text1 = null, text2 = null, text3 = null, text4 = null;
    ImageView background;

    View itemView = mLayoutInflater.inflate(R.layout.your_layout, container, false);
    text1 = (TextView) layout.findViewById(R.id.text3);
    text2 = (TextView) layout.findViewById(R.id.text4);
    text3 = (TextView) layout.findViewById(R.id.text5);
    background = (ImageView) layout.findViewById(R.id.background);

   text1.setText(arr[position]);
   ...
}