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.
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
package com.project.TargetTrackr3; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import an开发者_StackOverflow社区droid.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); ................................... ................................ } }
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:
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.
加载中,请稍侯......
精彩评论