带有ImageViews的Android ViewPager

时间:2015-07-10 09:52:51

标签: android imageview android-viewpager

我有一个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);
    }
}

1 个答案:

答案 0 :(得分:2)

我猜你的适配器代码  我可以看到所有不同的充气布局都有3个textview和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]);
   ...
}