2015-07-10 68 views
0

我有一個viewpager,其中包含4個框架佈局,每個與不同的圖像作爲其背景。我嘗試了幾種加載和調整圖像大小的方法,但是當您滑動時,viewpager仍然很慢;因爲它必須相應地改變背景圖像。Android的ViewPager與ImageViews

其他應用程序,如腦力達人,最谷歌自己的應用程序有教程,但圖像和他們很順利。有什麼辦法可以達到同樣的效果;保持圖像爲背景,同時優化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); 
    } 
} 
+0

能否請您提供您至今的解決方案? –

+0

將圖像放在主佈局上。 –

+0

我已經嘗試使用BitmapFactory.Options來嘗試和調整圖像大小,我已經嘗試使用片段作爲viewpager項目和設置圖像作爲背景,我目前使用幀佈局作爲viewpager項目,因爲我認爲他們會更輕,但我仍然有同樣的問題。如果我刪除圖像背景,viewpager是光滑的。 –

回答

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]); 
    ... 
} 
+0

你的方法是有效的,並且性能改進的一個巨大變化是顯而易見的。爲此非常感謝。 但我想現在我的問題在於我用於背景(全屏)的不同圖像。最大的一個是大約130kb。難道它們太重了嗎?如果是這樣,我怎麼能在最大化性能的同時加載它們? –

+0

嘗試使用通用圖像加載器加載圖像。你可以加載圖片從URL,繪製和Url .. btw謝謝接受答案:) –