Draw multiple bitmap on a view in android

廉价感情. 提交于 2019-12-12 19:41:28

问题


I am trying to make a view in which i am having 6-7 different images. There is no issue in drwaing only one image on a view, but how to draw multiple images on that view.

Here is my code-

 Bitmap img1= BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.football);

And here is my onDraw() method-

@Override
    protected void onDraw(Canvas canvas) {
        setBackgroundColor(Color.GREEN);

        float hgt=canvas.getHeight();
        float wth=canvas.getWidth();
        System.out.println("hgt and wt is "+hgt+" "+wth);

        if (startMovement) {

            left = 0;
            right = 0;
            startMovement = false;
        }
        CheckCorner(canvas);
        super.onDraw(canvas);
    }

The images are moving from top to bottom, and need to be displayed in a row.


回答1:


I don't know if you can add more that one bitmap to a View. What you probably want is to add 6-7 ImageViews to a Layout/GroupView:

ImageView iv = new ImageView(this);
iv.setBackgroundResource(R.drawable.myImage1);
ImageView iv2 = new ImageView(this);
iv2.setBackgroundResource(R.drawable.myImage2);
.
.
.
LinearLayout ll = new LinearLayout(this);
ll.addView(iv);
ll.addView(iv2);

Also, you can make the linearlayout to layout the views horizontally




回答2:


I would just use a RelativeLayout or a LinearLayout.

RelativeLayout gives you more flexibility on where to place your views, but requires a little bit more code for the layout parameters (where you want to place the view). Check this answer on RelativeLayouts

If you are going to just put the views in a line (horizontal or vertical), use a LinearLayout instead. Check this answer on LinearLayouts.



来源:https://stackoverflow.com/questions/18215766/draw-multiple-bitmap-on-a-view-in-android

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