package com.free.chart;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

public class DrawTextStudy extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder holder;

public DrawTextStudy(Context context) {

super(context);

// TODO Auto-generated constructor stub

holder = this.getHolder();

holder.addCallback(this);

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

// TODO Auto-generated method stub

new Thread(new MyThread()).start();

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

}

void drawText(Canvas canvas ,String text , float x ,float y,Paint paint ,float angle){

if(angle != 0){

canvas.rotate(angle, x, y);

}

canvas.drawText(text, x, y, paint);

if(angle != 0){

canvas.rotate(-angle, x, y);

}

}

class MyThread implements Runnable{

@Override

public void run() {

// TODO Auto-generated method stub

Canvas canvas = null;

try{

canvas = holder.lockCanvas();

Paint paint = new Paint();

paint.setColor(Color.WHITE);

paint.setTextSize(20);

canvas.drawLine(100, 100, 100, 400, paint);

drawText(canvas,"Hello", 80, 200, paint,-90);

paint.setColor(Color.RED);

paint.setTextSize(40);

drawText(canvas,"free", 150, 180, paint,-45);

paint.setColor(Color.BLUE);

drawText(canvas,"World", 150, 80, paint,0);

canvas.drawLine(100, 100, 400, 100, paint);

} catch(Exception e){

} finally {

holder.unlockCanvasAndPost(canvas);

}

}

}

}

activity调用

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new DrawTextStudy(this));

}

0818b9ca8b590ca3270a3433284dd417.png 转自: http://www.eoeandroid.com/thread-178896-1-1.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐