远程视窗setLayoutParams - 更改ImageView的尺寸内主屏幕小部件 [英] RemoteView setLayoutParams - Change ImageView Size Inside HomeScreen Widget

查看:394
本文介绍了远程视窗setLayoutParams - 更改ImageView的尺寸内主屏幕小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序显示,共有8个按键的Widget。我想给用户的风格,小部件和定制这是下面的按钮ImageViews规模的可能性。
这样做的目的是要离开的按钮,因为他们,而是要动态改变ImageView的大小。

有关该用户获取到设置图标大小,存储在共享preference作为一个整数的 iconSize

如何可以改变的ImageView的大小在一个Widget?

目前,该ImageViews与在XML文件中设置的大小创建。我怎样才能实现与其他尺寸重绘?

我认为没有太多code,张贴在这里,但我会很高兴,所以如果需要做的,只是告诉我,如果你有一个想法ç$ C可以帮助这里什么$。

我不想做的事:


  • 调整整个部件


  • 创建万吨布局文件根据iconSize切换


有些code:

这是我怎么设置我的活动ImageView的大小作为窗口小部件的preVIEW。的图标的是ImageViews数组,进度指的是用于选择进度的 iconSize

 为(ImageView的图标:图标){
    icon.requestLayout();    //转换成实际像素
    进度=(INT)TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_PX,
            进展,
            getResources()。getDisplayMetrics()
    );    //设置宽度和高度
    icon.getLayoutParams()高度=进度。
    。icon.getLayoutParams()宽=进展情​​况;    sizeText.setText(+进展);
}


解决方案

下面是一个小的解决方法,我发现:

简单的使用

  views.setViewPadding(R.id.vieId,左,上,右,下);

(意见= RemoteViews)

您只需要进行一些计算,这样一个的 iconSize 的100%(最大可能的大小)等于0填充和1%的 iconSize 的值等于最大填充。

它的工作没有了,太多,但我认为它不能伤害添加

 的android:cropToPadding =真

属性ImageViews如果你使用这种方法。

编辑:

我忘了设定的填充你应该更新部件后说,在某些时候(我做在的的onPause()当用户退出应用程序来看看小部件的)。
使用的 setPadding()的一个活动也将是没有出路的,而无需调用的无效()的对景,以迫使它的重绘/更新。

下面更code:

  seekBar.setOnSeekBarChangeListener(新SeekBar.OnSeekBarChangeListener(){
    @覆盖
    公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER){
        //映射值,100是我的搜索栏的定制最大
        //我加1,因为搜索栏从0-99的推移,
        //但是1%的尺寸为100%
        INT iconSize = 100 - (进展+ 1);        对于(ImageView的图标:图标){
            //设置填充
            icon.setPadding(iconSize,iconSize,iconSize,iconSize);            //强制的ImageView与新的填充,以重绘,这
            //作为图标尺寸的一个活preVIEW。
            icon.invalidate();
        }
    }    @覆盖
    公共无效onStartTrackingTouch(搜索栏搜索栏){
        //你可能想在这里做什么
    }    @覆盖
    公共无效onStopTrackingTouch(搜索栏搜索栏){
        //映射值
        INT iconSize = 100 - (seekBar.getProgress()+ 1);        //它保存在你的共享preferences或其他地方
        Utils.setDefaultsInt(Con._ICONSIZE,iconSize,MainPage.this);
    }});

My app shows a widget with a total of 8 buttons. I would like to give the user the possibility to style that widget and customize the size of the ImageViews which are below the buttons. The aim is to leave the Buttons as they are, but to change the ImageView sizes dynamically.

For this the User gets to set an icon Size, which is stored in a SharedPreference as an Integer iconSize.

How can I change the size of an ImageView in a Widget?

For now, the ImageViews are created with the size set in the xml file. How can I achieve a redraw with another size?

I assume there is not much code to post here but I will be glad to do so if necessary, just tell me if you have an idea what code could help here.

What I don't want to do:

  • resize the entire widget

  • create tons of layout files to switch according to the iconSize

Some Code:

This is how I set the ImageView size in my activity as a preview of the widget. icons is an array of ImageViews, progress refers to a ProgressBar that is used to choose iconSize.

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);
}

解决方案

Here is a little workaround I found:

simply use

views.setViewPadding(R.id.vieId, left, top, right, bottom);

(views = RemoteViews)

You just have to make some calculation, so that an iconSize of 100% (of the biggest possible size) equals 0 padding and 1% iconSize equals max padding.

It worked without, too but I think it can'T harm to add the

android:cropToPadding="true"

attribute to ImageViews if you use this method.

Edit:

I forgot to mention that at some point after setting the padding you should update the widget (I do it in onPause() when the user quits the application to look at the widget). Using the setPadding() in an activity will also lead to nowhere without calling invalidate() on the View, to force a redraw/update of it.

Here more code:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // Map the values, 100 is the custom max of my seekBar
        // I am adding 1 to because the seekBar goes from 0-99,
        // but the size from 1% to 100%
        int iconSize  = 100 - (progress+1);

        for (ImageView icon : icons) {
            // set the padding
            icon.setPadding(iconSize, iconSize, iconSize, iconSize);

            // force the ImageView to redraw with the new padding, this
            // serves as a live preview of the icons' sizes.
            icon.invalidate();
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // you might want to do something here
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // map the value
        int iconSize = 100 - (seekBar.getProgress()+1);

        // save it in your SharedPreferences or somewhere else
        Utils.setDefaultsInt(Con._ICONSIZE, iconSize, MainPage.this);
    }

});

这篇关于远程视窗setLayoutParams - 更改ImageView的尺寸内主屏幕小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆