简体   繁体   English

将画布添加到相对布局

[英]Adding a canvas to a relative layout

Currently I am writing a program that is supposed to add an image to a canvas and then add that canvas to a relative layout. 目前我正在编写一个程序,该程序应该将图像添加到画布,然后将该画布添加到相对布局。 The problem that I am having is that when I call this the layout is displayed but the canvas is not drawn on the layout. 我遇到的问题是,当我调用它时,将显示布局,但画布上没有绘制画布。

canvas = (RelativeLayout) findViewById(R.id.canvasLayout);                  
imageCanvas = new Canvas();
mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);

imageCanvas.drawBitmap(mainImage, 50, 50, null);

imageCanvas.drawColor(Color.BLACK);
canvas.draw(imageCanvas);

That is the code that is attempting to add the image to the canvas and then the canvas to the layout. 这是试图将图像添加到画布然后将画布添加到布局的代码。

I am completely lost as to what to do to attempt to fix this. 我完全迷失了试图解决这个问题的方法。

The canvas.draw(imageCanvas) call you are using actually draws canvas (the RelativeLayout) onto imageCanvas (the Canvas object) - likely not what you were intending. 您正在使用的canvas.draw(imageCanvas)调用实际上将canvas(RelativeLayout)绘制到imageCanvas(Canvas对象)上 - 可能不是您想要的。 There are several ways I would suggest to go about getting the drawable onto the RelativeLayout: 有几种方法我建议将drawable放到RelativeLayout上:

The first would be to create an ImageView, set it's image to your drawable, then add it to the layout. 第一个是创建一个ImageView,将它的图像设置为drawable,然后将其添加到布局中。 Depending on if you want the images to be added dynamically during runtime or not, you could actually do this in the xml graphic designer. 根据您是否希望在运行时动态添加图像,您可以在xml图形设计器中实际执行此操作。 This is probably the best way to go, as it gives you control over how large the image is, and how it gets scaled/cropped. 这可能是最好的方法,因为它可以控制图像的大小,以及图像的缩放/裁剪方式。

If you want to set a background to the layout, I believe you can actually set the Background attribute of the RelativeLayout directly to a drawable - this does not allow as much control over the scaling of the image though. 如果你想为布局设置背景,我相信你实际上可以将RelativeLayout的Background属性直接设置为drawable - 虽然这不能控制图像的缩放。

Finally, for complete control over how the RelativeLayout is draw, you can override the OnDraw method in any view. 最后,为了完全控制RelativeLayout的绘制方式,您可以在任何视图中覆盖OnDraw方法。 This method is called by the android system whenever it needs to refresh the view. 只要需要刷新视图,android系统就会调用此方法。 If you do end up using this, creating a custom view class, which you then place in the RelativeLayout will probably work better. 如果你最终使用它,创建一个自定义视图类,然后放在 RelativeLayout中可能会更好。

canvas = (RelativeLayout) findViewById(R.id.canvasLayout);                  
imageCanvas = new Canvas();

mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);

imageCanvas.drawBitmap(mainImage, 50, 50, null);



canvas.addView(imageCanvas);

The following will programmatically add an image to your layout from a bitmap resource which sounds like what you want to do. 以下将以编程方式从位图资源向您的布局添加图像,这听起来像您想要做的。 If you need to draw on top of that image, say, you would need to make a custom component by extending ImageView and overriding onDraw(). 如果您需要在该图像上绘制,比如说,您需要通过扩展ImageView并覆盖onDraw()来创建自定义组件。

canvas = (RelativeLayout) findViewById(R.id.canvasLayout);                  
imageView = new ImageView(getApplicationContext());
mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);
imageView.setImageBitmap( mainImage );
canvas.addView( imageView );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM