Android Simple imageView Widget

时间:2017-06-11 13:48:49

标签: android android-studio imageview android-widget

我试图制作一个只包含一个imageView的小部件,当你点击它时,它会变成另一个图像,从我拍摄的六个图像中随机选择。

我的问题是,对于如何制作小部件没有很好的解释,所以我不知道如何做到这一点。

我已经制作了一个小部件布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/widget_description"
        android:onClick="widgetOnClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5"
        app:srcCompat="@mipmap/ic_widget" />
</LinearLayout>

我已经尝试在我的.java文件中创建一些代码,但那并没有做任何事情。

package com.ggblbl.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;

import java.util.Random;

/**
 * Implementation of App Widget functionality.
 */
public class RandomSideWidget extends AppWidgetProvider {

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

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

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

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    public void widgetOnClick(View v) {
        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
        Random rnd = new Random();
        int number = rnd.nextInt(6) + 1;

        switch(number) {
            case 1:
                imageView.setImageResource(R.drawable.widget_blue);
                break;
            case 2:
                imageView.setImageResource(R.drawable.widget_green);
                break;
            case 3:
                imageView.setImageResource(R.drawable.widget_orange);
                break;
            case 4:
                imageView.setImageResource(R.drawable.widget_pink);
                break;
            case 5:
                imageView.setImageResource(R.drawable.widget_red);
                break;
            case 6:
                imageView.setImageResource(R.drawable.widget_yellow);
                break;
            default:
                imageView.setImageResource(R.mipmap.ic_widget);
                break;
        }
    }
}

我使用API​​级别15。

2 个答案:

答案 0 :(得分:0)

您应该使用PendingIntent和IntentService来处理来自窗口小部件的点击。 在updateAppWidget()方法内,按如下方式输入代码:

Intent intent = new Intent(context, MyIntentService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 1001 + appWidgetId, intent, 0);
views.setOnClickPendingIntent(R.id.imageView, pendingIntent);

将您的onclick内容放在自定义类的onHandleIntent方法中,该方法扩展为IntentService

并且在更新完成后也调用相同的方法updateAppWidget()

答案 1 :(得分:0)

你可以简单地使用

views.setImageViewResource(R.id.YOUR_IMAGEVIEW_ID,R.drawable.YOUR_IMAGE);

代替

imageView.setImageResource(R.drawable.widget_blue);