Home android Working with SurfaceView

Working with SurfaceView

Author

Date

Category

Of the many sources I concluded that the inheritance of class SurfaceView will allow the paint on canvas without using the method onDraw () .

However, it does not turn. Consider an example.

The class does not bring Activiti – it just sets the SurfaceView to twist the content, everything is standard
.
Subclass SurfaceView:

package hivasya.noteAll;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.content.Context;
import android.graphics *.;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
Import android.util.log;
import android.os.SystemClock;
public class HVnoteView1 extends
 SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "hivasya";
private Canvas canvas;
private Bitmap space;
private int w, h;
public HVnoteView1 (Context context, AttributeSet attrs) {// constructor
   super (context, attrs);
   getHolder () addCallback (this).;
   Log.d (TAG, "construct View1 finished");
} // end constructor
@Override
protected void onSizeChanged (int sw, int sh, int oldw, int oldh) {
   super.onSizeChanged (sw, sh, oldw, oldh);
   w = sw;
   h = sh;
}
@Override
public void onDraw (Canvas c) {
   Log.d (TAG, "onDraw");
   //c.drawBitmap(space, 0, 0, null);
}
@Override
public void surfaceChanged (SurfaceHolder hold, int format,
int width, int height) {
   Log.d (TAG, "surfaceChanged");
   } // end surfaceChanged
@Override
public void surfaceCreated (SurfaceHolder holder) {
   Log.d (TAG, "surfaceCreated");
   if (holder.getSurface (). isValid ()) Log. d (TAG, "surface valid");
   canvas = null;
   space = Bitmap.createBitmap (w, h, Bitmap.Config.ARGB_8888);
   space.eraseColor (Color YELLOW.);
   Try {
      if (canvas == null) Log. d (TAG, "canvas is null");
      canvas = holder.lockCanvas ();
      if (canvas! = null) Log. d (TAG, "canvas not null");
      synchronized (holder) {
      canvas.drawBitmap (space, 0, 0, null);
      Log. d (TAG, "synchronized");
      SystemClock.sleep (2000); // freeze the picture on the case,
                   // if it is redrawn
        }
      }
      catch (Exception e) {Log. e (TAG, "lock canvas Error" + e.toString (), e);}
      finally {if (canvas = null!) holder.unlockCanvasAndPost (canvas);}
   } // end surfaceCreated
@Override
public void surfaceDestroyed (SurfaceHolder hold) {
   Log.d (TAG, "surfaceDestroyed");
   } // end surfaceDestroyed
} // end class

Pay attention to my paranoid logs, monitoring each step.
Here’s what brings LogCat:

05-03 20: 37: 37.734: D / hivasya (25674): construct View1 finished
05-03 20: 37: 37.810: D / hivasya (25674): surfaceCreated
05-03 20: 37: 37.810: D / hivasya (25674): surface valid
05-03 20: 37: 37.842: D / hivasya (25674): canvas is null
05-03 20: 37: 37.846: D / hivasya (25674): canvas not null
05-03 20: 37: 37.861: D / hivasya (25674): synchronized
05-03 20: 37: 39.863: D / hivasya (25674): surfaceChanged
05-03 20: 37: 39.887: D / hivasya (25674): onDraw
05-03 20: 37: 40.011: D / hivasya (25674): onDraw

As if everything works, Holder received, the validity of the surface, canvas prepared.

Error The system does not issue, but does not draw.
If the method onDraw uncomment the line

//c.drawBitmap(space, 0, 0, null);

drawing executed – the screen is colored yellow

.

How do you get the canvas except through the parameter of onDraw ?
Holder tried to get at through the parameter, and the method getHolder () ,
It is also useless.
Thank you for your attention.


Answer 1, Authority 100%

Since the response received is not from the study of the documentation, and trial and error, I presume that it can someone be useful.

As it turned out, the reason is still in the class of activist.
If in the Activity class to install communication with SurfaceView
XML markup, for example SetContentView (r.Layout.main) , then in the subclass SurfaceView method onDraw () .

Advantages: Scroll and scaling can be easily implemented.

Disadvantages: each time the ONDRAW () can be redrawn completely. When drawing a finger, it is called by offset to each pixel. An attempt to draw out onDraw () – see my question higher.

Note: In the designer of the SurfaceView except argument context , there must be the attributeset argument, otherwise the application immediately ends with an error.

If the activist is used to install the SurfaceView instance, for example setContentView (New MysurfaceView (this)) method ONDRAW () . Obtaining the Canvas method LockcanVas and the subsequent drawing is possible. But here it begins his headache.
For example, drawn in the onsurfacecreated () method is not saved at subsequent attempts to draw. Documentation says that the LockcanVas method
You can pass the “dirty” rectangle, beyond which the pixels between the Lockcanvas calls will be saved. But it does not work. The argument type Rect “dirty” rectangle will be stretched to full screen. (Tested by the DrawRect method, which is transmitted to the same rectangle.
Well, that is not surprising, everything will be more difficult with scrolling and scaling.

Note: Example is higher for a designer with one argument type Context . Here ATTRIBUTIESET is not required. If this second argument is present, it can be sent NULL .

Thus, the setContentView () method sets one of the approaches to the drawing on the canvas.

Verified on the Lenovo tablet using IDE Eclipse.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions