How to set backgroundColor to view surface in android?

I use a surface view to draw an interactive piechart. here is my code that will look like all surface-looking examples.

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); } } } } } 

with this i can successfully draw pie charts. but the problem here is that "before loading the pie chart, the black rectangle is visible for a second, which is the default surface background." so I want to set the background color to view the surface to avoid a black rectangle.

Below is the modified code for drawing the background color on the surface view.

  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) { } } } 

with these changes, the problem with the black rectangle is resolved. but piechart is not refreshing properly. can someone help me solve either of these two problems.

+2
source share
1 answer
 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); } } } } } 

and in yout class client you can use setBackGroundColor (your color);)

+4
source

Source: https://habr.com/ru/post/903825/


All Articles