我想在 android 中实现它
为此,我必须从资产文件夹加载图像,我HorizontalScrollView
在 xml 文件中制作两个并ImageView
在其中动态加载。为了加载 ImageView 我正在使用此代码
LinearLayout myGallery = (LinearLayout) findViewById(R.id.mygallery);
try {
String galleryDirectoryName = "gallery";
String[] listImages = getAssets().list(galleryDirectoryName);
for (String imageName : listImages) {
InputStream is = getAssets().open(galleryDirectoryName + "/" + imageName);
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bitmap);
LinearLayout.LayoutParams myGallery1= new LinearLayout.LayoutParams(100, 100);
myGallery1.setMargins(20, 0, 10, 0);
//its is also working
// imageView.setLayoutParams(myGallery1);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// diplayImage.setImageBitmap(bitmap);
}
});
Log.e("GalleryWithHorizontalScrollView", e.getMessage(), e);
}
//重复上面的代码在第二个horizontalScrollView中加载imageView
LinearLayout myGallery2 = (LinearLayout) findViewById(R.id.mygallery2);
try {
String galleryDirectoryName1 = "gallery2";
String[] listImages2 = getAssets().list(galleryDirectoryName1);
for (String imageName : listImages2) {
InputStream is1 = getAssets().open(galleryDirectoryName1 + "/" + imageName);
Bitmap bitmap1 = BitmapFactory.decodeStream(is1);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bitmap1);
LinearLayout.LayoutParams myGallery21= new LinearLayout.LayoutParams(100, 100);
myGallery21.setMargins(20, 40, 10, 0);
//its is also working
myGallery.addView(imageView,myGallery1);
}
} catch (IOException e) {
myGallery2.addView(imageView,myGallery21);
}
} catch (IOException e) {
Log.e("GalleryWithHorizontalScrollView", e.getMessage(), e);
}
}
如果我制作一个horizontalScrollView 并在其中加载图像视图,那么它可以正常工作,但是对于第二个horizontalScrollView,它会在第76行给我错误
Bitmap bitmap1 = BitmapFactory.decodeStream(is1);
我的日志猫就是这个
05-14 11:13:26.000: E/AndroidRuntime(8350): FATAL EXCEPTION: main
05-14 11:13:26.000: E/AndroidRuntime(8350): java.lang.OutOfMemoryError
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:643)
05-14 11:13:26.000: E/AndroidRuntime(8350): at com.example.gallery.MainActivity.onCreate(MainActivity.java:76)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.Activity.performCreate(Activity.java:4470)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.os.Looper.loop(Looper.java:137)
05-14 11:13:26.000: E/AndroidRuntime(8350): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-14 11:13:26.000: E/AndroidRuntime(8350): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 11:13:26.000: E/AndroidRuntime(8350): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 11:13:26.000: E/AndroidRuntime(8350): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-14 11:13:26.000: E/AndroidRuntime(8350): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-14 11:13:26.000: E/AndroidRuntime(8350): at dalvik.system.NativeStart.main(Native Method)