1.先上解析,实现循环的原理:
2.自定义surfaceview,并addcallback(this)
在重写的方法中,操作
@Override public void surfaceCreated(SurfaceHolder holder) { start(); }
private void start() { updatethread = new UpdateViewThread(); updatethread.start(); }
public void run() { while (true) { if ((back.getWidth() - startx) <= 0){ startx = 0; continue; } Canvas canvas = surfaceHolder.lockCanvas(); Log.i("mydate", " " + back.getWidth()); Matrix matrix1 = new Matrix(); matrix1.setScale(1f, (float) MainActivity.windowheight / (float) back.getHeight()); if (back.getWidth() - startx < MainActivity.windowwidth) { Bitmap b = Bitmap.createBitmap(back, startx, 0, back.getWidth() - startx, back.getHeight(), matrix1, false); Bitmap b2 = Bitmap.createBitmap(back, 0, 0, width - (back.getWidth() - startx), back.getHeight(), matrix1, false); canvas.drawBitmap(b, 0, 0, null); canvas.drawBitmap(b2, 0 + back.getWidth() - startx, 0, null); } else { Bitmap bitmap = Bitmap.createBitmap(back, startx, starty, width, height, matrix, false); canvas.drawBitmap(bitmap, 0, 0, null); } surfaceHolder.unlockCanvasAndPost(canvas); startx += 20; try { Thread.sleep(60); } catch (Exception e) { e.printStackTrace(); } }