I have implemented everything I can find here on StackOverflow. Here is my code:
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>
The statusDialog.show function works fine and shows the dialog that it should. I am trying to paint a bar to the screen that fills content horizontally in the layout I'm specifying here, after the layout is drawn to the screen. If I hard-code the layout height to [N]dp, I get a pixel count back, but otherwise everything just returns -1 and I'm not able to dynamically fill the last commented out line with canvas.drawRect(0, 0, barWidth, barHeight, paint);
.
I don't know what I'm doing wrong here. HALP.
Not entirely sure of the real answer here, but I figured out that my problem was completely irrelevant and caused by my own ignorance/expected behavior.
The 480x800 (or whatever size) bitmap is automatically scaled into the FILL_PARENT View I was adding it to. So essentially I wasted 2 days on a problem that didn't matter, as I was trying to calculate the width of a progress bar wrapper. tl;dr, only getting return integers of -1 on measured views that fill or match parent seems to be normal.
themoreyouknow.gif
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.