我有一個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);
}
}
能否請您提供您至今的解決方案? –
將圖像放在主佈局上。 –
我已經嘗試使用BitmapFactory.Options來嘗試和調整圖像大小,我已經嘗試使用片段作爲viewpager項目和設置圖像作爲背景,我目前使用幀佈局作爲viewpager項目,因爲我認爲他們會更輕,但我仍然有同樣的問題。如果我刪除圖像背景,viewpager是光滑的。 –