Android在SurfaceView绘图

MainActivity

1 package com.example.surfacedemo; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 7 public class MainActivity extends Activity { 8 9@Override 10public void onCreate(Bundle savedInstanceState) { 11super.onCreate(savedInstanceState); 12setContentView(new MyView(this)); //设置显示MyView实例 13} 14 15@Override 16public boolean onCreateOptionsMenu(Menu menu) { 17getMenuInflater().inflate(R.menu.activity_main, menu); 18return true; 19} 20 }

MyView
1 package com.example.surfacedemo; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.view.SurfaceHolder; 8 import android.view.SurfaceHolder.Callback; 9 import android.view.SurfaceView; 10 11 public class MyView extends SurfaceView { 12 13SurfaceHolder sh; 14 15public MyView(Context context) { 16super(context); 17sh = getHolder(); //得到Holder 18sh.addCallback(new Callback() {//创建回调 19 20public void surfaceDestroyed(SurfaceHolder holder) {//界面销毁时执行 21 22} 23 24public void surfaceCreated(SurfaceHolder holder) {//界面创建后执行 25Canvas canvas = sh.lockCanvas(); 26Paint paint=new Paint(); 27paint.setColor(Color.BLUE); 28canvas.drawCircle(100,100, 20, paint); 29sh.unlockCanvasAndPost(canvas); 30} 31 32public void surfaceChanged(SurfaceHolder holder, int format, int width, 33int height) {//界面改变时执行 34 35} 36}); 37} 38 39 }

【Android在SurfaceView绘图】
转载于:https://www.cnblogs.com/hzy3774/archive/2012/07/13/2590940.html

    推荐阅读