0

我有一个显示coverflow动画的android应用程序。我想在coverflow中使用视图扩展surfaceview。但是这段代码没有显示任何内容。

public class CoverFlowView extends SurfaceView implements Callback {

    public CoverFlowView(Context context) {
        super(context);
    }

    public CoverFlowView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CoverFlowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }


    public void surfaceCreated(SurfaceHolder holder) {


         MyThread  myThread = new MyThread(holder);
         myThread.setFlag(true);
         myThread.start();
    }


    public void surfaceDestroyed(SurfaceHolder holder) {

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint borderPaint = new Paint();
        borderPaint.setARGB(255, 255, 128, 0);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(4);
        canvas.drawRect(0, 0, getWidth() - 1, getHeight() - 1, borderPaint);
    }

    class MyThread extends Thread {
        boolean flag;
        SurfaceHolder myHolder;
        public MyThread(SurfaceHolder holder) {
            myHolder = holder;
        }

        public void setFlag(boolean myFlag) {
            flag = myFlag;
        }

        public void run() {
            Canvas canvas = null;
            while (flag) {
                try {
                    canvas = myHolder.lockCanvas();
                    synchronized (myHolder) {
                          invalidate();
                        }

                } finally {
                    if (canvas != null) {
                        myHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }
    }

}

这里有什么问题?我是 Surfaceview 的新手。

提前致谢。

4

1 回答 1

0

像这样重写你的代码,然后它会正常工作,我已经检查过了

public class CoverFlowView extends SurfaceView implements SurfaceHolder.Callback {

然后将您的构造函数更改为

public CoverFlowView(Context context) {
    super(context);
    getHolder().addCallback(this);
}
于 2012-10-04T10:29:09.400 回答