我正在开发一个右侧带有图像的小部件,用户可以在其中一个设置屏幕中选择该小部件。
然后用户可以使用 ImageView.setScaleType("CENTER") 设置图像。那行得通。
然后,图像的 URL 作为 URL 以及 Base64 编码的位图字符串存储在首选项中(因为我想缩小图像并且用户可以在设置中旋转它)
在小部件中,我加载图像。效果很好。使用 URI 和位图。
ImageView 的 ScaleType 在布局中也设置为固定值,也有效。
但是如何以编程方式定义小部件中 ImageView 的 ScaleType ?
因为我想将 ScaleType 设置为用户在设置中选择的值。使用 RemoteViews,我们无法获取 ImageView...
我尝试过:
myRemoteViews.setString(R.id.myImage, "setScaleType", "CENTER");
日志文件显示: ImageView 无法使用 setScaleType 函数。
有人知道如何在 AppWidget 中进行这个定义吗?
I am developing a widget with an image on the rigth side, which can be choosen by the user in one of the settings screen.
The image can then be set from the user with the ImageView.setScaleType("CENTER"). That works.
The URL of the image is then stored in the preferences as URL, and also as Base64 encoded String of the Bitmap (cause I want to shrink the image and the user can rotate it in the settings)
In the widget, I load the image. That works fine. With the URI and with the Bitmap too.
The ScaleType of the ImageView is set as well to a fixed value in the Layout, works too.
But how can I define the ScaleType of the ImageView in the widget programmatically?
Cause I want to set the ScaleType to the value, the user has choosen in the settings. With RemoteViews, we cannot get the ImageView...
I tried:
myRemoteViews.setString(R.id.myImage, "setScaleType", "CENTER");
The logfile says: The function setScaleType is not possible for ImageView.
Does someone know how to make this definition inside the AppWidget?
发布评论
评论(3)
您可以使用此段以编程方式设置图像视图的比例类型。
希望它有帮助...
You can use this segment to set scale type for Image View programatically..
Hope it helps...
如果你看
ImageView
的源码,你会发现setScaleType
方法缺少@android.view.RemotableViewMethod
注解,所以它可以不能通过RemoteViews
接口调用。对于此限制有一个解决方法,但它并不完美:为您想要的每个 ScaleType 定义不同的布局 XML,并在创建
RemoteViews
之前在它们之间进行选择。如果您的布局很复杂,或者您有多个视图要应用此解决方法,您可能需要使用RemoteViews.addView
在 main 中添加特定的ImageView
布局 布局。我正在对我的应用中的应用小部件使用此解决方法(使用
addView
)淋浴。此方法的问题在于,库存启动器有一个错误:如果您通过创建新的 RemoteViews 并调用 addView更改现有应用程序小部件的缩放类型/code> 向其添加不同的ImageView
布局,它并不总是意识到布局已更改。If you look at the source code of
ImageView
, you can see thesetScaleType
method lacks the@android.view.RemotableViewMethod
annotation, so it can't be called through theRemoteViews
interface.There's a workaround for this limitation, but it's not pretty: define a different layout XML for each ScaleType you want, and choose between them just before you create the
RemoteViews
. If your layout is complex, or you have more than one view to apply this workaround to, you might want to useRemoteViews.addView
to add the particularImageView
layout inside the main layout.I'm using this workaround (with
addView
) for appwidgets in my app Showr. The problem with this method is that the stock launcher has a bug: if you change the scale type of an existing appwidget by creating a newRemoteViews
and callingaddView
to add a differentImageView
layout to it, it doesn't always realise the layout has changed.由于在删除视图上调用
setScaleType
并不是一种简单的方法,因此我想出的解决方法是在布局中创建三个 imageView,然后更改没有缩放类型的可见性由用户选择:Since the is no simple way to call
setScaleType
on the remove view the work around I came up with was making three imageViews in my layout and then changing the visibility of the ones that don't have the scale type selected by the user: