我要做的是放置多个图像而不会出现内存不足问题,因此我使用方法 PutImage() 并尝试将图像放入 ImageView 但来自 Activity 而不是 XML 文件
这是我的 XML 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true" android:alwaysDrawnWithCache="false">
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
这是活动
public class RecipeBanana extends Activity {
ImageView v;
Button button;
Context localcontext = null;
public static Drawable getAssetImage(Context context, String filename) throws IOException {
AssetManager assets = context.getResources().getAssets();
InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".jpg")));
Bitmap bitmap = BitmapFactory.decodeStream(buffer);
return new BitmapDrawable(bitmap);
}
public void PutImage(ImageView v, String x){
try {
v.setImageDrawable(Drawable.createFromStream(localcontext.getAssets().open("flags/" + x + ".jpg"), null));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_banana);
ImageView v=(ImageView) findViewById(R.id.imageView1);
PutImage(v,"bananarecipe");
addListenerOnButton();
}