我是android的新手,但不是java的新手。 我一直在通过XML文件在android中设计UI,在该页面中,我有3个线性布局,在我的顶部布局(第一个LinearLayout)中,我保留了一些图像,在最后一个布局中,我保留了一些按钮,现在我需要放置一个 我在中心布局中使用canvas绘制了一个圆形(红色),我编写了一个单独的类,该类扩展了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);
}
}
现在我必须将此画布带到第二个布局,我的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是添加到主活动布局中的布局。
嗨,请问您如何将其放在mControls的中央?