我允许用户通过发送包含用户图库中图像的请求代码来更改Personalize类中的图标。 setIconImageinWidget()方法在此处发送结果(在Drag_and_Drop_App中):
else if(requestCode == RESULT_ICON){
byte[] byteArray = data.getByteArrayExtra("myIconBitmap");
Bitmap myIcon = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
setBackgroundImageForIcon(myIcon);
Log.d("Drag_and_Drop_App", "Icon is set");
}
}
这是setBackgroundImageForIcon方法:
@SuppressLint("NewApi")
private void setBackgroundImageForIcon(Bitmap bitmap) {
ImageView ivICON = (ImageView) findViewById(R.id.bwidgetOpen);
Drawable dq = new BitmapDrawable(getResources(), bitmap);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
ivICON.setImageDrawable(dq);
} else {
ivICON.setImageDrawable(dq);
Log.d("Drag_and_Drop_App", "Icon is set");
}
}
这不会返回任何错误,但根据用户选择使用的图片,图标根本不会更改。
看了一会儿后,我意识到我必须在这里更改编码的app widget提供程序部分:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class AFBWidget extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
Random r = new Random();
int randomInt = r.nextInt(1000000000);
String rand = String.valueOf(randomInt);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++){
int awID = appWidgetIds[i];
RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
v.setTextViewText(R.id.tvwidgetUpdate, rand);
Intent configIntent = new Intent(context, Drag_and_Drop_App.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
v.setOnClickPendingIntent(R.id.bwidgetOpen, configPendingIntent);
//me trying to set the Bitmap from the above classes somehow... v.setImageViewBitmap(R.id.bwidgetOpen, R.id.);
appWidgetManager.updateAppWidget(awID, v);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "Thanks for checking us out!", Toast.LENGTH_SHORT).show();
}
}
我正在改变的imageView是这样的:
<ImageView
android:id="@+id/bwidgetOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/desc"/>
Widget.xml中的
如何更改我的窗口小部件提供程序以便它可以更改图标? 我知道这有很多东西需要阅读,但任何帮助都已经过了!
更新:
@SuppressLint("NewApi")
private void setBackgroundImageForIcon(Bitmap bitmap) {
Log.d("Drag_and_Drop_App", "Icon...");
ImageView ivICON = (ImageView) findViewById(R.id.bwidgetOpen);
BitmapDrawable dq = new BitmapDrawable(getResources(), bitmap);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// ivICON.setImageDrawable(dq);
ivICON.setImageResource(R.drawable.pattern1);
} else {
// ivICON.setImageDrawable(dq);
ivICON.setImageResource(R.drawable.pattern1);
Log.d("Drag_and_Drop_App", "Icon is set");
}
}