机器人:如何自定义图像添加到小部件? [英] android: how to add a customized image to widgets?

查看:114
本文介绍了机器人:如何自定义图像添加到小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来创建/改变在Android的任何类型的图像。我想要做的是以下内容:

i'm new to creating/altering images of any kind in android. what i'm trying to do is the following:

我有一个小工具,这会从广播接收器定期更新。事情是,我想在这个小工具来显示一个自制的(即程序创建的)图像。实际上我不知道如何将图像添加到小部件的布局。
谁能帮助我吗?继承人的code即更新微件:

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

所以这个工作得很好。在这里xyz被写入TextView的,我先前组装(在广播接收器)的数据线按预期显示,即,被更新。
在这个非常点,我想画这个数据,即创建应该被显示在窗口小部件的曲线图。

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)?

更新:

这是视图类应该做的图形。到目前为止,它只是绘制一个短线。这个作品,如果我将这个选项设为活动通过的setContentView()视图:

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

小部件的布局是pretty无聊的:

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>

没有我如何得到我在表级创建视图来显示在窗口小部件?不幸的是,因为它似乎RemoteViews不提供任何功能,以设置视图...:(

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... :(

推荐答案

看起来像我来回答我的问题。找到了答案与code挣扎不好受小时后>(

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

与Graph.java是某事像这样:

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

这篇关于机器人:如何自定义图像添加到小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆