早上好,我试图在我的Android应用程序中滚动一个大图像(800 * 3782,1.7 Mb png文件)我使用了这个文档https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
但每次我都有同样的错误:
08-28 11:00:36.534: E/AndroidRuntime(2143): Caused by: java.lang.OutOfMemoryError
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.content.res.Resources.loadDrawable(Resources.java:1965)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.widget.ImageView.(ImageView.java:120)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.widget.ImageView.(ImageView.java:110)
08-28 11:00:36.534: E/AndroidRuntime(2143): ... 28 more
这个洞代码:
about.java
public class About extends Activity {
ImageView mImageView; //reference to the ImageView
int xDim, yDim; //stores ImageView dimensions
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
//attach an instance of HandleClick to the Button
//reference the ImageView
mImageView=(ImageView)findViewById(R.id.imageView1);
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.aboutscroll, 800, 1200));
}
@Override
//Get the size of the Image view after the
//Activity has completely loaded
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
xDim=mImageView.getWidth();
yDim=mImageView.getHeight();
}
//Load a bitmap from a resource with a target size
static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
//Given the bitmap size and View size calculate a subsampling size (powers of 2)
static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
int inSampleSize = 1; //Default subsampling size
// See if image raw height and width is bigger than that of required view
if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
//bigger
final int halfHeight = options.outHeight / 2;
final int halfWidth = options.outWidth / 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;
}
}
这是xml文件:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
只有当我将图像缩放到100 * 100像素时,才有效。女巫不是我的目标我想滚动所有图像而不缩放我在nexus 7模拟器中测试应用程序
我希望有人帮助我,想你