Russian Federation
Russian Federation

Reputation: 194

OnDraw in android ImageView, Whats wrong

Guys I want to draw a arc using different colors, but it doesn't show anything on activity at all, I am new to android,

Thanks,

@Override
public void onDraw(Canvas canvas)
{
    try {
        Paint paint=new Paint();
        paint.setColor(Color.RED);

        Random rnd = new Random();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(12);

        Path obj=new Path();
        RectF rf=new RectF();
        RectF rectF = new RectF(50, 20, 500, 580);
        canvas.drawArc (rectF, 90, 45, true, paint);


        int i;
        for(i=0;i<500;i++)
        {

            Thread.sleep(10000);
            paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
            canvas.drawPath(obj, paint);
        }

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 360

Answers (3)

Andy.Zhao
Andy.Zhao

Reputation: 258

1) If you just want to draw a arc using different colors,this is my code.

paint = new Paint();
rectF = new RectF(50, 20, 500, 580);
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawColor(Color.GREEN);
paint.setStrokeWidth(12);

int startAngle = 90,sweepAngle = 45;
for(int i = 0; i < 10; i++)
{
    paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    canvas.drawArc(rectF, startAngle, sweepAngle, true, paint);    //绘制圆弧
    startAngle += sweepAngle;
}

2) You can use canvas.drawPath() to draw any specified path,like that

rnd = new Random();
obj = new Path();
obj.moveTo(50, 100);
for(int i = 0; i < 10; ++i)
{
    obj.lineTo(rnd.nextInt(100), rnd.nextInt(100));
}
canvas.drawPath(obj, paint);

3) Use this view:

<com.zfeng.stackquesdraw.TestOnDraw
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Upvotes: 0

user2413972
user2413972

Reputation: 1355

1) Create a pencil in onDraw bad practice. (Long operation)

2) You need super.onDraw

@Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

3) Remove Thread.sleep (10000);

4) You need an asynctask that will call invalidate() in onProgressUpdate. Your code will look like this:

class StupidTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
        try {
        int i;
        for(i=0;i<500;i++)
        {
            Thread.sleep(10000);
            paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
            publishProgress();
        }

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... params) {
            super.onProgressUpdate(values);
            postInvalidate();
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            invalidate();
        }
}

Upvotes: 1

StiggyBr0
StiggyBr0

Reputation: 103

My guess is the onDraw method actually needs to complete. So try doing your thread sleeping in the activity and invalidate the view every 10 seconds, which will call onDraw again

Upvotes: 0

Related Questions