【发布时间】:2011-02-21 08:44:30
【问题描述】:
大家好,过去我在这里得到了很多帮助,想知道你们是否可以帮我解决一些问题。
我想从资产中加载位图,然后将其添加到我在代码中创建的线性布局图像视图的背景中。我知道如何使用 xml 布局文件执行此操作,但在这种情况下,我需要在代码中执行此操作。到目前为止,我有这个,它适用于 .xml 布局,但是当我尝试在代码中创建线性布局和 ImageView 时,图像没有显示出来。有什么我忽略或做错了。 这是我得到的代码
ImageView backgroundPainting = new ImageView(this);
backgroundPainting.setAdjustViewBounds(true);
LinearLayout rel = new LinearLayout(this);
// RelativeLayout backgroundPaintingRL = (RelativeLayout) findViewById(R.id.RelativeLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
//lp.addRule(RelativeLayout.BELOW, R.id.ButtonRecalculate);
//lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rel.addView(backgroundPainting, lp);
// backgroundPainting = (ImageView) findViewById(R.id.backgroundPainting2);
getArtist = levelSelect.getArtistNameSelected();
getLevel = level.getLevelSelected() ;
String imagePath = "artists-images/"+getArtist + getLevel+".jpg";
try { // Get reference to AssetManager
AssetManager mngr = getAssets();
// Create an input stream to read from the asset folder
InputStream ins = mngr.open(imagePath);
// Convert the input stream into a bitmap
levelBitmap = BitmapFactory.decodeStream(ins);
backgroundPainting.setImageBitmap(levelBitmap);
} catch (final IOException e) {
e.printStackTrace();
Toast.makeText(levelView.this, "couldn't set image to background", Toast.LENGTH_LONG).show();
}
请帮助谢谢,
企鹅
【问题讨论】:
-
你的 toast 会在跑步时出现吗?
-
如果没有,你打电话给
setContentView(rel)吗? -
是的,我的吐司很好。我有一个自定义 GamePanel,我将其设置为内容视图,但它是一个不同的类。
-
请发布异常,然后您会在 LogCat 中找到它。
标签: android background bitmap imageview