iPhone中自绘实现步骤
1. 继承@interface MyView : UIView {
2. 实现- (void)drawRect:(CGRect)rect
3. 调用addSubView把新生成的view加入进来显示
addSubView[window addSubview:viewController.view];
4.示例代码
- (void)drawRect:(CGRect)rect {
// create the bitmap context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil,100,100,8,400, colorSpace,kCGImageAlphaPremultipliedLast);
CFRelease(colorSpace);
// create an arrow image
// set the fill color
CGColorRef fillColor = [[UIColor blackColor] CGColor];
CGContextSetFillColor(context, CGColorGetComponents(fillColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 24.0f, 4.0f);
CGContextAddLineToPoint(context, 24.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
CGContextSelectFont ( context, "Arial", 10.f, kCGEncodingMacRoman );
CGContextSetRGBFillColor ( context, 0.0f, 0.0f, 0.f, 1.f );
CGContextSetShouldAntialias ( context, 0 );
CGContextShowText(context, "hh", 2);
// convert the context into a CGImageRef
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage* image2 = [UIImage imageWithCGImage:image];
[image2 drawInRect:CGRectMake(0, 0, 120, 160)];
NSString* myStr = @"中文";
UIFont* font = [UIFont systemFontOfSize:12.0];
[myStr drawInRect: CGRectMake(160, 240, 100, 130) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
}
Android自绘实现步骤
1. 继承public class MyView extends View
2. 实现protected void onDraw(Canvas canvas){
3. 把生成的MyView加入到layout中
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.LinearLayout01);
MyView myView=new MyView(this);
rootLayout.addView(myView, 240, 320);
4. onDraw实现示例(注:代码是我从网上copy的)
protected void onDraw(Canvas canvas){
super.onDraw(canvas);//重写onDraw方法
int data[]={265481,335842,125466,256856,352151,254611,356584};
int data1[]={324225,144578,352415,458125,268451,351545,252145};
canvas.drawColor(Color.WHITE);//设置背景颜色
Paint paint= new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);//去锯齿
paint.setColor(Color.BLUE);//颜色
Paint paint1=new Paint();
paint1.setStyle(Paint.Style.STROKE);
paint1.setAntiAlias(true);//去锯齿
paint1.setColor(Color.DKGRAY);
//数据 线
int n=1,m=0,x=0;
int nn=1,mm=0,xx=0;
int[] y=new int[100];
y[0]=120;
for(int x1=0;x1<data.length;x1++){
y[n]=120-data[m]*20/100000;
x=x+30;
int x2=x+30;
canvas.drawLine(x, y[x1], x2, y[n], paint);
canvas.drawCircle(x2, y[n], 2, paint);
n=n+1;
m=m+1;
}
for(int x1=0;x1<data1.length;x1++){
y[nn]=120-data1[mm]*20/100000;
xx=xx+30;
int x2=xx+30;
canvas.drawLine(xx, y[x1], x2, y[nn], paint1);
canvas.drawCircle(x2, y[nn], 2, paint1);
nn=nn+1;
mm=mm+1;
}
//设置Y轴
canvas.drawLine(30, 10, 30, 120, paint);
canvas.drawLine(30, 20, 35, 20, paint);
canvas.drawLine(30, 40, 35, 40, paint);
canvas.drawLine(30, 60, 35, 60, paint);
canvas.drawLine(30, 80, 35, 80, paint);
canvas.drawLine(30, 100, 35, 100, paint);
//设置X轴
canvas.drawLine(30,120,300,120,paint);
canvas.drawLine(60, 120, 60, 115, paint);
canvas.drawLine(90, 120, 90, 115, paint);
canvas.drawLine(120, 120, 120, 115, paint);
canvas.drawLine(150, 120, 150, 115, paint);
canvas.drawLine(180, 120, 180, 115, paint);
canvas.drawLine(210, 120, 210, 115, paint);
canvas.drawLine(240, 120, 240, 115, paint);
canvas.drawLine(270, 120, 270, 115, paint);
//设置Y轴文字
paint.setTextSize(7);
canvas.drawText(getResources().getString(R.string.str_text1), 5, 100, paint);
}