【问题标题】:android: how to add a customized image to widgets?android:如何将自定义图像添加到小部件?
【发布时间】:2012-02-23 21:53:17
【问题描述】:

我不熟悉在 android 中创建/更改任何类型的图像。我想要做的是:

我有一个小部件,它会从广播接收器定期更新。事情是,我想在这个小部件中显示一个自制的(即以编程方式创建的)图像。而且我实际上不知道如何将图像添加到小部件布局中。 谁能帮我吗?这是更新小部件的代码:

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 的行中,我之前组装的数据(在广播接收器中)按预期显示,即得到更新。 此时,我想“绘制”这些数据,即创建一个应该在小部件中显示的图表。

但是:我该怎么做?我想我最后会有一个drawable。我如何真正让它显示在小部件中(当然也要更新)?

更新:

这是应该做图形的视图类。到目前为止,它只是画了一条短线。如果我在活动中通过 setContentView() 将其设置为视图,则此方法有效:

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

小部件的布局很无聊:

<?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 没有提供任何设置视图的功能... :(

【问题讨论】:

  • 我不太明白,就像你的布局中有一个TextView,添加一个ImageView。您还在寻找什么?
  • 是的 - 我有这个图像视图。现在假设,我有这个扩展“视图”的类 x,我确实在那里创建了我的图像......但是现在:我如何让视图(由类 x 表示)显示在 xml 中定义的图像视图中布局?

标签: android graphics drawable


【解决方案1】:

看来我必须回答我自己的问题。在与代码苦苦挣扎数小时后找到了答案>:(

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 是这样的:

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

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多