首页 > 代码库 > 【Android实战】SurfaceView绘制各种效果Demo
【Android实战】SurfaceView绘制各种效果Demo
1、狂奔的炸弹
public class MySurface extends SurfaceView implements Callback, Runnable { SurfaceHolder holder; boolean goOn = true; int w; int h; Bitmap bitmap; int x; int y; int bx; int by; boolean down = true; boolean right = true; int p = 30; public MySurface(Context context, int wi, int hi) { super(context); // TODO Auto-generated constructor stub holder = getHolder(); holder.addCallback(this); setFocusable(true); setFocusableInTouchMode(true); w = wi; h = hi; Bitmap obitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b1); bitmap = Bitmap.createScaledBitmap(obitmap, 260, 200, false); bx = bitmap.getWidth(); by = bitmap.getHeight(); Thread t = new Thread(this); } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub Thread t = new Thread(this); t.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub goOn = false; } @Override public void run() { // TODO Auto-generated method stub while (goOn) { myDraw(); try { if (y < h - by && down) { y += p; } else { down = false; if (x < w - bx && right) { x += p; } else { right = false; if (y > 0 && !down) { y -= p; } else { if (x > 0 && !right) { x -= p; } else { down = true; right = true; } } } } Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Canvas canvas; private void myDraw() { // TODO Auto-generated method stub canvas = holder.lockCanvas(); if (canvas != null) { Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); canvas.drawPaint(paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC)); canvas.drawColor(Color.GRAY); try { if (canvas != null) { canvas.drawBitmap(bitmap, x, y, null); } } finally { if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } } } }
【Android实战】SurfaceView绘制各种效果Demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。