关闭

android 画图 surfaceView

标签: androidlistview布局xml
250人阅读 评论(0) 收藏 举报
分类:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MySurface surface=new MySurface(MainActivity.this);
setContentView(surface);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class MySurface extends SurfaceView implements Callback {
private SurfaceHolder holder;
private MyThread myThread;
public MySurface(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
holder.addCallback(this);
paint=new Paint();
}
// SurfaceView发生变化的时候调用
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}

// SurfaceView 创建的时候调用
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
isRunning=true;
new MyThread().start();
}
// SurfaceView 销毁的时候调用
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
isRunning=false;
}

public boolean isRunning=false;
private Paint paint;
class MyThread extends Thread{
private int x,y;
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(isRunning){
//更新界面 50毫秒
draw();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private void draw() {
// TODO Auto-generated method stub
//锁定界面,拿到画板
// paint.setColor(0xFFFFFFFF);
// paint.setTextSize(25);
Canvas canvas = holder.lockCanvas();
canvas.drawColor(0xFFFF0000);
// canvas.drawText("SurfaceView", x, y, paint);
//decodeResource 参数:1——资源管理器 2——图片ID
Bitmap  bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//显示画板内容
canvas.drawBitmap(bitmap, x, y, paint);
if(canvas!=null)
holder.unlockCanvasAndPost(canvas);
x++;
y++;
}
}
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27289次
    • 积分:668
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条