我正在使用下面的代码使用remoteview在小部件imageview上设置图像现在我想将高度和宽度设置为imageview运行时.
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(WidgetActivity1_1.this);
RemoteViews remoteViews = new RemoteViews(WidgetActivity1_1.this.getPackageName(), R.layout.widget_layout1_1);
Intent configIntent = new Intent(WidgetActivity1_1.this,UpdateWidgetService.class);
configIntent.putExtra("Appid", appWidgetId);
PendingIntent configPendingIntent = PendingIntent.getService(WidgetActivity1_1.this, appWidgetId, configIntent, 0);
remoteViews.setImageViewResource(R.id.imgviewGrow, R.drawable.flower1);
}
Run Code Online (Sandbox Code Playgroud) 我的应用程序显示了一个小部件,共有 8 个按钮。我想让用户可以设置该小部件的样式并自定义按钮下方的 ImageViews 的大小。目的是让按钮保持原样,但动态更改 ImageView 大小。
为此,用户可以设置一个图标大小,它作为一个整数iconSize存储在 SharedPreference 中。
如何更改小部件中 ImageView 的大小?
现在,ImageViews 是使用 xml 文件中设置的大小创建的。如何使用其他尺寸实现重绘?
我认为这里没有太多代码可以发布,但如果有必要,我很乐意这样做,如果您知道哪些代码可以在这里提供帮助,请告诉我。
我不想做的事情:
调整整个小部件的大小
创建大量布局文件以根据 iconSize 进行切换
一些代码:
这就是我在活动中将 ImageView 大小设置为小部件预览的方式。icons是一个 ImageViews 数组,progress 指的是一个用于选择iconSize 的 ProgressBar。
for (ImageView icon : icons) {
icon.requestLayout();
// convert into actual Pixels
progress = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX,
progress,
getResources().getDisplayMetrics()
);
// set width and height
icon.getLayoutParams().height = progress;
icon.getLayoutParams().width = progress;
sizeText.setText("" + progress);
}
Run Code Online (Sandbox Code Playgroud)