在SurfaceView中自由書寫和擦除,surfaceview書寫
/////////繼承SurfaceView 的類public class PaintView extends SurfaceView implements Runnable,SurfaceHolder.Callback { private float mX; private float mY; private Paint mPaint = null; private Path mPath = null; // 線程結束標誌位 boolean mLoop = true; SurfaceHolder mSurfaceHolder = null; Canvas mCanvas; public PaintView(Context context, AttributeSet arr) { super(context , arr); mSurfaceHolder = this.getHolder();//擷取holder mSurfaceHolder.addCallback(this); mSurfaceHolder.setFormat(PixelFormat.OPAQUE);//不透明 //mSurfaceHolder.setFormat(PixelFormat.RGB_565); //mSurfaceHolder.setFormat(PixelFormat.RGBA_8888); this.setFocusable(true);// setZOrderOnTop(true);//放到最頂層 mPaint = new Paint(); mPath = new Path(); mPaint.setAntiAlias(true); mPaint.setStyle(Style.STROKE); mPaint.setStrokeWidth(5); mPaint.setColor(mPaintColor); mPaint.setStrokeCap(Paint.Cap.ROUND); //圓頭 mPaint.setDither(true);//消除拉動,使畫面圓滑 mPaint.setStrokeJoin(Paint.Join.ROUND); //結合方式,平滑} @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub new Thread(this).start();//啟動線程 } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub mLoop = false; //結束線程 } @Override public void run() { while (mLoop==true) { Draw(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } // 繪圖 private void Draw() { try{ mCanvas = mSurfaceHolder.lockCanvas(); mCanvas.drawPath(mPath, mPaint); //// }catch (Exception e){ }finally { if (mCanvas !=null) { mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touchDown(event); break; case MotionEvent.ACTION_MOVE: touchMove(event); } //更新繪製 invalidate(); return true; } //手指點下螢幕時調用 private void touchDown(MotionEvent event) { //隱藏之前的繪製 // mPath.reset(); // float x = event.getX(); float y = event.getY(); mX = x; mY = y; //mPath繪製的繪製起點 mPath.moveTo(x, y); } //手指在螢幕上滑動時調用 private void touchMove(MotionEvent event) { final float x = event.getX(); final float y = event.getY(); final float previousX = mX; final float previousY = mY; final float dx = Math.abs(x - previousX); final float dy = Math.abs(y - previousY); //兩點之間的距離大於等於3時,產生貝塞爾繪製曲線 if (dx >= 3 || dy >= 3) { //設定貝茲路徑的操作點為起點和終點的一半 float cX = (x + previousX) / 2; float cY = (y + previousY) / 2; //二次貝塞爾,實現平滑曲線;previousX, previousY為操作點,cX, cY為終點 mPath.quadTo(previousX, previousY, cX, cY); //第二次執行時,第一次結束調用的座標值將作為第二次調用的初始座標值 mX = x; mY = y; } }////橡皮擦 private void setEraser(){ mPaint = new Paint(); mPath = new Path(); mPaint.setAntiAlias(true); mPaint.setStyle(Style.STROKE); mPaint.setStrokeWidth(50); // mPaint.setColor(mPaintColor); mPaint.setStrokeCap(Paint.Cap.ROUND); //圓頭 mPaint.setDither(true);//消除拉動,使畫面圓滑 mPaint.setStrokeJoin(Paint.Join.ROUND); //結合方式,平滑 mPaint.setAlpha(0); //// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); }}////////在Activity中顯示以上視圖setContentView(new PaintView(this));
急在surfaceview中用opengl寫3D的方法與例子
奇怪了,我用nehe的android例子跑,總是有記憶體問題,一定要用allocate什麼的一個函數分配。但是我用這個分配了之後沒報錯就是畫出來的東西都找不到……
android:寫了一個surfaceview的角色移動的程式,報錯null 指標,們幫忙看看
看這樣子數組要先聲明 Bitmap PlayerBmp[] = new Bitmap[9] ;咯,感覺上需要動態增加的話用list可能會好一點, List<Bitmap> PlayerBmp = new ArrayList<Bitmap>();
然後可以PlayerBmp.add(BitmapFactory.decodeResource(this.getResources(),R.drawable.robot02)
);試試看。