Drawing canvas on android widgets

I am trying to draw canvas on widgets but I have nothing. I have a form with imageviev on which the bitmap I will draw all the beauty, but it didn’t draw ... Here is a piece of my code that it doesn’t work?

My provider where i draw

public class MainActivity extends AppWidgetProvider {

private Bitmap bmp;
private RemoteViews views;

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    views = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.main);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.RED);
    paint.setTextSize(16);


    paint.setAntiAlias(true);
    paint.setTypeface(Typeface.MONOSPACE);

    Bitmap bmp = Bitmap.createBitmap(100, 16, Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(bmp);

    c.drawText("fdgfdgfdgfdfdfdgGFDFGFDDDDG", 0, 0, paint);

     views.setImageViewBitmap(R.id.imageView1, bmp);
}

      

}

and my main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="@drawable/ic_launcher" >
<ImageView 
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ImageView>

      

+3


source to share


3 answers


try putting different values ​​for x and y. It is currently drawing but is out of the bitmap ... so try this.



+1


source


It looks like you might be having multiple problems, but this might help with a couple.

First, your onUpdate method should be able to serve each of the widgets whose ids are passed to you. Note that users can create multiple instances of your widget at the same time on one or more screens. So the first thing you need to do is create a loop to attract all of them.

Then you need to notify the widget manager when each of these widgets is ready to be updated.

You may also need to use a different BitMap depth. ALPHA_8 doesn't work in my test, but only ARGB_4444.



Note also that the Bitmap and RemoteViews member variables hide the ones you create in the body of your method. I recommend removing the RemoteViews item and stack bitmap.

Putting it all together, this is what it looks like:

public class MainActivity extends AppWidgetProvider {
    private Bitmap bmp = Bitmap.createBitmap(100, 16, Bitmap.Config.ARGB_4444);

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for(int appWidgetId : appWidgetIds) {
            RemoteViews views = new RemoteViews(...);
            ...
            views.setImageViewBitmap(R.id.imageView1, bmp);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

      

You may encounter other problems as well. For example, it is important that all graphics calls are made on the graphics thread. Implementing a widget is much more complex than an application. Good luck!

+2


source


You need to call

appWidgetManager.updateAppWidget(appWidgetIds, views);

      

+1


source







All Articles