0

I use function loadBitmap to get bitmap with canvas, but the place of the bitmap inside is wrong,I have thought for a long time, however could not find the reason. points is set in dp by layout_marignLeft and layout_marinTop in layout.xml. Here is the code.

 public Bitmap loadBitmap() {

    Bitmap b = BitmapFactory.decodeResource(mContext.getResources(),bg)
            .copy(Bitmap.Config.ARGB_8888, true);

    if (null == ids ||null ==  points || ids.size() != points.size())
        return b;

    Canvas c = new Canvas(b);

    for (int i = 0; i < ids.size(); i++) {
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), ids.get(i));
        c.drawBitmap(bitmap, dpToPx(points.get(i).x), dpToPx(points.get(i).y), null);
        bitmap.recycle();
    }

    c.save(Canvas.ALL_SAVE_FLAG);
    c.restore();
    return b;
}

public float dpToPx(float x) {
    return mContext.getResources().getDisplayMetrics().density * x;

}
方人也
  • 151
  • 3
  • 10

2 Answers2

0

is the image that you are using having padding in it. If image has some extra alpha boundary then your calculations can go wrong.

peeyush pathak
  • 3,663
  • 3
  • 19
  • 25
  • Integer is OK. The problem here is the bitmap is not at the right place. – 方人也 Jan 30 '16 at 11:37
  • so the x and y is calculated from top left of the canvas – peeyush pathak Jan 30 '16 at 11:52
  • x and y is the FrameLayout, FrameLayout includes some IamgeViews, x is the layout_marginLeft, y is the layout_marginTop. I just want to combine the bitmaps in the ImageViews to a big bitmap. – 方人也 Jan 30 '16 at 12:11
  • in ImageView why don't you directly set the image – peeyush pathak Jan 30 '16 at 12:21
  • My app is fullscreen, no padding, the x and y are the same as imgeviews' margin, The View of the layout is good, but the bitmap I draw seems wrong. What's more, the bitmap seems to be smaller than real. – 方人也 Jan 30 '16 at 16:44
  • I find the reason, I set ImageView width and height with "wrap_content".Size of ImageView is bigger than size of image. When I drawBitmap using canvas the place is always wrong. Now I need to set the ImageView size the same as image inside. Thank you~ – 方人也 Jan 30 '16 at 17:40
0

The problem has nothing to do with View and Canvas,I want to the app to fullscreen, but the tablet has navigation bar, which cause the layout not fill the screen, and the params of margin_left and margin_right are totally wrong.When I hide the navigation bar,everything is ok.Here is the method to hide navigation bar.Android tablet navigation bar won't hide

Community
  • 1
  • 1
方人也
  • 151
  • 3
  • 10