Reputation: 712
I have implemented a number of similar activities, but for some reason, the following class is not reaching the onDraw call. The (very simple) class:
import ...etc.
public class SVPlay extends SurfaceView implements Callback {
Context context;
Point screenSize;
Bitmap backGroundImage
public SVPlay(Context _context) {
super(_context);
context = _context;
this.getHolder().addCallback(this);
setFocusable(true);
screenSize = DrawUtil.getInstance(context).getScreenSize();
backgroundImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.room);
backgroundImage = Bitmap.createScaledBitmap(backgroundImage, screenSize.x, screenSize.y, false);
}
@Override public boolean onTouchEvent(MotionEvent event) {
...
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(backgroundImage, 0, 0, paint);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
setWillNotDraw(false);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
This class is instantiated and run by an activity: public class PlayActivity extends Activity {
SVPlay svPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
svPlay = new SVPlay(this);
setContentView(svPlay);
}
....
}
This is similar to any number of other activity/surfaceView implementations that work for me, but for some reason onDraw is not called. To simplify the code for the purpose of the question, I eliminated the actual drawing activity, and test by setting a breakpoint in onDraw(). Execution does not reach the onDraw call. Adding a postInvalidate() call does not help. The constructor is called, and executes without any problems or exceptions. What is going on? 19/5/13 - Further investigation. This activity/SurfaceView is displayed when run as a separate project, so the problem isn't in the code displayed here, it's in the code that calls the activity. More soon...
Upvotes: 1
Views: 1837
Reputation: 712
Thanks to Rob K for a helpful comment. In the end, after carefully doing the process from scratch, the problem went away. I'm not sure what I was doing wrong, but it apparently had to do with setting up the intent or calling StartActivityForResult().
Upvotes: 0
Reputation: 294
Does the visible state of your app need to constantly refresh as fast as possible (like a rapidly-changing game)? If not, there isn't really any need for a SurfaceView, as you can just call invalidate() when required then implement the onDraw(Canvas) method.
If you DO need to render continually and quickly, then it makes sense to use the SurfaceView. I think you need to add a SurfaceHolder object in your SurfaceView class (see http://developer.android.com/reference/android/view/SurfaceHolder.html). Use SurfaceHolder.lockCanvas() to access the Canvas to draw to, then draw to it using the normal Canvas drawing functions, then call unlockCanvasAndPost(Canvas). You would need to spawn a separate thread to handle the rendering, and the thread would need an ongoing loop where you would call lockCanvas() and unlockCanvasAndPost(Canvas).
Upvotes: 1