如何在特定的LinearLayout中添加Canvas?
我是Android的新角色,但不是Java。 我一直在通过XML文件在android中设计UI,在那个页面中我有3个线性布局,在我的顶层布局(第一个LinearLayout)中,我保留了一些图像,并且在最后一个布局中我保留了一些按钮,现在我需要放置一个在我的中心布局使用画布圈(红色),我已经写了一个单独的类,扩展视图在onDraw(画布画布),我画了一个圆圈。
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); } }
现在我必须将此画布带到第二个布局,我的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); ................................... ................................ } }
这是我所做的包括你的观点。
首先向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);
在这个例子中, mControls
是添加到主活动布局的布局。