Android 图形系统一: 绘制长方形
2010-06-02 00:55:00 来源:WEB开发网再关注一下android的图形系统,android去除了linux上的经典图形系统X,使用的是自己的图形叠加方式,先看一个简单图形绘制代码吧,用surfaceview来绘制长方形,当然也可以绘制其他图形。
package com.android.sufaceTest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class surfaceTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends SurfaceView implements SurfaceHolder.Callback{
SurfaceHolder holder;
public MyView(Context context) {
super(context);
holder = this.getHolder();//获取holder
holder.addCallback(this);
//setFocusable(true);
}
//@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
// @Override
public void surfaceCreated(SurfaceHolder holder) {
new Thread(new MyThread()).start();
}
// @Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
//内部类的内部类
class MyThread implements Runnable{
//@Override
public void run() {
Canvas canvas = holder.lockCanvas(null);//获取画布
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);
static long i = 0;
i++;
canvas.drawRect(new RectF(i%40,60,80,80), mPaint);
canvas.
holder.unlockCanvasAndPost(canvas);//解锁画布,提交画好的图像
}
}
}
}
更多精彩
赞助商链接