Android 如何在特定的 LinearLayout 中添加 Canvas?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3333749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Canvas in a specific LinearLayout?
提问by Nandagopal T
I am new comer to android but not to java. I have been designing UI in android through XML file, in that page i have 3 linear Layouts, in my top layout(first LinearLayout) i have kept some image and in the last layout i have kept some buttons,now i need to place a circle(of red color) in my center layout using canvas,i have written a separate class that extends View where in onDraw(Canvas canvas) ,i have drawn a circle.
我是android的新手,但不是java的新手。我一直在通过 XML 文件在 android 中设计 UI,在那个页面我有 3 个线性布局,在我的顶部布局(第一个 LinearLayout)中我保留了一些图像,在最后一个布局中我保留了一些按钮,现在我需要放置一个圆形(红色)在我的中心布局中使用画布,我编写了一个单独的类来扩展 View ,在 onDraw(Canvas canvas) 中,我画了一个圆圈。
package com.project.TargetTrackr3; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class DrawCanvasCircle extends View{ public DrawCanvasCircle(Context mContext) { super(mContext); } public void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); canvas.drawColor(Color.WHITE); paint.setColor(Color.BLUE); canvas.drawCircle(20, 20, 15, paint); } }
Now i have to bring this canvas to the second layout,my main.xml is shown below
现在我必须把这个画布带到第二个布局,我的 main.xml 如下所示
package com.project.TargetTrackr3; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; public class TargetTrackr3Activity extends Activity { /** Called when the activity is first created. */ protected LinearLayout ll; DrawCanvasCircle c; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main1); //layouting file ll = (LinearLayout) findViewById(R.id.LinearLayout_DrawCircle);//This is where i have to bring the canvas c = new DrawCanvasCircle(this); ................................... ................................ } }
采纳答案by James Black
Here is what I did to include your view.
这是我为包含您的观点所做的工作。
Start with adding a new layout to your xml file, then you can retrieve that, and then you can add to it like this:
首先向您的 xml 文件添加一个新布局,然后您可以检索它,然后您可以像这样添加到它:
DrawCanvasCircle pcc = new DrawCanvasCircle (this);
Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
pcc.draw(canvas);
pcc.setLayoutParams(new LayoutParams(25, 25));
mControls.addView(pcc);
In this example mControls
is a layout that is added to the main activity layout.
在这个例子中mControls
是一个添加到主活动布局的布局。