How to fix starting position of view to drag
When I hold down on my ImageView to drag it across the screen, the ImageView becomes invisible, and the draggable version of the ImageView will spawn onto the screen with the center of the spawned view underneath my fingertip; this is not organic. I want the draggable version of the view to spawn directly on top of the ImageView, and not directly underneath my finger tip.
For example, If my ImageView is a Ace of Diamonds on top of a card deck, and I want to drag the card off the deck, the card should drag smoothly off the deck, and not reposition itself underneath my finger and then drag, if that makes sense.
This my code for the draggable ImageView; I'm not sure how to make the dragging animation smoother as described above.
public class DragView2 extends AppCompatImageView
implements View.OnDragListener, View.OnTouchListener{
private static final String TAG = DragView2.class.getSimpleName();
public DragView2(Context context)
{
super(context, null);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
public DragView2(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case ACTION_DOWN:
Log.d(TAG, "ACTION_DOWN");
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
this.setVisibility(View.INVISIBLE);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
return false;
}
@Override
public boolean onDrag(View view, DragEvent event)
{
final View viewToDrag = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG,"ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG,"ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG,"ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DROP:
Log.d(TAG,"ACTION_DROP");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG,"ACTION_DRAG_ENDED");
view.post(new Runnable() {
public void run() {
viewToDrag.setVisibility(View.VISIBLE);
}
});
break;
default:
Log.d(TAG,"default");
break;
}
return true;
}
}
add a comment |
When I hold down on my ImageView to drag it across the screen, the ImageView becomes invisible, and the draggable version of the ImageView will spawn onto the screen with the center of the spawned view underneath my fingertip; this is not organic. I want the draggable version of the view to spawn directly on top of the ImageView, and not directly underneath my finger tip.
For example, If my ImageView is a Ace of Diamonds on top of a card deck, and I want to drag the card off the deck, the card should drag smoothly off the deck, and not reposition itself underneath my finger and then drag, if that makes sense.
This my code for the draggable ImageView; I'm not sure how to make the dragging animation smoother as described above.
public class DragView2 extends AppCompatImageView
implements View.OnDragListener, View.OnTouchListener{
private static final String TAG = DragView2.class.getSimpleName();
public DragView2(Context context)
{
super(context, null);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
public DragView2(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case ACTION_DOWN:
Log.d(TAG, "ACTION_DOWN");
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
this.setVisibility(View.INVISIBLE);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
return false;
}
@Override
public boolean onDrag(View view, DragEvent event)
{
final View viewToDrag = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG,"ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG,"ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG,"ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DROP:
Log.d(TAG,"ACTION_DROP");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG,"ACTION_DRAG_ENDED");
view.post(new Runnable() {
public void run() {
viewToDrag.setVisibility(View.VISIBLE);
}
});
break;
default:
Log.d(TAG,"default");
break;
}
return true;
}
}
add a comment |
When I hold down on my ImageView to drag it across the screen, the ImageView becomes invisible, and the draggable version of the ImageView will spawn onto the screen with the center of the spawned view underneath my fingertip; this is not organic. I want the draggable version of the view to spawn directly on top of the ImageView, and not directly underneath my finger tip.
For example, If my ImageView is a Ace of Diamonds on top of a card deck, and I want to drag the card off the deck, the card should drag smoothly off the deck, and not reposition itself underneath my finger and then drag, if that makes sense.
This my code for the draggable ImageView; I'm not sure how to make the dragging animation smoother as described above.
public class DragView2 extends AppCompatImageView
implements View.OnDragListener, View.OnTouchListener{
private static final String TAG = DragView2.class.getSimpleName();
public DragView2(Context context)
{
super(context, null);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
public DragView2(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case ACTION_DOWN:
Log.d(TAG, "ACTION_DOWN");
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
this.setVisibility(View.INVISIBLE);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
return false;
}
@Override
public boolean onDrag(View view, DragEvent event)
{
final View viewToDrag = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG,"ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG,"ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG,"ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DROP:
Log.d(TAG,"ACTION_DROP");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG,"ACTION_DRAG_ENDED");
view.post(new Runnable() {
public void run() {
viewToDrag.setVisibility(View.VISIBLE);
}
});
break;
default:
Log.d(TAG,"default");
break;
}
return true;
}
}
When I hold down on my ImageView to drag it across the screen, the ImageView becomes invisible, and the draggable version of the ImageView will spawn onto the screen with the center of the spawned view underneath my fingertip; this is not organic. I want the draggable version of the view to spawn directly on top of the ImageView, and not directly underneath my finger tip.
For example, If my ImageView is a Ace of Diamonds on top of a card deck, and I want to drag the card off the deck, the card should drag smoothly off the deck, and not reposition itself underneath my finger and then drag, if that makes sense.
This my code for the draggable ImageView; I'm not sure how to make the dragging animation smoother as described above.
public class DragView2 extends AppCompatImageView
implements View.OnDragListener, View.OnTouchListener{
private static final String TAG = DragView2.class.getSimpleName();
public DragView2(Context context)
{
super(context, null);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
public DragView2(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
this.setOnTouchListener(this);
this.setOnDragListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case ACTION_DOWN:
Log.d(TAG, "ACTION_DOWN");
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
this.setVisibility(View.INVISIBLE);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
return false;
}
@Override
public boolean onDrag(View view, DragEvent event)
{
final View viewToDrag = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG,"ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG,"ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG,"ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DROP:
Log.d(TAG,"ACTION_DROP");
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG,"ACTION_DRAG_ENDED");
view.post(new Runnable() {
public void run() {
viewToDrag.setVisibility(View.VISIBLE);
}
});
break;
default:
Log.d(TAG,"default");
break;
}
return true;
}
}
asked Nov 22 '18 at 23:48
the_prolethe_prole
2,98163380
2,98163380
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A pointer towards the solution.
I extended the View class. Use the following code to successfully drag the object at the right coords.
@Override
public boolean onTouchEvent(MotionEvent event)
{
float oldX, oldY, newX, newY, originalX, originalY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
originalX = oldX;
originalY = oldY;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
updatePositions(oldX, oldY, newX, newY);
oldX = newX;
oldY = newY;
invalidate();
break;
case MotionEvent.ACTION_UP:
newX = event.getX();
newY = event.getY();
// USE ANY OF THE FOLLOWING
// updatePositions(oldX, oldY, newX, newY);
// updatePositions(originalX, originalY, newX, newY);
oldX = newX;
oldY = newY;
}
}
private void updatePositions(float pt1, float pt2, float pt3, float pt4) {
// code to change the position of the position
}
Make the 6 variables class variables and use Toast to show positions. Hope this hints at solution :)
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%2f53439178%2fhow-to-fix-starting-position-of-view-to-drag%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
A pointer towards the solution.
I extended the View class. Use the following code to successfully drag the object at the right coords.
@Override
public boolean onTouchEvent(MotionEvent event)
{
float oldX, oldY, newX, newY, originalX, originalY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
originalX = oldX;
originalY = oldY;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
updatePositions(oldX, oldY, newX, newY);
oldX = newX;
oldY = newY;
invalidate();
break;
case MotionEvent.ACTION_UP:
newX = event.getX();
newY = event.getY();
// USE ANY OF THE FOLLOWING
// updatePositions(oldX, oldY, newX, newY);
// updatePositions(originalX, originalY, newX, newY);
oldX = newX;
oldY = newY;
}
}
private void updatePositions(float pt1, float pt2, float pt3, float pt4) {
// code to change the position of the position
}
Make the 6 variables class variables and use Toast to show positions. Hope this hints at solution :)
add a comment |
A pointer towards the solution.
I extended the View class. Use the following code to successfully drag the object at the right coords.
@Override
public boolean onTouchEvent(MotionEvent event)
{
float oldX, oldY, newX, newY, originalX, originalY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
originalX = oldX;
originalY = oldY;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
updatePositions(oldX, oldY, newX, newY);
oldX = newX;
oldY = newY;
invalidate();
break;
case MotionEvent.ACTION_UP:
newX = event.getX();
newY = event.getY();
// USE ANY OF THE FOLLOWING
// updatePositions(oldX, oldY, newX, newY);
// updatePositions(originalX, originalY, newX, newY);
oldX = newX;
oldY = newY;
}
}
private void updatePositions(float pt1, float pt2, float pt3, float pt4) {
// code to change the position of the position
}
Make the 6 variables class variables and use Toast to show positions. Hope this hints at solution :)
add a comment |
A pointer towards the solution.
I extended the View class. Use the following code to successfully drag the object at the right coords.
@Override
public boolean onTouchEvent(MotionEvent event)
{
float oldX, oldY, newX, newY, originalX, originalY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
originalX = oldX;
originalY = oldY;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
updatePositions(oldX, oldY, newX, newY);
oldX = newX;
oldY = newY;
invalidate();
break;
case MotionEvent.ACTION_UP:
newX = event.getX();
newY = event.getY();
// USE ANY OF THE FOLLOWING
// updatePositions(oldX, oldY, newX, newY);
// updatePositions(originalX, originalY, newX, newY);
oldX = newX;
oldY = newY;
}
}
private void updatePositions(float pt1, float pt2, float pt3, float pt4) {
// code to change the position of the position
}
Make the 6 variables class variables and use Toast to show positions. Hope this hints at solution :)
A pointer towards the solution.
I extended the View class. Use the following code to successfully drag the object at the right coords.
@Override
public boolean onTouchEvent(MotionEvent event)
{
float oldX, oldY, newX, newY, originalX, originalY;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
oldX = event.getX();
oldY = event.getY();
originalX = oldX;
originalY = oldY;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
updatePositions(oldX, oldY, newX, newY);
oldX = newX;
oldY = newY;
invalidate();
break;
case MotionEvent.ACTION_UP:
newX = event.getX();
newY = event.getY();
// USE ANY OF THE FOLLOWING
// updatePositions(oldX, oldY, newX, newY);
// updatePositions(originalX, originalY, newX, newY);
oldX = newX;
oldY = newY;
}
}
private void updatePositions(float pt1, float pt2, float pt3, float pt4) {
// code to change the position of the position
}
Make the 6 variables class variables and use Toast to show positions. Hope this hints at solution :)
answered Nov 23 '18 at 6:14
Muzammil SaeedMuzammil Saeed
145
145
add a comment |
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%2f53439178%2fhow-to-fix-starting-position-of-view-to-drag%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