0

我有5 SlidingTabs。我想將ViewPageranimation放在其中一個。ViewPager動畫outOfMemoryError

片段類:

public class AdFragment extends Fragment { 

int[] mResources = { 
     R.drawable.six, 
     R.drawable.five, 
     R.drawable.four, 
     R.drawable.eight, 
     R.drawable.one, 
     R.drawable.two, 
     R.drawable.three, 
     R.drawable.seven, 
     R.drawable.nine, 
     R.drawable.ten, 
}; 
int currentPage; 
public AdFragment() { 
    // Required empty public constructor 
} 
Timer swipeTimer; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_ad, container, false); 
    CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(getActivity(),mResources); 

    final ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager); 
    mViewPager.setAdapter(mCustomPagerAdapter); 

    final Handler handler = new Handler(); 
    final Runnable Update = new Runnable() { 
     public void run() { 
      if (currentPage == mResources.length) { 
       currentPage = 0; 
      } 
      mViewPager.setCurrentItem(currentPage++, true); 
     } 
    }; 

    swipeTimer = new Timer(); 
    swipeTimer.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      handler.post(Update); 
     } 
    }, 500, 3000); 

    return view; 
} 

}

適配器類:

class CustomPagerAdapter extends PagerAdapter { 

Context mContext; 
LayoutInflater mLayoutInflater; 
int[] mResources; 
private Resources resource; 

public CustomPagerAdapter(Context context,int[] mResources) { 
    mContext = context; 
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.mResources = mResources; 
} 

@Override 
public int getCount() { 
    return mResources.length; 
} 

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

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false); 

    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); 
    imageView.setImageResource(mResources[position]); 

    container.addView(itemView); 

    return itemView; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView((LinearLayout) object); 
} 
@Override 
public Object instantiateItem(View view, int position) { 
    ImageView myView = new ImageView(mContext); 

    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), mResources[position]); 
    myView.setImageBitmap(bitmap); 

    ((ViewPager) view).addView(myView); 
    return myView; 
} 
} 

目前我有10個圖像slideshow.When我睜開Tab與上述Fragment,等待一段時間,然後嘗試去另一個Tab,並再次回到此 - 我得到:

01-27 14:13:53.794 12652-12652/igor.com.totalsoulbeta E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: igor.com.totalsoulbeta, PID: 12652 
                    java.lang.OutOfMemoryError 
                     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
                     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587) 
                     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422) 
                     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) 
                     at android.content.res.Resources.loadDrawable(Resources.java:2113) 
                     at android.content.res.Resources.getDrawable(Resources.java:700) 
                     at android.content.res.XResources.getDrawable(XResources.java:521) 
                     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323) 
                     at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175) 
                     at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168) 
                     at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51) 
                     at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72) 
                     at igor.com.totalsoulbeta.CustomPagerAdapter.instantiateItem(CustomPagerAdapter.java:46) 

每張圖片的大小不超過150kb。我的問題是:是否可以使用25張圖像進行此類動畫而不使用OutOfMemoryError?如果是的話,我需要在我的代碼中更改什麼?我如何清除heap以及我需要這樣做的地方? 如果它不是最好的動畫方式,請告訴我你的變種

在此先感謝!

回答

0

A ViewPager應該能夠顯示任意數量的頁面。在任何時候,它只有3頁內存(除非你改變屏幕外頁面限制)。這個問題可能與Timer泄漏有關。該documentation說:

當不再需要一個定時器,用戶應該調用取消(),它 釋放定時器的線程和其他資源。定時器不明確 可能會無限期地保存資源。

如果您在Fragment中添加類似內容,有幫助嗎? :

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 

    swipeTimer.cancel(); 
    swipeTimer = null; 
} 
+0

似乎工作正常!謝謝! –