码迷,mamicode.com
首页 > 其他好文 > 详细

使用SurfaceView

时间:2015-08-21 09:25:33      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

一.新建一个工程“LearnSurfaceView”

二.新建一个类“MySurfaceView”

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private Paint paint=null;
    public MySurfaceView(Context context) {
        super(context);
        paint = new Paint();
        paint.setColor(Color.RED);
        getHolder().addCallback(this);
    }
    public void draw(){
        Canvas canvas = getHolder().lockCanvas();
        canvas.drawColor(Color.WHITE);
        canvas.drawLine(0,0,getWidth()/2,getHeight()/2,paint);
        canvas.drawCircle(getWidth()/2,getHeight()/2,50,paint);
        canvas.drawRect(0,0,100,100,paint);
        getHolder().unlockCanvasAndPost(canvas);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        draw();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

三.修改MainActivity.java文件中的代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MySurfaceView(this));
    }

 

使用SurfaceView

标签:

原文地址:http://www.cnblogs.com/happygirl-zjj/p/4746780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!