Android RemoteViews,如何在小部件内设置 ImageView 的 ScaleType?

发布于 2024-12-12 04:35:20 字数 585 浏览 0 评论 0 原文

我正在开发一个右侧带有图像的小部件,用户可以在其中一个设置屏幕中选择该小部件。 然后用户可以使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-12-19 04:35:20

您可以使用此段以编程方式设置图像视图的比例类型。

 imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

希望它有帮助...

You can use this segment to set scale type for Image View programatically..

 imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

Hope it helps...

老街孤人 2024-12-19 04:35:20

如果你看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 the setScaleType method lacks the @android.view.RemotableViewMethod annotation, so it can't be called through the RemoteViews 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 use RemoteViews.addView to add the particular ImageView 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 new RemoteViews and calling addView to add a different ImageView layout to it, it doesn't always realise the layout has changed.

一瞬间的火花 2024-12-19 04:35:20

由于在删除视图上调用 setScaleType 并不是一种简单的方法,因此我想出的解决方法是在布局中创建三个 imageView,然后更改没有缩放类型的可见性由用户选择:

    // first I declared a map to associate preference values with imageView ids

    private static final HashMap<Integer,Integer> widgetImageViewIds;

    static {
        widgetImageViewIds = new HashMap<Integer, Integer>();
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_START, R.id.imageViewStart);
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_CENTER, R.id.imageViewCenter);
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_END, R.id.imageViewEnd);
    }

    ...

    // then when I update my widget show/hide the widgets based on the preference

    for (HashMap.Entry<Integer, Integer> entry : widgetImageViewIds.entrySet()) {
        Integer imageViewAlignment = entry.getKey();
        Integer imageViewId = entry.getValue();
        views.setViewVisibility(imageViewId, imageViewAlignment.intValue() != imageAlignmentPreferenceValue ? View.GONE : View.VISIBLE);
    }

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:

    // first I declared a map to associate preference values with imageView ids

    private static final HashMap<Integer,Integer> widgetImageViewIds;

    static {
        widgetImageViewIds = new HashMap<Integer, Integer>();
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_START, R.id.imageViewStart);
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_CENTER, R.id.imageViewCenter);
        widgetImageViewIds.put(MyAppPreferences.PREF_ALIGN_END, R.id.imageViewEnd);
    }

    ...

    // then when I update my widget show/hide the widgets based on the preference

    for (HashMap.Entry<Integer, Integer> entry : widgetImageViewIds.entrySet()) {
        Integer imageViewAlignment = entry.getKey();
        Integer imageViewId = entry.getValue();
        views.setViewVisibility(imageViewId, imageViewAlignment.intValue() != imageAlignmentPreferenceValue ? View.GONE : View.VISIBLE);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文