Touch event on custom keyboard - android

I've created a customized keyboard of my own.
When I click & drag over the keyboard the key should be highlighted according to my finger move. When I lift up my finger the corresponding letter has to be printed on the EditText.
How could I do this?

Assuming you are just drawing your 'customized' keyboard onto a Canvas. Obviously using the built-in keyboard is better. However if you really want to do this yourself, here is a solution.
Draw each character of your keyboard, store it's x,y location in an array corresponding to each key as you draw it.
//initialise
int[] letterX = new int[29];
int[] letterY = new int[29];
char[] keyboardChar = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M',' ', '<','#'};
somewhere in your 'draw' method:
// first keyboard row (do this for each row moving adding to y for next row
x=10; y=50; keySpacing = 30; // starting x, y position
for ( int charIndex = 0; charIndex < 10; charIndex++) {
letterX[charIndex] = x;
letterY[charIndex] = y;
textWidth = mPaint.measureText(keyboardChar, charIndex, 1);
if ( !letterHit ) {
canvas.drawText(keyboardChar, charIndex, 1, x - (textWidth/2), y, mPaint);
} else {
// this key is being pressed so draw highlighted somehow
canvas.drawText(keyboardChar, charIndex, 1, x - (textWidth/2), y,mPaint);
}
x += keySpacing;
}
In the onTouchEvent, compare the x, y touch position to your array of letterX, letterY this will tell you if a key is being pressed. The array index tells you which character it is. You'd need some code in the draw method to print out the key highlighted if its being pressed (example below assumes 16px tolerance).
for (int j=0; j < 29; j++ ) {
if (( Math.abs(touchX - letterX[j]) < 16 ) && ( Math.abs(touchY - letterY[j]) < 16 ) && !keyboardLock ) {
letterHit = j;
}
}
You'll still need more logic (for deleting etc) and you'll need to maintain a string. That's why really this is best to use the system keyboard if possible.

I think you need something like this post .

Related

How to make an apple fall from the top of the screen?

I am trying to make an apple falls from the top of the screen to the the bottom. I found the below solution which works fine. but there is only one problem if I change the variable changingY to localCoordinates2.getY() the apple does not move any more, So how can I make the apple falls fall down ?
canvas.drawBitmap(localGoodApple.getGraphic(), localCoordinates2.getX(),changingY, null);
if(changingY < canvas.getHeight() ) {
changingY +=10; // The changingY is a field of type integer
}else
{
changingY = 0;
}
If you change changingY to localCoordinates2.getY() the apple will not move because getY() is just returning the value of whatever the y is on localCoordinates2, but not the reference, so any changes you make to getY() won't actually change the y.
Change your code to this(assuming setY is a method):
canvas.drawBitmap(localGoodApple.getGraphic(), localCoordinates2.getX(), localCoordinates2.getY(), null);
if(localCoordinates2.getY() < canvas.getHeight() ) {
localCoordinates2.setY(localCoordinates2.getY() + 10);
} else
{
localCoordinates2.setY(0);
}

Animating 3 parts of a view

I am trying to make an app view that, when presented with a subtraction problem, will show an animation of a growing rectangle (or fat line) that crawls up the number line to that integer.
I already have things set up so that I click "show me!" and bars are drawn along the number line showing the minuend, the subtrahend and the difference, but I'd like to be able to have a positive number's rectangle crawl up in a positive direction, negative from zero in the negative direction.
In looking through the documentation there seem to be several different ways to go about this. I am hoping somebody can suggest a way that's reasonably simple for this novice to implement. Here are the different approaches I've found:
This seems very much like this person's desire to have a bar graph where the bars "pop up," but it doesn't have an answer. Android Animated Bar Chart (invalidate())
I've perused http://developer.android.com/guide/topics/resources/drawable-resource.html -- but I don't have a "drawable" because it's being drawn in the View. I'm thinking of making the rest of the number line a background bitmap per Android View.getDrawingCache returns null, only null but I want three rectangles (for the minuend, subtrahend and difference).
I have thought of making a series of rectangle drawables and showing them frame-by-frame to show the growth.
I have looked at Animation at a specified rate using canvas / Ondraw but cannot discern just what code to wrap in that "if" statement, if in fact my problem is re-drawing...
I looked at using Paths -- and put the following code together. If direction matters, then it seems I should be able to slow things down and watch the path going in that direction, but it's instantaneous. I found I saw an example at http://www.curious-creature.org/2013/12/21/android-recipe-4-path-tracing/
if (minuendLength > 0) // start at 0 and go to the minuend length
{
path.addRect(interpX(0), yPosition(40), interpX(minuendLength), yPosition(43) , Path.Direction.CW);
// interpX lets me say what number on the number line it should align with;
//yPosition is percent of the way down the screen.
canvas.drawPath(path,minuendPaint);
// Seems same as drawRect -- instantaneous.
}
(The number line in the 'background' code is as follows, with different options for different sized integers entered:
if ( (minuendLength <10 && subtrahendLength <10 ) && (minuendLength >-10 && subtrahendLength >-10 ) )
{
this.setLineDimension(10); // default
super.onDraw(canvas);
canvas.drawLine(interpX(-this.getLineDimension()), yPosition(52 ),
interpX(this.getLineDimension()), yPosition(52), axisPaint);
int step = this.getLineDimension()/5; // so you're not writing *all* the numbers
// when they enter numbers and you make your own number line.
// paints the little hatch marks
for (int x = -this.getLineDimension(); x <= this.getLineDimension(); x+=step/2)
canvas.drawLine(interpX(x), yPosition(52), interpX(x), yPosition(53) , littleAxisPaint);
// draw the numbers on the hatch marks
textPaint.setTextAlign(Paint.Align.CENTER);
for (int x = -this.getLineDimension() + step; x < this.getLineDimension(); x += step)
{
canvas.drawText(Integer.toString(x), interpX(x), yPosition(56), textPaint);
}
}
I ended up doing this using Runnables.
animate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( ((addend1 > 0) && (addend2 < 0)) )
{
tempNum1 =addend1 ;
tempNum2 = addend2;
h.post(shrink1bigger);
}
else if (addend1 < 0 && addend2 > 0)
{
tempNum1 =addend1 ;
tempNum2 = addend2;
h.post(shrink2bigger);
}
}
});
private Runnable shrink2bigger = new Runnable(){
#Override
public void run() {
tempNum1+=1;
tempNum2-=1;
shrinkDraw();
Log.i("WatchAnimActivity", "tempNum1 is " + tempNum1);
h.postDelayed(shrink2bigger, 500);
checkTemp(shrink2bigger);
}};
private void shrinkDraw()
{
numLine.setFirstNum(tempNum1);
numLine.setNextNum(tempNum2);
numLine.invalidate();
}
public void checkTemp(Runnable shrink)
{
Log.i("WatchAnimActivity", "tempNum1 in CHeckTemp is " + tempNum1);
if (tempNum1 ==0 || tempNum2==0)
h.removeCallbacks(shrink);
}

libgdx smooth movement of rectangles

I want to moove two objects smoothely at Touching.
Here is my Code:
for(int i = 0; i <96; i++){
Asstest.rect_pipe_down.y--);
}
This should move the rect 96 pixels down (SMOOTH)
But it just close without smoothed...
What did I wrong?
If you Touch, the pipes should close, but not hard, smooth should they close.
But with following code they just close hard...
Here is the full touched code:
if(Gdx.input.isTouched()){
Assets.rect_pipe_down.y = 512 - 320/2;
Assets.rect_pipe_up.y = -320 + 320/2;
for (int i = 0; i < 96; i++){
smoothTime = TimeUtils.millis();
if(TimeUtils.millis() - smoothTime > 10) {
Assets.rect_pipe_down.y--;
Assets.rect_pipe_up.y++;
batch.begin();
batch.draw(Assets.region_pipe_down, Assets.rect_pipe_down.x, Assets.rect_pipe_down.y);
batch.draw(Assets.region_pipe_up, Assets.rect_pipe_up.x, Assets.rect_pipe_up.y);
batch.end();
}
}
closed = true;
}
You cannot do rendering multiple times in one render() call, one call is for drawing exactly one frame. In your current code, the later images simply overwrite the previous ones.
What you could do is have a variable which persists between frames which stores whether or not the pipes are currently closing, a constant for the speed and some condition to tell when they can stop - maybe when they are some given distance from each other, not sure what you would want here. Anyway, that's what I'll use in my example.
Then in the render() method, before drawing anything, you can do this:
if (closing) {
Assets.rect_pipe_down.y -= CLOSE_SPEED * delta;
Assets.rect_pipe_up.y += CLOSE_SPEED * delta;
if (Assets.rect_pipe_down.y - Assets.rect_pipe_up.y < TARGET_DIST) {
Assets.rect_pipe_down.y = Assets.rect_pipe_up.y + TARGET_DIST;
closing = false;
}
}
Here, closing is a variable you set to true when you want them to start closing, the others are constants. You could add some more variables/constants if you want to make sure they end up at a specific height independent on framerate.

TouchLeave for DOM element

I want a div element on my page which increases a number on the page while it is pressed.
This is fairly easy, but I want this to work on IPad and Android devices as well.
I am using the touchstart event using jQuery bind on the div.
Using a setInterval I update the number (I could use calling a setTimeout every time the number is increased, but that is irrelevant).
I want the interval cleared when the finger (touch) moves outside the div. Unfortunately touchend is not called until the finger is released from the screen.
Mobile safari or webkit do not seem to support touchleave.
So here my question: Any ideas on how to mimic touchleave?
Based on above answer, here's what I do.
var $this = $('elementselector');
var fnmove = function (e) {
e.preventDefault();
e = e.originalEvent;
var touch = e.touches[0] || e.changedTouches[0];
if (!_isInBounds(touch, $this.offset(), $this.outerWidth(), $this.outerHeight())) {
$this.trigger(touch_leave_event);
$this.unbind(touch_move_event, fnmove);
};
};
$this.bind(touch_move_event, fnmove);
-- inbounds function
function _isInBounds(touch, elemposition, width, height) {
var left = elemposition.left,
right = left + width,
top = elemposition.top,
bottom = top + height,
touchX = touch.pageX,
touchY = touch.pageY;
return (touchX > left && touchX < right && touchY > top && touchY < bottom);
};
EDIT (thanks to Guillaume to completing this):
var $this = $('elementselector');
var fnmove = function (e) {
e.preventDefault();
e = e.originalEvent;
var touch = e.touches[0] || e.changedTouches[0];
if (!_isInBounds(touch, $this.offset(), $this.outerWidth(), $this.outerHeight())) {
$this.trigger(touch_leave_event);
$this.unbind(touch_move_event, fnmove);
};
};
$this.bind(touch_move_event, fnmove);
function _isInBounds(touch, elemposition, width, height) {
var left = elemposition.left,
right = left + width,
top = elemposition.top,
bottom = top + height,
touchX = touch.pageX,
touchY = touch.pageY;
return (touchX > left && touchX < right && touchY > top && touchY < bottom);
};
Sorry for the short answer, I'm leaving work. I'll come back to fill in the blanks. But you should get the idea.
The best method I've found is to check the position of the touchpoint inside your touchmove handler. When a touchpoint is created (on touchstart), it is added to an array called "touches". These touchpoints can be accessed by index (order in which the touchpoints were added). Then you can access the x and y coordinates from this point to determine whether or not it's still within the bounds of the element you're observing. Let's say I have a div called "myDiv" and I want to detect when the user's finger is dragged out of it from within.
var myDiv = document.getElementById('myDiv');
myDiv.addEventListener('touchmove', function(event){
var currentElement = document.elementFromPoint(event.touches[0].clientX, event.touches[0].clientY);
if (currentElement.id !== 'myDiv'){
console.log('finger has moved outside of element');
}
}, false);
So when we move our finger, we capture over which element it currently is with the document.elementFromPoint() function, passing in the x and y coordinates gathered from the touch stored in the touches array, at index 0 (addition touches would be added to the array at index 1, 2, 3, etc.). Lastly, we check if this captured element where our finger currently is is the same as "myDiv". If it isn't, we know the finger has been moved off of this element.

How can you detect which view you are passing over when performing a touch event?

I want to know how I can detect child views if I move a view from one ViewGroup to another ViewGroup, particularly when doing a touch event. Is there a method I can call that will let me know which views i'm "hovering" over?
What I'm doing right now is when I detect an ACTION_MOVE event on my view i'm raising it to the top level parent so that it can move and be drawn within the entire window ( and not just inside it's original parent bounds ), then I want to move the view across to a different ViewGroup and on ACTION_UP attach the view to that ViewGroup.
Inspired by Ami's response, but discovering that MotionEvent#getX()/getY() along with View#getTop()/etc return coordinates wrt the parent View, I ended up doing the following below to operate in screen coordinates, allowing me to work across ViewGroups:
private boolean inRegion(float x, float y, View v) {
v.getLocationOnScreen(mCoordBuffer);
return mCoordBuffer[0] + v.getWidth() > x && // right edge
mCoordBuffer[1] + v.getHeight() > y && // bottom edge
mCoordBuffer[0] < x && // left edge
mCoordBuffer[1] < y; // top edge
}
whose usage inside an OnTouchListener is e.g.:
boolean inside = inRegion(event.getRawX(), event.getRawY(), targetView);
I think I found a simpler way to do this.
Create an ArrayList of possible targets
Call this method from your touch event, supplying your targets list and the coords
private View findView(float x, float y, ArrayList<View> targets)
{
final int count = targets.size();
for (int i = 0; i < count; i++) {
final View target = targets.get(i);
if (target.getRight() > x && target.getTop() < y
&& target.getBottom() > y && target.getLeft() < x) {
return target;
}
}
return null;
}
I found Sebastian Roth's answer very helpful with resources, but since it wasn't really an answer to my question, I thought I'd share what I came up with.
Here is the code I use to detect views ( only views that will accept a drop that is ) given a coordinate on the screen.
private DropView findDropTarget( int x, int y, int[] dropCoordinates ){
final Rect r = mRectTemp;
final ArrayList<DropView> dropTargets = ((main) context).getBoardDropTargets();
final int count = dropTargets.size();
for (int i=count-1; i>=0; i--) {
final DropView target = dropTargets.get(i);
target.getHitRect(r);
target.getLocationOnScreen(dropCoordinates);
r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
if (r.contains(x, y)) {
dropCoordinates[0] = x - dropCoordinates[0];
dropCoordinates[1] = y - dropCoordinates[1];
return target;
}
}
}
Ok, first off mRectTemp is just an allocated Rectangle so you don't have to keep creating new ones ( I.E. final Rect r = new Rect() )
The next line dropTargets is a list of views that will accept a drop in my app.
Next I loop through each view.
I then use getHitRect(r) to return the screen coordiantes of the view.
I then offset the coordiantes to account for the notification bar or any other view that could displace the coordiantes.
finally I see if x and y are inside the coordinates of the given rectangle r ( x and y are the event.rawX() and event.rawY() ).
It actually turned out to be simpler then expected and works very well.
Read this:
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)
I had implemented a Drag and Drop using that method.
I also highly recommend a read of the HomeScreen sourcecode, which contains this thing (kind of):
https://android.googlesource.com/platform/packages/apps/Launcher2

Categories

Resources