我在活动中滑动图片时遇到问题。当我滑动 5-10 次时,我得到一个内存不足错误。这没什么了不起的。我的问题与使用BitmapFactory
和计算图像大小有关,如本页所示:https://android.jlelse.eu/loading-large-bitmaps-efficiently-in-android-66826cd4ad53。当我使用SlidingAdapter/PagerAdapter并且我的图像已编号时,我不知道它是如何工作的。
这是我的代码:
public class SliderAdapter extends PagerAdapter {
static Resources res = null;
Context context;
LayoutInflater layoutInflater;
public SliderAdapter(Context context)
{
this.context = context;
slide_headings = context.getResources().getStringArray(R.array.p06_naglowek);
slide_descs = context.getResources().getStringArray(R.array.p06_opis);
}
public int [] slide_images = {
R.drawable.komputer_startowe,
R.drawable.ecu_version,
R.drawable.potrzebne_elementy,
R.drawable.co_gdzie,
R.drawable.rm11,
R.drawable.p06_finish
};
String[] slide_headings;
String[] slide_descs;
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
@Override
public int getCount() {
return slide_headings.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == (RelativeLayout) object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout, container, false);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = calculateInSampleSize(options, 500,500);
Bitmap startImage = BitmapFactory.decodeResource(context.getResources(),R.drawable.komputer_startowe,options);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_descs[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((RelativeLayout)object);
}
}
好的,我有解决方案。也许它对某人有用。
以下是活动:
public class SliderAdapter extends PagerAdapter {
static Resources res = null;
Context context;
LayoutInflater layoutInflater;
static Bitmap bitmap;
public SliderAdapter(Context context)
{
this.context = context;
slide_headings = context.getResources().getStringArray(R.array.p06_naglowek);
slide_descs = context.getResources().getStringArray(R.array.p06_opis);
}
public int [] slide_images = {
R.drawable.komputer_startowe,
R.drawable.ecu_version,
R.drawable.potrzebne_elementy,
R.drawable.co_gdzie,
R.drawable.rm11,
R.drawable.p06_finish
};
String[] slide_headings;
String[] slide_descs;
@Override
public int getCount() {
return slide_headings.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == (RelativeLayout) object;
}
@NonNull
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 1;
// o.inDither = false;
o.inScaled = true;
o.inDensity = srcWidth;
o.inTargetDensity = dstWidth*o.inSampleSize;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), slide_images[position], o);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_descs[position]);
slideImageView.setImageBitmap(bitmap);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((RelativeLayout)object);
}
}