从资源中获取位图然后将其加载到后台

发布于 2024-10-18 14:53:48 字数 1663 浏览 1 评论 0原文

大家好,我过去在这里得到了很多帮助,想知道你们是否可以帮助我解决一些问题。

我想从资源中加载位图,然后将其添加到我在代码中创建的线性布局图像视图的背景中。我知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2024-10-25 14:53:48

您必须首先在 xml 文件中创建一个线性布局。
然后在代码中访问该布局。

LinearLayout linearMain = (LinearLayout) findViewById(R.id.linearmainLayout);

然后你尝试下面的事情,在backgroundPainting中设置位图图像后在最后添加这两行

rel.addView(backgroundPainting, lp);

linearMain.addView(rel);

,或者你可以尝试这种方法

setContentView(rel); 

并在backgroundPainting中设置位图图像后在最后写一行

rel.addView(backgroundPainting, lp); 

You have to first create one linear layout in your xml file.
And then access that layout in your code.

LinearLayout linearMain = (LinearLayout) findViewById(R.id.linearmainLayout);

Then you try below thing by adding this two lines at the last after setting bitmap image in backgroundPainting

rel.addView(backgroundPainting, lp);

linearMain.addView(rel);

or you can try this way

setContentView(rel); 

and write one line at the last after setting bitmap image in backgroundPainting

rel.addView(backgroundPainting, lp); 
毁梦 2024-10-25 14:53:48

您必须将此视图相关添加到其父视图视图正在创建但尚未添加

You have to add this view rel to its parent view the view is being created but not added

蹲墙角沉默 2024-10-25 14:53:48

创建 LinearLayout 后只需使用 setContentView(rel);

Just use setContentView(rel); after creating your LinearLayout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文