Add a cubic bezier from the last point, approaching control points (x1,y1) and (x2,y2), and ending at (x3,y3). If no moveTo() call has been made for this contour, the first point is automatically set to (0,0).
Create a new class MyView extends View. Override the onDraw(Canvas canvas) method to draw a path with cubicTo().
package com.AndroidMyCanvas;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Paint paint;
Path path;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
path = new Path();
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
path.moveTo(50, 50);
path.cubicTo(300, 50, 100, 400, 400, 400);
canvas.drawPath(path, paint);
path.reset();
paint.setColor(Color.GRAY);
paint.setStrokeWidth(1);
path.moveTo(50, 50);
path.lineTo(300, 50);
path.lineTo(100, 400);
path.lineTo(400, 400);
canvas.drawPath(path, paint);
}
}
Modify main Java activity to setContentView() using our custom View, MyView.
package com.AndroidMyCanvas;
import android.app.Activity;
import android.os.Bundle;
public class AndroidMyCanvasActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
}
No comments:
Post a Comment