0
我有5 SlidingTabs
。我想將ViewPager
animation
放在其中一個。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
以及我需要這樣做的地方? 如果它不是最好的動畫方式,請告訴我你的變種
在此先感謝!
似乎工作正常!謝謝! –