How do I move a custom view through the whole layout of android app?
I'm trying to move a custom view (a small circle) over the whole application layout by using proportional x and y values. Let's say for example, that I want to move the circle to exactly the middle of the screen, so I'll send (0.5f, 0.5f) and then multiply each value by my current screen width and height respectively. But for some reason the circle ends up somewhere else, like (0.5, 0.5) ending up at the bottom right of the screen, just as if I where using wrong and larger resolution values, which I'm not, since I've even hardcoded my device resolution to make sure that's not the problem.
Here is my custom view current implementation:
class CirclePointer(context: Context, attrs: AttributeSet) :View(context, attrs){
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas.apply {
this!!.drawCircle(x, y, 5f, mCirclePointerPaint)
}
}
private val mCirclePointerPaint = Paint().apply {
}
fun move(newX: Float, newY: Float){
x = newX * 1280// hardcoded width
y = newY * 720// hardcoded height
invalidate()
requestLayout()
}// end of move
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
}
}
And my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/heading"
android:orientation="vertical">
<my.package.annotation_views.CirclePointer
android:id="@+id/circlePointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I've just leant how to implement my own custom View so I'm pretty sure I'm missing something. So any help would be appreciated.
What I've tried
- Setting my CirclePointer width and height to match_parent instead of wrap_content
Observations
I'm using Android Studio 3.2 and macOS Mojave
Thanks in advance.
add a comment |
I'm trying to move a custom view (a small circle) over the whole application layout by using proportional x and y values. Let's say for example, that I want to move the circle to exactly the middle of the screen, so I'll send (0.5f, 0.5f) and then multiply each value by my current screen width and height respectively. But for some reason the circle ends up somewhere else, like (0.5, 0.5) ending up at the bottom right of the screen, just as if I where using wrong and larger resolution values, which I'm not, since I've even hardcoded my device resolution to make sure that's not the problem.
Here is my custom view current implementation:
class CirclePointer(context: Context, attrs: AttributeSet) :View(context, attrs){
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas.apply {
this!!.drawCircle(x, y, 5f, mCirclePointerPaint)
}
}
private val mCirclePointerPaint = Paint().apply {
}
fun move(newX: Float, newY: Float){
x = newX * 1280// hardcoded width
y = newY * 720// hardcoded height
invalidate()
requestLayout()
}// end of move
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
}
}
And my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/heading"
android:orientation="vertical">
<my.package.annotation_views.CirclePointer
android:id="@+id/circlePointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I've just leant how to implement my own custom View so I'm pretty sure I'm missing something. So any help would be appreciated.
What I've tried
- Setting my CirclePointer width and height to match_parent instead of wrap_content
Observations
I'm using Android Studio 3.2 and macOS Mojave
Thanks in advance.
Well, it's not exactly a solution but for some unknown reason if I use half the actual width and height the pointer movement is reproduced at the exact position it should...
– Jairo Lozano
Nov 27 '18 at 19:15
add a comment |
I'm trying to move a custom view (a small circle) over the whole application layout by using proportional x and y values. Let's say for example, that I want to move the circle to exactly the middle of the screen, so I'll send (0.5f, 0.5f) and then multiply each value by my current screen width and height respectively. But for some reason the circle ends up somewhere else, like (0.5, 0.5) ending up at the bottom right of the screen, just as if I where using wrong and larger resolution values, which I'm not, since I've even hardcoded my device resolution to make sure that's not the problem.
Here is my custom view current implementation:
class CirclePointer(context: Context, attrs: AttributeSet) :View(context, attrs){
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas.apply {
this!!.drawCircle(x, y, 5f, mCirclePointerPaint)
}
}
private val mCirclePointerPaint = Paint().apply {
}
fun move(newX: Float, newY: Float){
x = newX * 1280// hardcoded width
y = newY * 720// hardcoded height
invalidate()
requestLayout()
}// end of move
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
}
}
And my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/heading"
android:orientation="vertical">
<my.package.annotation_views.CirclePointer
android:id="@+id/circlePointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I've just leant how to implement my own custom View so I'm pretty sure I'm missing something. So any help would be appreciated.
What I've tried
- Setting my CirclePointer width and height to match_parent instead of wrap_content
Observations
I'm using Android Studio 3.2 and macOS Mojave
Thanks in advance.
I'm trying to move a custom view (a small circle) over the whole application layout by using proportional x and y values. Let's say for example, that I want to move the circle to exactly the middle of the screen, so I'll send (0.5f, 0.5f) and then multiply each value by my current screen width and height respectively. But for some reason the circle ends up somewhere else, like (0.5, 0.5) ending up at the bottom right of the screen, just as if I where using wrong and larger resolution values, which I'm not, since I've even hardcoded my device resolution to make sure that's not the problem.
Here is my custom view current implementation:
class CirclePointer(context: Context, attrs: AttributeSet) :View(context, attrs){
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas.apply {
this!!.drawCircle(x, y, 5f, mCirclePointerPaint)
}
}
private val mCirclePointerPaint = Paint().apply {
}
fun move(newX: Float, newY: Float){
x = newX * 1280// hardcoded width
y = newY * 720// hardcoded height
invalidate()
requestLayout()
}// end of move
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
}
}
And my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/heading"
android:orientation="vertical">
<my.package.annotation_views.CirclePointer
android:id="@+id/circlePointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I've just leant how to implement my own custom View so I'm pretty sure I'm missing something. So any help would be appreciated.
What I've tried
- Setting my CirclePointer width and height to match_parent instead of wrap_content
Observations
I'm using Android Studio 3.2 and macOS Mojave
Thanks in advance.
asked Nov 27 '18 at 1:37
Jairo LozanoJairo Lozano
398210
398210
Well, it's not exactly a solution but for some unknown reason if I use half the actual width and height the pointer movement is reproduced at the exact position it should...
– Jairo Lozano
Nov 27 '18 at 19:15
add a comment |
Well, it's not exactly a solution but for some unknown reason if I use half the actual width and height the pointer movement is reproduced at the exact position it should...
– Jairo Lozano
Nov 27 '18 at 19:15
Well, it's not exactly a solution but for some unknown reason if I use half the actual width and height the pointer movement is reproduced at the exact position it should...
– Jairo Lozano
Nov 27 '18 at 19:15
Well, it's not exactly a solution but for some unknown reason if I use half the actual width and height the pointer movement is reproduced at the exact position it should...
– Jairo Lozano
Nov 27 '18 at 19:15
add a comment |
1 Answer
1
active
oldest
votes
making the x and y of the circle in the half of your screen width and height respectively will not work because your circle's x and y are the top left of your circle not the center of the circle. The solution is to subtract half of the width from the x, and half of height from y such as:
int left = screenWidth / 2 - circleWidth / 2
int top = screenHeight / 2 - circleHeight / 2
yourCircle.setX(left);
yourCircle.setY(top);
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491537%2fhow-do-i-move-a-custom-view-through-the-whole-layout-of-android-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
making the x and y of the circle in the half of your screen width and height respectively will not work because your circle's x and y are the top left of your circle not the center of the circle. The solution is to subtract half of the width from the x, and half of height from y such as:
int left = screenWidth / 2 - circleWidth / 2
int top = screenHeight / 2 - circleHeight / 2
yourCircle.setX(left);
yourCircle.setY(top);
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
add a comment |
making the x and y of the circle in the half of your screen width and height respectively will not work because your circle's x and y are the top left of your circle not the center of the circle. The solution is to subtract half of the width from the x, and half of height from y such as:
int left = screenWidth / 2 - circleWidth / 2
int top = screenHeight / 2 - circleHeight / 2
yourCircle.setX(left);
yourCircle.setY(top);
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
add a comment |
making the x and y of the circle in the half of your screen width and height respectively will not work because your circle's x and y are the top left of your circle not the center of the circle. The solution is to subtract half of the width from the x, and half of height from y such as:
int left = screenWidth / 2 - circleWidth / 2
int top = screenHeight / 2 - circleHeight / 2
yourCircle.setX(left);
yourCircle.setY(top);
making the x and y of the circle in the half of your screen width and height respectively will not work because your circle's x and y are the top left of your circle not the center of the circle. The solution is to subtract half of the width from the x, and half of height from y such as:
int left = screenWidth / 2 - circleWidth / 2
int top = screenHeight / 2 - circleHeight / 2
yourCircle.setX(left);
yourCircle.setY(top);
answered Nov 27 '18 at 2:12
Khalid TahaKhalid Taha
1,89411531
1,89411531
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
add a comment |
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
Thanks, I understand that and will definitely update my code to subtract half the circle radius. But my offset is far greater than that, I mean, my circle has a radius of 5px and when positioned half of the screen it ends up even outside the drawing area. I think I'll start playing with the hardcoded width and height to see if I can find any correlations on the offset...
– Jairo Lozano
Nov 27 '18 at 3:58
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
I will accept this as a correct answer since it's the right way to position the circle at the exact desired coordinates. Thanks for your time @Khalid Taha
– Jairo Lozano
Dec 28 '18 at 20:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491537%2fhow-do-i-move-a-custom-view-through-the-whole-layout-of-android-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well, it's not exactly a solution but for some unknown reason if I use half the actual width and height the pointer movement is reproduced at the exact position it should...
– Jairo Lozano
Nov 27 '18 at 19:15