-
更多相关内容
-
labelimg 标注图像时 生成的size 那里的 weight 和height 有时候为0
2019-03-27 09:37:29注意在 labelimg 标注图像时 生成的size 那里的 weight 和height 有时候为0。 在执行label.py的时候会提示错,如下: root@jdh-Precision-Tower-5810:/home/meiling/darknet/quexian# python label.py Traceback ...RT
注意在 labelimg 标注图像时 生成的size 那里的 weight 和height 有时候为0。
在执行label.py的时候会提示错,如下:
root@jdh-Precision-Tower-5810:/home/meiling/darknet/quexian# python label.py
Traceback (most recent call last):
File "label.py", line 57, in <module>
convert_annotation( image_id)
File "label.py", line 44, in convert_annotation
bb = convert((w,h), b)
File "label.py", line 15, in convert
dw = 1./size[0]
ZeroDivisionError: float division by zero此时检查一下:
-
动态添加控件导致weight和height失效的解决方法
2016-07-21 16:35:16:任何使用 addView(...) 的方法,无论你所实例化的 View 本身的 xml 的 width 和 height 设置了什么,都是没效果的,请看清楚,是 width height 失效,上面的 scaleType 是有效的, 问题 java 代码中调用 addView ...本文转自:一个关于 imageView 设置 scaleType 的问题。就在刚才 晚上9 点多的时候,我的一个外包伙伴发一个工程代码我,叫我去看下这样一个"bug",说折腾了很久,图片选择器在选择完图片后,就要显示图片到界面上,大家可以想象下 微信 发表图片,因为我们相机的图片肯定是 长宽都不一致的,为了统一格式,一般都是把要显示出来的 imageView 设置成 scaleType = centerCrop 或者 center。
问题就是:他在设置了上面的属性后,宛然无效!imageView 设置成 scaleType = centerCrop 或者 center,对图片没效果。
先上事例图:
理想效果 和 问题效果(左->右):
公用的 xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <LinearLayout 12 android:id="@+id/images_container" 13 android:paddingLeft="10dp" 14 android:orientation="horizontal" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content"> 17 18 19 20 </LinearLayout> 21 22 23 </RelativeLayout>
我们来看看出问题的代码:
imageView 的 xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <ImageView 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/image_one" 5 android:scaleType="centerCrop" 6 android:layout_width="30dp" 7 android:layout_height="30dp" />
他设置了动态 addView() 的方法 添加用户选中的 图片,java 代码,为了避免长篇大论,我已做简化,事例效果一样:
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.test); 5 final ImageView image = (ImageView) LayoutInflater.from(this).inflate(R.layout.send_post_image, null, false); 6 LinearLayout images_container = (LinearLayout) findViewById(R.id.images_container); 7 image.setImageResource(R.drawable.beni); 8 images_container.addView(image); 9 }
这样的代码,貌似没什么问题,通过 LayoutInflater.from(this).inflate(...) 来事例化一个 View,而最为之关键的是,这个View 就是上面的 imageView的xml,里面明确设置了 width 和 height 是 30dp,显示方式是 centerCrop,最后通过 addView 添加到一个 linearLayout 中。但是,这份代码显示出来的效果是 右图,非 理想效果!!哈哈,感觉恍然大悟吧。
你可能会有这样一个印象,我们绝大多项目或者是练习中的 imageVIew 使用都是直接findViewById, 一样的 imageView 设置,却没问题, 没错,的确没问题,你可以把上面的代码替换为这个试试:
test.xml 换为:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <LinearLayout 12 android:id="@+id/images_container" 13 android:paddingLeft="10dp" 14 android:orientation="horizontal" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content"> 17 <ImageView 18 android:id="@+id/image" 19 android:scaleType="centerCrop" 20 android:layout_width="30dp" 21 android:layout_height="30dp" /> 22 23 24 </LinearLayout> 25 26 27 </RelativeLayout>
java 换为
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.test); 5 final ImageView image = (ImageView)findViewById(R.id.image); 6 image.setImageResource(R.drawable.beni); 7 }
这样显示出来的 效果 就是我们所 期望的。
为什么通过 addView() 的方法却败了呢?
问题的原因是这样的:任何使用 addView(...) 的方法,无论你所实例化的 View 本身的 xml 的 width 和 height 设置了什么,都是没效果的,请看清楚,是 width height 失效,上面的 scaleType 是有效的, 问题 java 代码中调用 addView 的时候并没有传入 LayoutParam 布局参数,好了,我们来 粘下 源码,见证真相只有一个。
1 /** 2 * <p>Adds a child view. If no layout parameters are already set on the child, the 3 * default parameters for this ViewGroup are set on the child.</p> 4 * 5 * <p><strong>Note:</strong> do not invoke this method from 6 * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, 7 * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> 8 * 9 * @param child the child view to add 10 * 11 * @see #generateDefaultLayoutParams() 12 */ 13 public void addView(View child) { 14 addView(child, -1); 15 } 16 17 public void addView(View child, int index) { 18 if (child == null) { 19 throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); 20 } 21 LayoutParams params = child.getLayoutParams(); 22 if (params == null) { 23 params = generateDefaultLayoutParams(); 24 if (params == null) { 25 throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); 26 } 27 } 28 addView(child, index, params); 29 } 30 31 protected LayoutParams generateDefaultLayoutParams() { 32 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 33 }
因为没有自己传入 LayoutParam 而 get 的又是 null ,为什么 get 的会是null,请在上面的事例代码中加入这句 log,Log.d("zzzzz",""+image.getLayoutParams()); ,就会看到输出 null 了,而最终调用源码的:
protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } 直接使用了 Wrap_parent,所以啊,导致了失效。那我就是要使用 addView 怎么办?自己加个宽高限制即可。
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.test); 5 final ImageView image = (ImageView) LayoutInflater.from(this).inflate(R.layout.send_post_image, null, false); 6 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50,50); /** 这里 */ 7 LinearLayout images_container = (LinearLayout) findViewById(R.id.images_container); 8 image.setLayoutParams(lp); /** 添加 */ 9 image.setImageResource(R.drawable.beni); 10 Log.d("zzzzz", "" + image.getLayoutParams()); 11 images_container.addView(image); 12 }
行了,快1点了。。。。
总结下。 导致这样的事情发生,我觉得还是缺少自己动手踏实编码的问题,现在框架比较泛滥了,又想起了那句老话:我们不生产代码,我们都是 github 的搬运工 ...
如果您认为这篇文章还不错或者有所收获,您可以通过扫描一下下面的支付宝二维码 打赏我一杯咖啡【物质支持】,也可以点击右下角的【推荐】按钮【精神支持】,因为这两种支持都是我继续写作,分享的最大动力
、
-
android之layout_width/layout_height和weight
2014-06-21 13:28:01(垂直)时,layout_weight只对height有效。 设布局控件为LinearLayout,其宽度为X,设置布局控件的android:orientation属性为horizontal(水平),在布局控件中放置n个Button,每个Button命名为b1、b2、...、...android的控件从初始化到显示到屏幕上,在绘制(Draw)之前,需要测量(Measure)其高度和宽度。下面以Button为例,用实验证明。
重写Button类:
布局文件:package com.zzj.ui.control; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.widget.Button; public class MyButton extends Button { public MyButton(Context context) { this(context, null); } public MyButton(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.buttonStyle); } public MyButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); System.out.println("width:" + this.getWidth()); System.out.println("height:" + this.getHeight()); System.out.println("MyButton" + this.getId() + ".onMeasure() is called in the time " + System.currentTimeMillis() +"!"); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); System.out.println("width:" + this.getWidth()); System.out.println("height:" + this.getHeight()); System.out.println("MyButton" + this.getId() + ".onDraw() is called in the time " + System.currentTimeMillis() +"!"); } }
效果:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <com.zzj.ui.control.MyButton android:id="@+id/wbutton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="b1" /> <com.zzj.ui.control.MyButton android:id="@+id/wbutton2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="b2" /> </LinearLayout>
输出:
我们可以看到测量了两次,绘制了一次,并且只绘制了一个按钮,因为第二个按钮测量出来的宽度为0,所以就没必要绘制了。实验证明,在绘制前确实是先测量。
修改一下布局文件:
效果:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <com.zzj.ui.control.MyButton android:id="@+id/wbutton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="b1" /> <com.zzj.ui.control.MyButton android:id="@+id/wbutton2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="b2" /> </LinearLayout>
输出:
从输出看出,进行了4次测量,可见控件会根据初始条件进行多次测量。
那么测量的公式是什么呢?
首先确认以下3点:
1.只有定义在LinearLayout中的控件,设置的layout_weight才有效。
2.当LinearLayout的android:orientation属性为horizontal(水平)时,layout_weight只对width有效。
3.当LinearLayout的android:orientation属性为vertical(垂直)时,layout_weight只对height有效。
设布局控件为LinearLayout,其宽度为X,设置布局控件的android:orientation属性为horizontal(水平),在布局控件中放置n个Button,每个Button命名为b1、b2、...、bn,每个Button的宽度width定义为w1、w2、...、wn,权重weight定义为g1、g2、...、gn,则按钮bi最终的宽度为:
wi + gi / (g1 + g2 + ... + gn) * (X - (w1 + w2 + ... + wn))
公式计算分两步:
1.测量bi通过layout_width设置的宽度(也就是wi)。
2.测量bi通过layout_weight设置的宽度。按钮bi通过weight设置的宽度等于bi的权重比(gi / (g1 + g2 + ... + gn))乘以屏幕的剩余宽度(X - (w1 + w2 + ... + wn))。
最终结果就是将两次测量的结果相加。
特别地,当bi的宽度layout_width设置为match_parent(fill_parent)时,wi=X。屏幕的剩余宽度可能为负数(X < (w1 + w2 + ... + wn)),所以会出现权重比越大的按钮最终宽度越小的现象。
需要注意的是,测量和绘制都是有先后顺序的,在布局文件中先定义的控件先测量或绘制。最后一次计算,前面控件宽度的总和如果超过了屏幕的宽度,则后面控件的宽度都为0,并且宽度为0的控件都不会被绘制。
系统默认的weight为0,如果不设置weight(g1 + g2 + ... + gn = 0),则无法进行第二次计算(分母为0)。
建议:如果要使用weight属性,使各个控件按设置的weight显示,显然应该设置width为0。
-
Python面向对象之类的定义和使用
2020-11-24 17:15:25任务:给定了一个 Dog 类,类中有 foot、weight 和 height 三个属性。请在类的外部输出这三个属性的值。 """ class Animal: foot = 4 weight = 14 height = 30 # 请在下面的Begin-End之间按照注释中给出的提示... -
真正的能理解CSS中的line-height,height与line-height
2018-09-20 15:48:21在最近的项目中,常常用到line-height,只是简单的理解为行高但并...行高与行距从字面的意思是非常容易理解的,但是对布局和样式来说,我们应该更深入的理解各个属性之间的关系,比如line-height与font-size存在什... -
Android weight布局均分和width(height)的一点关联
2016-12-28 15:43:40android:id="@+id/project_content_value_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/detail_item_circleProgre -
LinearLayout中layout_weight设置无效
2019-12-22 09:44:45发现LinearLayout中layout_weight设置无效,检查是否是RecyclerView或ListView中的item项(小问题点,其实也很容易遗漏) 解 1.先检查该布局下layout_width 或 layout_height是否有为0dp的(正常不会犯这种错误) 2.... -
layout_weight属性的用法和意义
2019-05-07 10:01:02一直没理解在LinearLayout中的layout_weight属性的意义,使用的时候都是将子控件的layout_width或者layout_height设置为0,然后在设置layout_weight的权重值,以至于在被问到如果设置了layout_width为具体的值时对... -
CSS样式:line-height、height与font-size的联系
2019-09-02 22:24:05(2)行距是上一行的底线和下一行的顶线之间的距离。 行距的一半就是半行距。上间距等于下间距。 (3)字体大小是同一行的顶线和底线之间的距离。 从图中可以看出: 行高=行间距+字体大小=上半行距+字体大小+下半... -
每天一点--css中min-height,min-weight,max-height,max-weight的妙用
2014-12-02 14:44:21在我们写前台界面的时候,很多时候呢,需要让... 大家都知道min即小,max即大,故min-height也就是最小高度,max-height也就是最大高度,最小和最大即产生了一个交互区,这个交互区可能就是我们用户体验的最佳区域了。 -
C++定义一个Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及...
2022-04-14 20:06:50定义一个Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数,声明一个Box对象,观察构造函数和析构函数的调用顺序。 【输入输出形式】 参考的输入及输出... -
定义一个Person类,包含姓名(name)、身高(height)、体重(weight),以及speak()方法
2021-05-27 18:49:27定义一个Person类,包含姓名(name)、身高(height)、体重(weight),以及speak()方法,该方法的功能是,输出自己的身高和体重信息。 Person类实现Comparable接口,实现比较两个Person对象的大小,比较规则是:... -
定义一个基类Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及...
2021-05-06 17:39:38定义一个基类Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数,声明一个Box对象,观察构造函数与析构函数的调用顺序。 代码: #include <iostream>... -
OpenCV - C++ - cv::Mat image_height = image.rows - image_weight = image.cols
2020-03-04 10:48:05OpenCV - C++ - cv::Mat image_height = image.rows - image_weight = image.cols 1. image_height = image.rows - image_weight = image.cols //================================================================... -
weight和weightSum的区别
2017-08-22 14:23:08说起weight大家肯定都非常的清楚它的使用,可是weightSum恐怕有很多人不是很清楚,起码今天前我是不知道的,哈哈,来个笔记记录下二者的区别 -
imutils中resize的width和height关系
2020-08-11 00:09:30imutils中resize的width和height关系介绍当参数只有width参数只有height参数有width和height总结 介绍 imutils中resize,我们一般就传入两个参数,一个需要修改大小的图像,一个weight或者是height,是根据的长宽比... -
TabHost和android:layout_height="0.0dip"以及android:layout_weight配合在布局中的使用
2013-12-04 12:22:58最近在搞UI部分,对布局有了一定的认识(仅限于各人的理解...首先对android:layout_height="0.0dip"的属性很是迷惑,在网上找了一大堆的东西,其实也并没有说出来一个所以然来。找了很多文章发现一个规律就是:当andro -
Android:layout_weight详解
2016-06-20 15:37:15控件A最终宽度 = 控件A初始宽度+(屏幕宽度 - 控件宽度和)* 控件A的weight的值 /所有weight之和 下面分析中所提到的 剩余宽度 = 屏幕宽度 - 控件宽度和 举例: 上图的六条,分表对应下面的六段代码,具体... -
关于LayoutParams(int width, int height, float weight) 参数解析
2016-06-14 16:40:15关键问题在第一个参数width=0,一般情况下,设置宽度为0默认为忽略宽度,按照weight来布局。在我同事的开发环境下也确实这样运行的。但在我的开发环境下,0变成了0px。导致Textview不能显示出来。 至于为... -
重新认识font-size、line-height和行高
2017-10-25 22:41:27重新认识font-size、line-height和行高line-height,font-size均是css格式中很常设置的样式,这两个样式会对元素产生什么影响呢?从样式的名字来看,分别代表行高和字体大小的意思,那么line-height真的等于行高吗?... -
关于weight属性使用的一些细节
2015-08-29 18:17:43假设我们要在一个LinearLayout布局中显示两个按钮,button1和button2,button2的宽度是button1的二倍,正常情况下使用weight应该是这样的:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi -
div和span区别/font-weight
2017-08-21 22:25:13①div和span的区别 div{ width: 200px; height: 20px; background: red; margin-bottom: 5px; } span{ width: 200px; -
Android 动态修改layout_weight
2017-11-01 10:34:58这个需求来自于动态向LinearLayout中添加子View,在使用权重的情况下,原先的layout_weight值肯定是要修改的 所以便需要动态修改layout_weight。 原理很简单,就是借助LayoutParams给这个子View设置weight即可... -
python、tkinter Frame height和width参数值有n
2020-12-22 13:08:0599.99%的情况下,这是完全正确的行为,因为它会导致gui响应字体大小、屏幕分辨率和窗口大小的变化。在如果您的目标是将屏幕分成两部分,其中一部分占屏幕的1/4,另一部分占屏幕的3/4,那么最好的解决方案是使用grid... -
width和height对weight的影响
2012-03-26 17:25:39weight是比重,在线性布局中用来分配各组件的空间. ...1.若要A和B严格按照weight比例分配空间,则必须设置两个的height都为0或者都为fillparent. 2.若其中有一个为wrapcontent,且weight设为0,则不管其他组件wei... -
Android系列之一:android:layout_weight和android:weightSum总结
2017-05-06 13:38:12android:layout_weight是LinearLayout布局的一个属性,它定义了每个控件占据屏幕剩余空间的权重。以划分屏幕横向空间为例:每个控件利用android:layout_width来定义自身的宽度,Android首先将所有控件定义的宽度相加... -
布局属性layout_weight解析
2016-07-05 17:59:05关于Layout Weight,我们先来看一下官方释义。 LinearLayout通过子View的android:layout_weight属性,来为子View分配权重。layout_weight属性指定子View在屏幕上占有空间的重要性。较大的权重值使得子View可以扩充... -
pytorch中data shape和卷积层的weight shape
2019-10-15 20:40:09Input: (batch_size, in_channel, width, height) # conv layer class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True) input... -
LinearLayout的layout_weight属性的计算
2018-07-07 20:14:25布局的时候,系统先按照view的layout_width和layout_height来布局,然后再根据layout_weight对view的位置进行调整。举例来说<?xml version="1.0" encoding="utf-8"?> <...