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);
}

更多相关文章

  1. 浅析Android中的visibility属性
  2. android开发EditText输入时弹出数字输入键盘
  3. Android(安卓)CTS
  4. Android(安卓)琐碎知识点汇总
  5. Android有哪些权限?Android开发权限集合
  6. Android_ActionBar
  7. Android(安卓)点击Notification事件处理
  8. android之自定义组合控件
  9. Android学习笔记(24):进度条组件ProgressBar及其子类

随机推荐

  1. Android 零散知识点整理
  2. 【Android 开发】:数据存储之 SQLite 数据
  3. Windows上搭建Android开发环境详细教程
  4. 深入浅出 - Android系统移植与平台开发(十
  5. Android 开发者 for Flutter (1)Flutter和A
  6. 如何让TextView中的文字居中显示
  7. Android(安卓)Jetpack应用指南学习笔记6
  8. [置顶] Android NDK开发轻松入门
  9. Android 消息处理机制(Looper、Handler、M
  10. Android 应用设计--- 无边界设计理念