Reputation: 1671
i'm new to creating/altering images of any kind in android. what i'm trying to do is the following:
i have a widget, that gets updated periodically from a broadcast receiver. thing is, i want to display a self-made (i.e. programmatically created) image in this widget. and i actually don't know how to ADD the image to the widgets layout. can anyone help me out? heres the code that updates the widget:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
// DO SOME THINGS...
ComponentName myWidget = new ComponentName(context.getApplicationContext(), MyWidgetProvider.class);
int[] allMyWidgetIds = appWidgetManager.getAppWidgetIds(myWidget);
for (int widgetId : allMyWidgetIds) {
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
rViews.setTextViewText(R.id.TextView01, "xyz"); // this works just fine, i.e. it gets updated as intended :)
appWidgetManager.updateAppWidget(widgetId, rViews);
}
// set next update
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, Config.UPDATE_RATE);
Date d = new Date(c.getTimeInMillis());
Intent i = new Intent(context.getApplicationContext(), MyReceiver.class);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent sender = PendingIntent.getBroadcast(context.getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
aManager.set(AlarmManager.RTC, d.getTime(), sender);
}
}
so this works just fine. in the line where "xyz" is written to the TextView, the data i assembled earlier (in the broadcastreceiver) is displayed as intended, i.e. gets updated. at this very point, i want to "draw" this data, i.e. create a graph that should be displayed in the widget.
but: how do i do this? i guess i'll have a drawable in the end. how do i actually get this to be displayed in the widget (and of course updated as well)?
UPDATE:
This is the view-class that should do the graphics. so far it just draws a short line. this works if i set this as a view via setContentView() in an activity:
public class Chart extends View {
public Chart(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
canvas.drawLine(0, 0, 30, 30, paint);
}
}
the layout of the widget is pretty boring:
<?xml version="1.0" encoding="utf-8"?>
<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/traffic_shape" >
<TextView
android:id="@+id/TextView01"
android:text="test"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<ImageView
android:id="@+id/graph"
android:contentDescription="blah"
android:src="@drawable/icon"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ImageView>
</LinearLayout>
no how do i get the view i create in my Chart-class to be displayed in the widget? Unfortunately RemoteViews does not offer any functionality to set a view as it seems... :(
Upvotes: 0
Views: 2004
Reputation: 1671
looks like i have to answer my own question. found the answer after painfull hours of struggling with the code >:(
for (int widgetId : allWidgetIds)
{
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Graph graph = new Graph();
rViews.setImageViewBitmap(R.id.graph, graph.getBitmap());
appWidgetManager.updateAppWidget(widgetId, rViews);
}
with Graph.java being sth like this:
public class Graph {
public Bitmap getBitmap() {
Bitmap bitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
canvas.drawLine(0, 0, 128, 128, paint);
return bitmap;
}
}
Upvotes: 1