Apr 24 | 7:20 PM |
Mark M. | has entered the room |
Apr 24 | 7:25 PM |
Mark M. | turned on guest access |
EGHDK | has entered the room |
Mark M. |
hello, EGHDK!
|
Mark M. |
how can I help you today?
|
EGHDK |
Hello. I was wondering if there was any easy way of finding out the width of a view after I scale it. For example. I Log the getWidth of a view. Then I do view.setScaleX,Y(2) and then log the width again, but its exactly the same.
|
Mark M. |
um, divide by 2?
|
Mark M. |
:-)
|
Mark M. |
or multiply by 2
|
Mark M. |
not sure of your syntax there
|
EGHDK |
Same thing for if I set a But why is my view on the screen clearly doubling in width. but when I request width, it gives me the original width.
|
EGHDK |
But why is my view on the screen clearly doubling in width. but when I request width, it gives me the original width.
|
Mark M. |
because otherwise you get into an infinite scaling loop
|
Mark M. |
now, you could argue that there should be getWidth() and getScaledWidth(), or something like that
|
Mark M. |
in fact, I thought maybe they offered that, but it doesn't look like it
|
Apr 24 | 7:30 PM |
Mark M. |
but there has to be a way for the View itself to know is "true" width
|
EGHDK |
Ugh.
|
Mark M. |
you're not changing the intrinsic size of the View by scaling it
|
Mark M. |
you are changing its *effective* size, for how it is drawn, how it handles touch events, etc.
|
EGHDK |
That doesn't make any sense to me.
|
EGHDK |
I need to dynamically tell what the size is of the view.
|
EGHDK |
so what's my best bet.
|
EGHDK |
Holding onto the scale factor ?
|
Mark M. |
call getScaleX(), call getWidth(), and do the math
|
Mark M. |
likewise for getScaleY() and getHeight()
|
EGHDK |
Okay, so that may seemingly solve the first part of my problem.
|
EGHDK |
The second, is that. I want to get the correct coordinates from getLeft, getTop, getRight, getBottom
|
EGHDK |
but after I scale them, I get the original points.
|
EGHDK |
after I scale the view, I get the original points.
|
Apr 24 | 7:35 PM |
EGHDK |
Let's say view.getBottom gives me 100, but then I scale the view(2) and I getBottom again, it gives me 100.
|
Mark M. |
short of running the calculations yourself, I don't see an easy way to get those values
|
EGHDK |
While the bottom coord has definitely changed
|
EGHDK |
So can I just change the height and width of the view instead of scaling it?
|
Mark M. |
you can change the LayoutParams that govern its intrinsic height and width
|
EGHDK |
instead of doing view.setScaleX(2) can I just do view.setWidth(view.getWidth*2)
|
Mark M. |
and doing that will affect getWidth(), getLeft(), etc.
|
Mark M. |
setScaleX() is designed for animations
|
Mark M. |
short-lived stuff
|
EGHDK |
Will that change the "instrinsic" size?
|
Mark M. |
changing the LayoutParams? yes
|
EGHDK |
Not the layoutParms? just height and width
|
Mark M. |
you change the height and width by changing the LayoutParams that govern the height and width
|
Mark M. |
there is no setHeight() on Vierw
|
Mark M. |
er, View
|
Mark M. |
there is no setWidth() on View
|
EGHDK |
so I have to do view.setLayoutParams();
|
Mark M. |
yes
|
EGHDK |
LayoutParams params = (height, width)? is that a thing?
|
EGHDK |
I've never had to do that programaticallt before I think.
|
Mark M. |
what is the parent container of this view that you are scaling?
|
Apr 24 | 7:40 PM |
EGHDK |
RelativeLayout, but thats just because I don't know what to use.
|
EGHDK |
I think absolute layout would work, but thats depracated.
|
Mark M. |
if you don't know what to use, what you are trying to accomplish?
|
Mark M. |
what is this view that you are scaling?
|
Mark M. |
why are you scaling it?
|
EGHDK |
Okay, so essentially, the app is a blank canvas with one imageview (100 x 100) that starts at 0,0. I want to be able to touch the imageView and drag it around the canvas.
|
EGHDK |
I'm unsure of what to make the root layout. but relativelayout has worked so far.
|
Mark M. |
sounds like that should be done by directly working with the Canvas, rather than mucking around with views
|
Apr 24 | 7:45 PM |
EGHDK |
Hrm.... how would I do that?
|
Mark M. |
well, I don't have any code for that, as working at that level is something I generally don't do much
|
EGHDK |
Gotcha.
|
Mark M. |
but, in essence, what you described is a "fingerpaint" scenario, except you are dragging an image rather than drawing a line
|
Mark M. |
interactive elements like that are usually their own widget, working with the Canvas under the covers
|
Mark M. |
for example, a SeekBar (slider) is not made up of a bunch of widgets -- it *is* a widget
|
Mark M. |
the touch events, the movement of the thumb, and so on are drawn by the widget itself
|
Mark M. |
it's not like that's a RelativeLayout holding onto ImageViews or something
|
EGHDK |
Gotcha.
|
Mark M. |
there are books, blog posts, and the like that get into that sort of stuff
|
EGHDK |
Interesting stuff.
|
Mark M. |
my book doesn't
|
EGHDK |
Thanks.
|
Mark M. |
though I dip my toes in those waters a bit here and there
|
Mark M. |
I haven't had a need to get down-and-dirty at that level, so while I know that's the direction to head in, I can't get you there
|
Apr 24 | 7:50 PM |
Mark M. |
sometime this year(?), I hope to add a signature capture sample to the book and use that to get more into this level of stuff
|
Mark M. |
now, you *can* accomplish what you want using views the way you are
|
Mark M. |
but you'll find it to be a bit clunky
|
Mark M. |
and keeping the frame rate up (i.e., no jank) may get interesting in spots
|
EGHDK |
So I'm trying the layoutParams route of resizing instead of scaling.
|
EGHDK |
View paste
|
Mark M. |
um, well, you have no choice but to lose precision
|
Mark M. |
I mean, you can't force Android to use floats just because
|
Mark M. |
sizes are integers
|
Mark M. |
there are some rounding methods somewhere (Math?)
|
Mark M. |
yeah, Math.round()
|
Mark M. |
I don't know if casting a float to an int rounds or not -- you'd have to look around for that
|
Mark M. |
but round() should work fine
|
EGHDK |
Yup. Round. Cool. No need to typecast afterwards either.
|
EGHDK |
Thanks
|
EGHDK |
I think the whole size instead of scale will solve my problems.
|
EGHDK |
because I'm polling for getLeft and getX and it gets thrown off because of scale.
|
Apr 24 | 8:10 PM |
EGHDK |
Do you know of any definitive lifecycle chart for android? for activities and fragments? I'm finding it increasingly difficult to understand how and when to do stuff in fragments. it feels like learning a brand new whacked out version of an activity lifecycle.
|
Mark M. | |
EGHDK |
So does oncreate and onStart and all of the matching activity lifecycle commands happen at the same time?
|
Mark M. |
the activity's onStart() calls onStart() of its fragments
|
Mark M. |
and so on
|
EGHDK |
So if my activity has three fragments, and the activity calls onStart, then it wont move on until onStart of the three fragments is called?>
|
Apr 24 | 8:15 PM |
Mark M. |
that's my understanding
|
Mark M. |
that's easier to see in FragmentActivity from the support package: https://android.googlesource.com/platform/frame...
|
Mark M. |
(starting line 1034)
|
Mark M. |
whoops, sorry, starting line 552
|
Apr 24 | 8:20 PM |
EGHDK |
Gotcha. Alright. Thats it for me. Thanks again. as always.
|
Mark M. |
you are very welcome
|
Apr 24 | 8:30 PM |
Mark M. |
that's a wrap for today's chat
|
Apr 24 | 8:30 PM |
Mark M. |
the transcript will be posted to http://commonsware.com/office-hours/ shortly
|
Mark M. |
have a pleasant day!
|
EGHDK | has left the room |
Mark M. | turned off guest access |