Friday, April 15, 2011

Drawing on widget canvas

There are a lot of examples about android canvas. But how to be if we want to draw something in our widget?

Here is simple example how to do this using RemoteViews.

//create a remote view from widget layout
RemoteViews views = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.widget_layout);

//create a bitmap
bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);

//create a canvas from existant bitmap that will be used for drawing
Canvas canvas = new Canvas(bitmap);

Than we can use android canvas methods to draw all we want.

//create new paint
Paint p = new Paint(); 
p.setAntiAlias(true);
p.setColor(hands_color);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(2);

//draw circle
canvas.drawCircle(50, 50, 7, p);

//set our bitmap to view
views.setImageViewBitmap(R.id.imageView, bitmap);

Have a nice code!

3 comments:

  1. Можно пожалуйста полный текст java файла с рисованием круга на виджете? i@f-ph.ru

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Выложи весь код пожалуйста, а то не понятно что и куда писать (:

      Delete