Android 简单的 imageView 小部件
Android Simple imageView Widget
我正在尝试制作一个仅包含一个 imageView 的小部件,当您点击它时,它会更改为另一张图片,该图片是从我制作的六张图片中随机选择的。
我的问题是,对于如何制作小部件没有很好的解释,所以我不知道该怎么做。
我已经做了一个widget布局:
<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 级。
您应该使用 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 内容放在自定义 class 的 onHandleIntent
方法中,该方法扩展了 IntentService
.
并在更新完成后调用相同的方法updateAppWidget()
。
你可以简单地使用
views.setImageViewResource(R.id.YOUR_IMAGEVIEW_ID,R.drawable.YOUR_IMAGE);
而不是
imageView.setImageResource(R.drawable.widget_blue);
我正在尝试制作一个仅包含一个 imageView 的小部件,当您点击它时,它会更改为另一张图片,该图片是从我制作的六张图片中随机选择的。
我的问题是,对于如何制作小部件没有很好的解释,所以我不知道该怎么做。
我已经做了一个widget布局:
<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 级。
您应该使用 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 内容放在自定义 class 的 onHandleIntent
方法中,该方法扩展了 IntentService
.
并在更新完成后调用相同的方法updateAppWidget()
。
你可以简单地使用
views.setImageViewResource(R.id.YOUR_IMAGEVIEW_ID,R.drawable.YOUR_IMAGE);
而不是
imageView.setImageResource(R.drawable.widget_blue);