如何在android中将backgroundColor设置为surfaceview?

时间:2011-11-12 13:43:02

标签: android surfaceview

我正在使用表面视图来绘制交互式饼图。这是我的代码,它看起来像所有表面视图示例。

class PieChart extends SurfaceView implements SurfaceHolder.Callback {

public PieChart(Context context) {
        super(context);
        // Log.i("PieChart", "PieChart : constructor");
        getHolder().addCallback(this); 
}

@Override
    public void onDraw(Canvas canvas) {
        if (hasData) {

            resetColor();
            try {
                canvas.drawColor(getResources().getColor(R.color.graphbg_color));

                graphDraw(canvas);
            } catch (ValicException ex) {

            }

        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

        Log.i("PieChart", "surfaceChanged");

    }

    public int callCount = 0;

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

            try {
                // Log.i("PieChart", "surfaceCreated");
                mChartThread = new ChartThread(getHolder(), this);
                mChartThread.setRunning(true);

                if (!mChartThread.isAlive()) {
                    mChartThread.start();
                }

                Rect mFrame = holder.getSurfaceFrame();

                mOvalF = new RectF(0, 0, mFrame.right, mFrame.right);

            } catch (Exception e) {
                // No error message required
            }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // Log.i("PieChart", "surfaceDestroyed");
        boolean retry = true;
        callCount = 0;
        mChartThread.setRunning(false);
        while (retry) {
            try {
                mChartThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // No error message required
            }
        }
    }
}

class ChartThread extends Thread {

    private SurfaceHolder mSurfaceHolder;
    private PieChart mPieChart;
    private boolean mRefresh = false;

    public ChartThread(SurfaceHolder surfaceHolder, PieChart pieChart) {
        // Log.i("ChartThread", "ChartThread");
        mSurfaceHolder = surfaceHolder;
        mPieChart = pieChart;
    }

    public void setRunning(boolean Refresh) {
        // Log.i("ChartThread", "setRunning : " + Refresh);
        mRefresh = Refresh;
    }

    @Override
    public void run() {
        Canvas c;
        // Log.i("ChartThread", "run : " + mRefresh);
        while (mRefresh) {
            c = null;
            try {
                c = mSurfaceHolder.lockCanvas(null);
            //  c.drawColor(0xFFebf3f5);
                synchronized (mSurfaceHolder) {

                    mPieChart.onDraw(c);

                }
            } catch (Exception ex) {

            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

有了这个我能够成功绘制饼图。但这里的问题是“在加载饼图之前,黑色矩形是可见的,这是Surfaceview的默认背景”。所以我想为表面视图设置背景颜色以避免黑色矩形。

以下是将背景颜色绘制到表面视图的更改代码。

 public PieChart(Context context) {
            super(context);
            // Log.i("PieChart", "PieChart : constructor");
            getHolder().addCallback(this); 
            setBackgroundColor(getResources().getColor(R.color.graphbg_color));
    }


@Override
    public void onDraw(Canvas canvas) {
        if (hasData) {
             setBackgroundColor(getResources().getColor(R.color.graphbg_color));
            resetColor();
            try {
        canvas.drawColor(getResources().getColor(R.color.graphbg_color));

                graphDraw(canvas);
            } catch (ValicException ex) {

            }

        }
    }

通过这些更改,解决了黑色矩形问题。但是饼图没有正确刷新。有人可以帮我解决这两个问题中的任何一个。

1 个答案:

答案 0 :(得分:4)

class PieChart extends SurfaceView implements SurfaceHolder.Callback {

private int backGroundColor = Color.BLACK;

public PieChart(Context context,int backGroundColor) {
    super(context);
    setBackGroundColor(backGroundColor);
    // Log.i("PieChart", "PieChart : constructor");
    getHolder().addCallback(this); 
}

public void setBackGroundColor(int color){
    this.backGroundColor = color;
}

@Override
public void onDraw(Canvas canvas) {

            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setStrokeWidth(3);
            paint.setAntiAlias(true);
            paint.setColor(backGroundColor);
    canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), paint);

    if (hasData) {

        resetColor();
        try {

            canvas.drawColor(getResources().getColor(R.color.graphbg_color));

            graphDraw(canvas);
        } catch (ValicException ex) {

        }

    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    Log.i("PieChart", "surfaceChanged");

}

public int callCount = 0;

@Override
public void surfaceCreated(SurfaceHolder holder) {

        try {
            // Log.i("PieChart", "surfaceCreated");
            mChartThread = new ChartThread(getHolder(), this);
            mChartThread.setRunning(true);

            if (!mChartThread.isAlive()) {
                mChartThread.start();
            }

            Rect mFrame = holder.getSurfaceFrame();

            mOvalF = new RectF(0, 0, mFrame.right, mFrame.right);

        } catch (Exception e) {
            // No error message required
        }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // Log.i("PieChart", "surfaceDestroyed");
    boolean retry = true;
    callCount = 0;
    mChartThread.setRunning(false);
    while (retry) {
        try {
            mChartThread.join();
            retry = false;
        } catch (InterruptedException e) {
            // No error message required
        }
    }
}
}

class ChartThread extends Thread {

private SurfaceHolder mSurfaceHolder;
private PieChart mPieChart;
private boolean mRefresh = false;

public ChartThread(SurfaceHolder surfaceHolder, PieChart pieChart) {
    // Log.i("ChartThread", "ChartThread");
    mSurfaceHolder = surfaceHolder;
    mPieChart = pieChart;
}

public void setRunning(boolean Refresh) {
    // Log.i("ChartThread", "setRunning : " + Refresh);
    mRefresh = Refresh;
}

@Override
public void run() {
    Canvas c;
    // Log.i("ChartThread", "run : " + mRefresh);
    while (mRefresh) {
        c = null;
        try {
            c = mSurfaceHolder.lockCanvas(null);
        //  c.drawColor(0xFFebf3f5);
            synchronized (mSurfaceHolder) {

                mPieChart.onDraw(c);

            }
        } catch (Exception ex) {

        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}
}

在您的班级客户端中,您可以使用设置BackGround Color(您的颜色);)