How to decrease sensitivity of onTouch? - android

How to decrease sensitivity of onTouch ? I have a custom remote with a virtual touch pad on it. The problem is that when I move finger over the touch pad the mouse cursor moves very fast. So is there a way to decrease that movement speed ?
I have googled the problem and in some articles there is written that it is possible only if the phone is rooted.
public void onTouch(View v, MotionEvent event) {
KeyImageView mKeyImageView= (KeyImageView)v;
int width = mKeyImageView.getWidth();
int height = mKeyImageView.getHeight();
Log.d(TAG,"onTouch:"+width+" "+height+" "+RemoteIME.mService.getWidth()+" "+RemoteIME.mService.getHeigt());
int touch_x = switchTouchXY((int)event.getX(), width, RemoteIME.mService.getWidth());
int touch_y = switchTouchXY((int)event.getY(), height, RemoteIME.mService.getHeigt());
Touch mTouch = new Touch(RemoteIME.mService, event.getAction(), touch_x, touch_y);
sendCmdMessage(SendThread.CMD_SEND_TOUCH, 0, 0, mTouch);
}
public int switchTouchXY(int touch, int source, int target) {
return (touch * target / source);
}

Related

Java syntax in android studio

What I want to do is to click a space on the screen, get the coordinates and paint the image here. I did this using paintIcon and mouseListener in eclipse but how to do the same thing in android studio? Thanks!
public static Game game = new Game();
public Control(){
this.setLayout(null);
this.setBounds(0, 0, 780, 780);
addMouseListener(this);
}
public void paint(Graphics g){
setBackground(Color.GREEN);
board.paintIcon(this, g, 0, 0);
for(int i = 0; i < 19; i++){
for(int j = 0; j < 19; j++){
if(game.gameBoard.board[i][j] != 'E'){
if(game.gameBoard.board[i][j] == 'B'){
black.paintIcon(this, g, j * 40, i * 40);
}
if(game.gameBoard.board[i][j] == 'W'){
white.paintIcon(this, g, j * 40, i * 40);
}
}
}
}
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
mouseX = e.getX();
mouseY = e.getY();
int targetX = mouseX / 40;
int targetY = mouseY / 40;
game.move(targetX, targetY);
repaint();
}
In Android you use a different system than in "Java"(you know Android is Java), for instance as is clear from your code you do not have a Mouse but a Touch event.
The first thing you should do is to study this official link that explains the concept of Canvas. Then you will learn how to integrate the "tap" into your app that is instead this official link
This is an excellent post to learn ho to do it.
You begin extending the View and overriding the method onDraw(Canvas canvas) then you need to override a listener that say you when the user clicked the screen onTouchEvent(MotionEvent event) usually there are three motion events you want to cover MotionEvent ACTION_DOWN(you tap and the screen recognizes the X,Y coordinates, that is the one you need),ACTION_MOVE(if you drag your finger maintaining pressure on the screen) and ACTION_UP(when you release your finger)
Just remember you need to call invalidate() if you have an animation or something changes on the screen. Basically is a "forced" call to the "onDraw" method. On the basis of the three links I sent you can cover all thebasis of the 2d graphics in Android that is a bit different by Java because of the different features but also the Android dependency on the specific SDK classes
what I want to do is to click a space on the screen, get the coordinates and paint the image here
Here is an example of how you can obtain that in Android instead of "just" Java, please see the notes comments I did below with the double slashes //
public class YourBoard extends View {//EXTENDS VIEW this is important because you override methods
Drawable editIcon = getResources().getDrawable(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
float xPos = 0;
float yPos = 0;
public YourBoard (Context context) {//HERE THE CONSTRUCTOR YOU CAN INITIALIZE THINGS HERE
super (context);
}
#Override
protected void onDraw (Canvas canvas) {//This was your paint(Graphics g)
super.onDraw(canvas);
canvas.save();
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.translate(xPos, yPos);
editIcon.draw(canvas);
canvas.restore();
}
#Override
public boolean onTouchEvent (MotionEvent event) {//THIS WAS YOUR MOUSE LISTENER
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
xPos = event.getX();
yPos = event.getY();
invalidate();//NOTICE THE INVALIDATE I MENTIONED
break;
}
return true;
}
}
}

Get the real X/Y position of an onTouch Event

I know there are some similar topics that have been posted, but I couldn’t find a good solution for my problem.
I have a GridView which is filed with a custom ImageAdapter. Everything works fine, but whenever I click on an image contained in the GridView, I would like to move another Imageview at the click's position.
However, the coordinates of the Event, that I take with event.getX() and event.getY(), don’t correspond to the click’s position.
I first thought of a problem of dp/px conversion, and I tried several solutions in this way but none of them worked.
Then I tried to use the getXPrecision(), but I couldn’t make a working solution…
Maybe there is another way?
I would like to make the correct position programmatically, without adding constants int, so my project will work on various phone and tablet, with different dp and resolutions.
EDIT : Here is a screenshot, where i clicked the 3rd cell of the first line, and setted the position of the pencil with getRawX() - getRawY(). As we can see, this is not the correct position, I want the red dot (imageview's center) to be positionned where i clicked.
The code used :
//getting the position of the onTouch event :
GridView centre = (GridView) findViewById(R.id.gridView);
adapter = new ImageAdapter(this, (dim * dim), tailleCell);
centre.setAdapter(adapter);
centre.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
int X = (int)event.getRawX();
int Y = (int)event.getRawY();
animation(X, Y, etat);
return false;
}
});`
//launching the animation (and setting position of the pencil) :
private void animation(int posX, int posY, int etat)
{
final ImageView img;
if(etat == 0)
{
img = (ImageView) findViewById(R.id.imageView);
}
else
{
img = (ImageView) findViewById(R.id.imageView2);
}
img.clearAnimation();
img.setVisibility(View.VISIBLE);
img.setX(posX);
img.setY(posY);
[...]
}
EDIT 2 : ~Solution :
Jesus Molina Rodríguez De Vera's solution wasn't working as expected, but i managed to make a workable solution. I just changed my code in the Event Handler to adjust the image's position :
int[] offset = new int[2];
centre.getLocationOnScreen(offset);
int Xoffset=offset[0];
int Yoffset = offset[1];
int X = (int)event.getRawX();
int Y = (int)event.getRawY();
animation(X-((int)Math.round(Xoffset/1.15)), Y-((int)Math.round(Yoffset/1.5)), etat);
Sorry for my bad English :)
Thanks for your help!
Try using getRawX() and getRawY() instead of getX and getY.
Edit
I think that i have found the problem.
You are obtaining X and Y relative to the GridView top-left corner, not to the absolute screen coordinetes.
What you can do is the following:
int[] offset = new int[2];
center.getLocationOnScreen(offset);
int Xoffset=offset[0];
int Yoffset = offset[1];
private void animation(int posX, int posY, int etat){
//...
img.setX(posX+Xoffset);
img.setY(posY+Yoffset);
[...]
}
This is supposed to set the the top-left corner of the ImageView in the selected point. In order to set the center in that point:
int ivWidth = img.getWidth();
int ivHeight = img.getHeight();
private void animation(int posX, int posY, int etat){
//...
int[] finalPosition=new int[2];
finalPosition[0] = posX+Xoffset-(ivWidth/2);
finalPosition[1] = posY+Yoffset-(ivHeight/2);
img.setX(finalPosition[0]);
img.setY(finalPosition[1]);
[...]
}
I haven't try it but it should work.
Edit 2
Xoffset and Yoffset are only needed if you use getX()/getY() instead of getRawX()/getRawY()

LibGDX - only draggable movement

:D
I'm currently creating my first ever LibGDX game for Android.
And I've run into a issue regarding the movement of my little player(which is a bucket ;)).
I want to make the bucket(player) only draggable on the x-axis, and not clickable to change position.
The y-axis won't be an issue, because the bucket will only be able to change position on the x-axis.
So I basicly want to make the bucket only draggable.
Sorry for my writing, English isn't my native language.
Here's a code snippet from my code, where the movement operates:
if (Gdx.input.isTouched()){
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
//Check the values
float touchPosX = Gdx.input.getX();
float touchPosY = Gdx.input.getY();
String touchXString = Float.toString(touchPosX);
String touchYString = Float.toString(touchPosY);
Gdx.app.log("Movement - X", touchXString);
//Gdx.app.log("Movement - Y", touchYString);
Gdx.app.log("Movement - BucketX", bucketXString);
//Gdx.app.log("Movement - BucketY", bucketYString)
camera.unproject(touchPos);
bucket.x = touchPos.x - 64 / 2;;}
The code is a bit messy...
If I understand correctly, what you want is that the bucket only moves when you are dragging it, and right now what it does is move to whatever "x" you clicked/taped, even if that "x" is far from the bucket, thus "teleporting" it.
To do this, first have a boolean lets say, "btouched".
boolean btouched = false;
Then you will have to use an Input Processor (instead of InputPolling). to check in the touchdown event if the click/tap was inside the bucket, then only if it was, it will move in the drag event:
MyInputProcessor inputProcessor;
public class MyInputProcessor implements InputProcessor{
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button){
camera.unproject(touchPos.set(Gdx.input.getX(pointer), Gdx.input.getY(pointer), 0));
if(bucket.contains(touchPos)){
btouched = true;
}else{
btouched = false;
}
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer){
camera.unproject(touchPos.set(Gdx.input.getX(pointer), Gdx.input.getY(pointer), 0));
if(btouched){
bucket.x = touchPos.x - 64 / 2;
}
return false;
}
...
}
Dont forget to set the input processor:
inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);

Android TouchEvent Modify

I am working with the ViewPager trying to accomplish some animations. One of them is trying to slide from left to right (default transition of view pager is from right to left). I have done that.
My problem is that I want to "hack" the touch event so I don't need to modify the view pager. For example, for the left to right transition, I will want to make some kind of mirroring with the X in the touch event passed to the view pager.
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
event.setX(Math.abs(event.getX() - getWidth());
return super.onInterceptTouchEvent(event);
}
I finally did it.
public boolean dispatchTouchEvent(MotionEvent event) {
MotionEvent hackedEvent = MotionEvent.obtain(event.getDownTime(),
event.getEventTime(), event.getAction(), (event.getX() - getWidth()) * -1,
event.getY(), event.getMetaState());
boolean result = super.dispatchTouchEvent(hackedEvent);
hackedEvent.recycle();
return result;
};
For people with the same issue and using Kotlin:
I created an extension function to copy the whole MotionEvent in data class style, which allows you to modify certain attributes of it.
E.g.
val modifiedEvent = event.copy(downTime = System.currentTimeMillis())
event.recycle()
Extension function:
/**
* Copies a whole MotionEvent. Use the named parameters to modify certain
values.
* Don't forget to recycle the original event (if it is not used anymore :)
)!
*/
fun MotionEvent.copy(
downTime: Long = getDownTime(),
eventTime: Long = getEventTime(),
action: Int = getAction(),
pointerCount: Int = getPointerCount(),
pointerProperties: Array<MotionEvent.PointerProperties>? =
(0 until getPointerCount())
.map { index ->
MotionEvent.PointerProperties().also { pointerProperties ->
getPointerProperties(index, pointerProperties)
}
}
.toTypedArray(),
pointerCoords: Array<MotionEvent.PointerCoords>? =
(0 until getPointerCount())
.map { index ->
MotionEvent.PointerCoords().also { pointerCoords ->
getPointerCoords(index, pointerCoords)
}
}
.toTypedArray(),
metaState: Int = getMetaState(),
buttonState: Int = getButtonState(),
xPrecision: Float = getXPrecision(),
yPrecision: Float = getYPrecision(),
deviceId: Int = getDeviceId(),
edgeFlags: Int = getEdgeFlags(),
source: Int = getSource(),
flags: Int = getFlags()
): MotionEvent =
MotionEvent.obtain(
downTime,
eventTime,
action,
pointerCount,
pointerProperties,
pointerCoords,
metaState,
buttonState,
xPrecision,
yPrecision,
deviceId,
edgeFlags,
source,
flags
)
Source: https://gist.github.com/sebschaef/b803da53217c88e8c691aeed08602193

How to test for rotation amount before coming to a stop?

I've got a sprite that rotates and when touch input is released, it rotates back to 0 degrees quickly. How can I get the rotation (degrees or otherwise) of the sprite just before the touch input is released?
I've looked and can't find any way to achieve, tough question to google.
EDIT Sorry for the latent response. Here is my code so far, will I be able to use the rPower variable to direct a projectile? Haven't gotten that far yet.
#Override
public boolean touchDown(int x, int y, int pointer, int button) {
if (Gdx.input.isTouched(0)) {
cam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
}
if (Gdx.input.isTouched(1)) {
cam.unproject(touchPoint2.set(Gdx.input.getX(), Gdx.input.getY(), 0));
}
return true;
}
#Override
public boolean touchDragged(int x, int y, int pointer) {
if (Gdx.input.isTouched(0)) {
cam.unproject(dragPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
dx = touchPoint.x - dragPoint.x;
dy = touchPoint.y - dragPoint.y;
throwerLowerArmSprite.setRotation(dx * 30);
}
if (Gdx.input.isTouched(1)){
cam.unproject(dragPoint2.set(Gdx.input.getX(), Gdx.input.getY(), 0));
d1x = dragPoint2.x - touchPoint2.x;
d1y = dragPoint2.y - touchPoint2.y;
throwerUpperArmSprite.setRotation(d1x * 30);
}
return true;
}
#Override
public boolean touchUp(int x, int y, int pointer, int button) {
if (!Gdx.input.isTouched(0)) {
cam.unproject(releasePoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
rPower = releasePoint.x - touchPoint.x;
throwerLowerArmSprite.setRotation(0);
}
if (!Gdx.input.isTouched(1)) {
cam.unproject(releasePoint2.set(Gdx.input.getX(), Gdx.input.getY(), 0));
rPower = releasePoint2.x - touchPoint2.x;
throwerUpperArmSprite.setRotation(0);
}
return true;
}
Your sprite rotates back to 0 degrees because of this in the touchUp method:
throwerLowerArmSprite.setRotation(0);
All the objects with setRotation methods also have a getRoatation() method. So you can save the current rotation with:
float oldRotation = mySprite.getRotation();
Unrelated to the question, but you can simplify all of your input event callbacks. You're mixing in the event polling methods to lookup data that the event callback already provides. For example, your touchDown method could use its parameters like this:
public boolean touchDown(int x, int y, int pointer, int button) {
if (pointer == 0) {
cam.unproject(touchPoint.set(x, y, 0));
} else if (pointer == 1) {
cam.unproject(touchPoint2.set(x, y, 0));
}
return true;
}
This is even more useful in your touchUp method where the pointer parameter will tell you what Gdx.input.isTouched cannot (namely which pointer is the one that is no longer touching the screen).

Categories

Resources