我有5 SlidingTabs
。我想将ViewPager
animation
放入其中一个。
片段类:
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张图片。当我用上述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。我的问题是:是否可以在没有OutOfMemoryError
的情况下拍摄25张图像的动画?如果是,我需要在代码中更改什么?我如何清除heap
以及我需要做什么?
如果它不是动画的最佳方式,请告诉我您的变体
提前致谢!
答案 0 :(得分:0)
ViewPager
应该能够显示任意数量的页面。在任何时候它只有3页内存(除非您更改屏幕外页面限制)。问题可能在于Timer
泄漏的东西。 documentation说:
当不再需要计时器时,用户应该调用cancel(),这就是 释放计时器的线程和其他资源。定时器没有明确说明 取消可能无限期地持有资源。
如果在Fragment
中添加类似内容,会有帮助吗? :
@Override
public void onDestroyView() {
super.onDestroyView();
swipeTimer.cancel();
swipeTimer = null;
}