Android之 SurfaceView画图  

研究完<View画图>,开始研究SurfaceView画图,开工kiss
程序效果:用线程画一个方形

package org.c30;

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 Test extends Activity {
    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.RED);
				canvas.drawRect(new RectF(40,60,80,80), mPaint);
				holder.unlockCanvasAndPost(canvas);//解锁画布,提交画好的图像
			}
		}
    }
}

欢迎大佬支持本博客的发展 -- Donate --

本文链接:Android之 SurfaceView画图

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:三十岁,谢谢!^^


分享到:          
  1. 没有评论

  1. 没有通告