从资源中获取位图然后将其加载到后台
大家好,我过去在这里得到了很多帮助,想知道你们是否可以帮助我解决一些问题。
我想从资源中加载位图,然后将其添加到我在代码中创建的线性布局图像视图的背景中。我知道如何使用 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();
}
请帮忙,谢谢,
Pengume
Hey Everyone I have had a lot of help here in the past and was wondering if you guys could help me out with something.
I would like to load a bitmap from the assets and then add it to the background of a linear layout image view that i created in code. I know how to do this with an xml layout file but in this case i need to do it in code. I have this so far and it works with an .xml layout but when I try to create the linear layout and ImageView in code the image is not showing up. Is there something i am overlooking or doing wrong.
here is the code I got
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();
}
Please Help thnx,
Pengume
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须首先在 xml 文件中创建一个线性布局。
然后在代码中访问该布局。
然后你尝试下面的事情,在backgroundPainting中设置位图图像后在最后添加这两行
,或者你可以尝试这种方法
并在backgroundPainting中设置位图图像后在最后写一行
You have to first create one linear layout in your xml file.
And then access that layout in your code.
Then you try below thing by adding this two lines at the last after setting bitmap image in backgroundPainting
or you can try this way
and write one line at the last after setting bitmap image in backgroundPainting
您必须将此视图相关添加到其父视图视图正在创建但尚未添加
You have to add this view rel to its parent view the view is being created but not added
创建
LinearLayout
后只需使用setContentView(rel);
Just use
setContentView(rel);
after creating yourLinearLayout