Draw Bitmap When Still Using XML View

微笑、不失礼 提交于 2019-12-25 10:16:15

问题


So I am using my main.xml as a view in my program but I still need to be able to add bimaps/drawables to the screen through programming. Is there any way to do that?

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 }

That is kinda what my onCreate looks like (but with a lot of code for the purpose of the app).

Is there anyway to maybe add a View or Canvas ONTOP of my current view?

I am new to bitmaps and drawables for android so sorry if this is commonly known, but I can't find a decent answer anywhere :P

Thanks, Brandon


回答1:


Create a Id to a framelayout in the xml (or another layout where you want to put the view) Maybe you can have your onCreate() something like this:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   //Init the framelayout from xml by id
   FrameLayout myFrameLayout = (FrameLayout) findViewById(R.id.myFrameLayout);

   //Create a new ImageView
   img_icon = new ImageView(this);
   img_icon.setImageResource(R.drawable.icon);
   img_icon.setAdjustViewBounds(true);
   img_icon.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

   //Add the newly created ImageView
   myFrameLayout.addView(img_icon);

}


来源:https://stackoverflow.com/questions/5208140/draw-bitmap-when-still-using-xml-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!