java - Why does Android draw View inside another (with width and height defined in pixels) in incorrect way? -
i wondering in way android draws views on screen inside layout views defined android:layout_width , android:layout_height values in pixels.
i using android class view extends android.view.surfaceview. mjpegview display mjpeg stream camera on android app. working code (with small changes): android ics , mjpeg using asynctask
this mjpegview class placed inside relativelayout. mjpegview object added programmatically , relativelayout has attributes defined in way:
android:layout_width="800px" android:layout_height="600px" this size values necessary application can't change them. application working in landscape orientation. mjpegview need set 320 x 240 pixels size inside relativelayout (800 x 600 px). application enlarges realtivelayout fill screen on current device. , problem connected streaming mjpeg. working on nexus 7 (2012 version), has 1280 x 800 resolution.
to real size of view inside layout have measured height of nexus 7 screen without action , navigation bars. have used method inside 1 activity:
@override public void onwindowfocuschanged(boolean hasfocus) { super.onwindowfocuschanged(hasfocus); relativelayout parentlayout = (relativelayout) findviewbyid(r.id.parent_layout); if (parentlayout != null && islandscape) { this.measuredactivityheight = parentlayout.getbottom(); } } i getting 703 value measuredactivityheight of running activity. ratio in android translates 320 x 240 px inside 800 x 600 px dp value on screen have written method:
public int calculateforcurrentscreen(float pixelvalue) { float ratio = pageactivity.measuredactivityheight / 600f; int result = (int) (pixelvalue * ratio); return result; } so 320 x 240 px on nexus 7 getting 374 x 281. , mjpegview in size need use method inside mjpegview class:
public void setresolution(int width, int height) { holder.setfixedsize(width, height); } and size of streaming view in wanted size. view has additional black areas. in fact whole view (camera stream + black areas on bottom , right side) has 440 x 328 size.
i have checked rect object in mjpegview drawing 374 x 281 size ok. surfaceholder , canvas have size. bitmap object has 320 x 240 size, ok because bitmap object camera stream. , resized wanted size.
problem disappear (there no black areas , mjpegview in correct position , size relative other views) when have switched relativelayout 800dp x 600dp size , invoke setresolution(320, 240) mjpegview object. whole layout in moment big.
edit
thanks @mathiash comment. have uploaded screenshot nexus 7 (2012 edition) show problem.
as can see, green area relativelayout defined 800 x 600 px size. mjpeg stream http://trackfield.webcam.oregonstate.edu/. have used url: http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg/video.cgi?resolution=320x240.
for screenshot have invoked setresolution(320, 240) method. , black area has 374 x 281 , video stream 320 x 240.

for screenshot have invoked setresolution(374, 281) method. , black area bigger , video stream 374 x 281. , in case video stream has correct size black area unnecessary.

as levente kurusa suggested, using virtual pixel unit such dp may solve problem. should avoid using px diretly.
Comments
Post a Comment