Write and erase SurfaceView freely in surfaceview, and write SurfaceView
//////// Inherit the class public class PaintView extends SurfaceView implements Runnable and SurfaceHolder of SurfaceView. 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 (); // Obtain holder mSurfaceHolder. addCallback (this); mSurfaceHolder. setFormat (PixelFormat. OPAQUE); // OPAQUE // mSurfaceHolder. setFormat (PixelFormat. RGB_565); // mSurfaceHolder. setFormat (PixelFormat. RGBA_8888); this. setFocusable (true); // setZOrderOnTop (true); // put it to the top-level mPaint = new Paint (); mPath = new Path (); mPaint. setAntiAlias (true); mPaint. setStyle (Style. STROKE); mPaint. setStrokeWidth (5); mPaint. setColor (mPaint Color); mPaint. setStrokeCap (Paint. cap. ROUND); // ROUND head mPaint. setDither (true); // remove the pull to smooth the screen into the mPaint. setStrokeJoin (Paint. join. ROUND); // method of combination, smooth} @ 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 (); // start thread} @ Override public void surfaceDestroyed (SurfaceHolder holder) {// TODO Auto-generated method stub mLoop = false; // end thread} @ Override public void run () {while (mLoop = true) {Draw (); try {Thread. sleep (100);} catch (InterruptedException e) {e. printStackTrace () ;}}// drawing 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);} // update the invalidate (); return true;} // call private void touchDown (MotionEvent event) when the screen is clicked) {// hide the previous drawing // mPath. reset (); // float x = event. getX (); float y = event. getY (); mX = x; mY = y; // the start point of mPath. moveTo (x, y);} // call private void touchMove (MotionEvent event) {final float x = event when the finger slides on the screen. getX (); final float y = event. getY (); final float previusx = mX; final float previusy = mY; final float dx = Math. abs (x-previusx); final float dy = Math. abs (y-previusy); // when the distance between two points is greater than or equal to 3, The beiser curve is generated if (dx> = 3 | dy> = 3) {// set the start point and end point to half float cX = (x + previusx)/2; float cY = (y + previusy)/2; // implement a smooth curve by secondary besell. previusx, previusy is the operation point, cX, and cY are the end point mPath. quadTo (previusx, previusy, cX, cY); // The coordinate value of the first end call is used as the initial coordinate value mX = x of the second call; mY = y ;}/// eraser 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); // ROUND head mPaint. setDither (true); // remove the pull to smooth the screen into the mPaint. setStrokeJoin (Paint. join. ROUND); // combine to smooth the mPaint. setAlpha (0); // mPaint. setXfermode (new porterduxfermode (PorterDuff. mode. CLEAR) ;}//// // display the preceding view setContentView (new PaintView (this) in the Activity ));
Methods and examples of writing 3D with opengl in surfaceview
It's strange that I run it using the nehe android example, and there is always a memory problem. I must use a function like allocate to allocate it. But after I use this assignment, no error is reported, that is, I cannot find anything I have drawn ......
Android: I wrote a surfaceview role movement program and reported a null pointer. Let's take a look.
To look at this array, you must first declare Bitmap PlayerBmp [] = new Bitmap [9]; slightly, it may be better to use the list if you need to add it dynamically, list <Bitmap> PlayerBmp = new ArrayList <Bitmap> ();
Then PlayerBmp. add (BitmapFactory. decodeResource (this. getResources (), R. drawable. robot02)
); Try it.