• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps surfaceview setbackgrounddrawable()

mrqs

Android Expert
Code:
public class V extends SurfaceView implements SurfaceHolder.Callback {
    private CThread mThread; // see next code block for class
    public int x;
    public int y;

    public V(Context context) {
        super(context);
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        mThread = new CThread(holder,this);
        x = 0;
        y = 0;
        setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawLine(x, y, getWidth(), getHeight(), paint);
    }

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                x = (int) event.getX();
                y = (int) event.getY();
                default: break;
        }
        return true;
    }
    public void surfaceCreated(SurfaceHolder holder) {
        mThread.setRunning(true);
        mThread.start();
    }
/* sufraceDestroyed etc */
}
Code:
public class CThread extends Thread {

    private SurfaceHolder mSurfaceHolder;
    private V mView;
    private boolean mRun = false;
    
    public CThread(SurfaceHolder surfaceHolder, V view) {
        mSurfaceHolder = surfaceHolder;
        mView = view;
    }

    public void setRunning(boolean run) {
        mRun = run;
    }

    @Override
    public void run() {
        Canvas c;
        while (mRun) {
            c = null;
            try {
                c = mSurfaceHolder.lockCanvas(null);
                synchronized (mSurfaceHolder) {
                    mView.onDraw(c);
                }
            } finally {
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
if i leave the setBackgroundDrawable() call out of the V constructor, i get black background with a line drawn from the lower right corner to the top left corner; and where ever i move the "cursor" afterwards, but when i have that method call, i only get the initial x=y=0 line drawn on top of the background image and moving the "cursor" doesn't do anything

what's up with that?
 
Back
Top Bottom