【问题标题】:Proper way to update ImageView in a RemoteViews?在 RemoteViews 中更新 ImageView 的正确方法?
【发布时间】:2014-10-23 07:07:37
【问题描述】:

我正在尝试从远程 URL 获取图像并将其放入 ImageView。 ImageView 本身位于应用小部件中,因此根据我目前的理解,这意味着它被封装在 RemoteViews 中,并没有真正直接处理。

但我永远无法让应用小部件显示图像。我正在使用 AsyncTask 扩展从远程 URL 将图像作为位图获取,并且似乎图像被获取正常(无论如何都会返回非空位图),但我无法显示它来确认.

我认为我必须使用错误的方法来实际更新应用小部件的 ImageView。

以下是所有相关代码:

布局/new_app_widget.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/content" />
</RelativeLayout>

NewAppWidget.java 中的相关位:

public class NewAppWidget extends AppWidgetProvider {

...

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
        int appWidgetId) {

    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

    String strURL = "http://example.com/YqLOSfr4.png";

    new ImageFetchAsyncTask(strURL, R.id.imageView).execute();
    // ^^^^^
    // in the above, I pass the URL and the id of the imageview, fetch
    // the bitmap, and in the onPostExecute() method I use
    // setImageViewBitmap(viewId, bitmap) to load the bitmap into the imageview
    // 
    // I don't have the code to hand, but I can provide it.  For now, someone
    // may be able to tell me somewhere else I'm doing something wrong

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mike.widgetapptest" >
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        android:debuggable="true"
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".NewAppWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/new_app_widget_info" />
        </receiver>
    </application>

</manifest>

【问题讨论】:

  • 您只需要使用第三方库,它将在后台管理它自己的生命周期。您只需将图片网址提供给您的网络图片视图
  • Danial,由于我是新手,您认为您可以扩展一下您的评论吗?什么 3rd 方库,将图像 url 提供给网络图像视图是什么意思?
  • 你知道如何在android中使用库项目吗?
  • 还没有,但我很快就会!
  • 我给你一个链接androidhive.info/2014/05/android-working-with-volley-library-1你只需要按照你当前代码中的第2步,第3步,第8步。从那个链接

标签: android android-appwidget remoteview


【解决方案1】:

经典的初学者错误...我试图在异步任务之后立即执行某些操作,期望异步任务已经完成。因此,我将取决于异步任务结果的内容移到了异步任务的 onPostExecute() 方法中,瞧……图像显示在我的应用小部件中!

【讨论】:

  • 我想我可能会因为回答自己的问题而失去声誉!
  • 不,完全有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 2016-07-02
  • 1970-01-01
  • 2012-04-04
  • 2017-11-01
相关资源
最近更新 更多