我已经在StackOverflow上实现了我能找到的所有内容。这是我的代码:
MainActivity:
statusDialog.getWindow().setAttributes(lp);
statusDialog.show();
final LinearLayout drawbar = (LinearLayout) statusDialog.findViewById(R.id.drawbar);
int barWidth = drawbar.getWidth();
int barHeight = drawbar.getHeight();
DrawBar(drawbar);
}
public void DrawBar(View v){
LinearLayout statusDialog = (LinearLayout) v;
Paint paint = new Paint();
paint.setColor(Color.parseColor("#CD5C5C"));
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
View bar = statusDialog.findViewById(R.id.drawbar);
LinearLayout test = (LinearLayout) statusDialog.findViewById(R.id.drawbar);
int barWidth2 = statusDialog.getLayoutParams().width;
int barHeight2 = statusDialog.getLayoutParams().height;
int barWidth3 = v.getLayoutParams().width;
int barHeight3 = v.getLayoutParams().height;
int barWidth4 = bar.getLayoutParams().width;
int barHeight4 = bar.getLayoutParams().height;
int barWidth5 = v.getMeasuredWidth();
int barHeight5 = v.getMeasuredHeight();
int barWidth6 = statusDialog.getWidth();
int barHeight6 = statusDialog.getHeight();
test.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
int barheight7 = test.getMeasuredHeight();
int barwidth7 = test.getMeasuredWidth();
canvas.drawRect(0, 0, 50, 50, paint);
//canvas.drawRect(0, 0, barWidth, barHeight, paint);
statusDialog.setBackgroundDrawable(new BitmapDrawable(this.getResources(), bg));
}
drawbar.xml:
<LinearLayout
android:id="@+id/drawbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
statusDialog.show函数正常工作并显示它应该的对话框。我正在尝试在布局绘制到屏幕后,在我在此指定的布局中水平填充内容的屏幕上绘制一个条形图。如果我将布局高度硬编码为[N] dp,我会得到一个像素计数,但是否则所有内容都返回-1并且我无法使用canvas.drawRect(0, 0, barWidth, barHeight, paint);
动态填充最后一个注释掉的行。< / p>
我不知道我在这里做错了什么。 HALP。
答案 0 :(得分:0)
这里并不完全确定真正的答案,但我发现我的问题与我自己的无知/预期行为完全无关。
480x800(或任何大小)位图会自动缩放到我添加到的FILL_PARENT视图中。所以基本上我浪费了2天的时间来处理一个无关紧要的问题,因为我正在尝试计算进度条包装器的宽度。 tl; dr,仅在填充或匹配父级的测量视图上获得-1的返回整数似乎是正常的。
themoreyouknow.gif