How to move button randomly on the screen on click? - android

So I am trying to get a button on the screen to move when it is clicked. It currently is setup to count clicks, but I want the button to move everytime you click it. I have tried a translate animation, which didn't work because the button still fires the onClick from the original location.
I leared about ObjectAnimator and have been trying to get that to work since the onClick event will move with the button, but I can't seem to get it to work. I also need help getting the button to move randomly within it's layout parent. Any help would be greatly appreciated :)
Here's my current Object Animator Code. Please note it is not setup for random movement yet, since I am unsure of how to get it to do that:
ObjectAnimator animX = ObjectAnimator.ofFloat(btnCount, "xTranslate", 0f, 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(btnCount, "TranslateY", 0f, 50f);
AnimatorSet animSet = new AnimatorSet();
animSet.playSequentially(animX, animY);
animSet.setDuration(500);
animSet.start();

Easiest way to get this done will be as given below. Do this on your onClick listener.
btnCount.animate().xBy(10).yBy(10);
With regards to random, I guess you can use something as simple as this... In any case, you may have to take care of reaching the screen limit.
Random r = new Random();
btnCount.animate().xBy(r.nextInt(10)+1).yBy(r.nextInt(10)+1);

Found that using the below did what I needed it to:
Random r = new Random();
int buttonHeight;
int buttonWidth;
buttonHeight = btnCount.getHeight();
buttonWidth = btnCount.getWidth();
int xLeft = r.nextInt(480 - buttonHeight);
int yUp = r.nextInt(800 - buttonWidth);
int xRight = r.nextInt(480 + buttonHeight);
int yDown = r.nextInt(800 + buttonHeight);
btnCount.setX(xLeft);
btnCount.setY(yUp);
btnCount.setX(xRight);
btnCount.setY(yDown);

Related

Image sliding from left to right infinitely

I wish to have an animated background like this gif:
.
I don't have a mac or adobe after effects software to create a lottie json for such animation, and i want a more command over animation than simply using a gif. From what i have read over the internet, I have to use either TransitionManager or TranslationAnimation , but i am unable to figure out the correct code for it.
I am also interested in knowing if there is a plugin, or some way to make minor edits to already available lottie files like background color change , items color change, items removal, etc just like the way we could do for simple vector images(Using Android Studio, or at the very least, some free software). I found this lottie file which was doing a very great job for me, but i couldn't modify its colors
found it! When i logged the values of some variables like view.getX() or view.getRootView().getX(), i was able to figure out, that my view will have its location as 0,0 . so After that it was all a need for finding the screen size to finally make this infinite cloud moving animation. I am not sure if this function is good in terms of memory or performance , but on my device, its running smoothly.
private void showCloudMovingContinuouslyAnimation(View v) {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int runtimeScreenWidth = displayMetrics.widthPixels;
int totalDuration = runtimeScreenWidth*5;
int viewPositionX = 0, viewPositionY = 0;
TranslateAnimation outToRight = new TranslateAnimation(
viewPositionX, (runtimeScreenWidth / 2f) + 50,
viewPositionY, viewPositionY);
outToRight.setDuration(totalDuration / 2);
outToRight.setRepeatCount(Animation.INFINITE);
TranslateAnimation inFromLeft = new TranslateAnimation(
-((runtimeScreenWidth / 2f) + 50), viewPositionX,
viewPositionY, viewPositionY
);
inFromLeft.setDuration(totalDuration / 2);
inFromLeft.setRepeatCount(Animation.INFINITE);
boolean shouldRemainThereAfterAnimationFinishes = true;// useful when animating for single time
AnimationSet animationSet=new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.setRepeatCount(Animation.INFINITE);
animationSet.addAnimation(inFromLeft);
animationSet.addAnimation(outToRight);
v.startAnimation(animationSet); // start animation
}

Can't move Android SeekBar with Appium

I have a customized Android seekbar like this, and the positions where it can move to. And it starts from the middle:
I want to move the slider first and then check if it's saved. I have a method which I use TouchAction:
public void moveSeekBar(){
WebElement seekBar = appiumDriver.findElementById("com.feverapp:id/SymptomTrackingActivity_var");
//Get start point of seekbar.
int startX = seekBar.getLocation().getX();
System.out.println(startX);
//Get end point of seekbar.
int endX = seekBar.getSize().getWidth();
System.out.println(endX);
//Get vertical location of seekbar.
int yAxis = seekBar.getLocation().getY();
//Set slidebar move to position.
// this number is calculated based on (offset + 3/4width)
int moveToXDirectionAt = 786;
System.out.println("Moving seek bar at " + moveToXDirectionAt+" In X direction.");
//Moving seekbar using TouchAction class.
TouchAction act=new TouchAction(appiumDriver);
act.press(startX,yAxis).moveTo(moveToXDirectionAt,yAxis).release().perform();
}
Something I notice that:
seekbar doesn't move at all
getX and getY are always the same values even I tried to set slider back to very left before I call this method
I tried with sendkeys like this:
seekbar.sendKeys("0.5");
and it worked: it moved the slider to the very left, and it only worked with 0.5, when I entered a number within the range from 0 to 1, it didn't work
Any help much appreciated. Thanks
#Test
public void testSeekBar()throws Exception
{
//Locating seekbar using resource id
WebElement seek_bar=driver.findElement(By.id("seek_bar"));
// get start co-ordinate of seekbar
int start=seek_bar.getLocation().getX();
//Get width of seekbar
int end=seek_bar.getSize().getWidth();
//get location of seekbar vertically
int y=seek_bar.getLocation().getY();
// Select till which position you want to move the seekbar
TouchAction action=new TouchAction(driver);
//Move it will the end
action.press(start,y).moveTo(end,y).release().perform();
//Move it 40%
int moveTo=(int)(end*0.4);
action.press(start,y).moveTo(moveTo,y).release().perform();
}
This worked for me very well
I just solved my problem: instead of using press(), I try longpress() and it works like a charm! I can move seekbar where I want.
I tried like this seekbar.sendKeys("50");
and its working for me.(0(min) - 100(max))
#Test
public void seekBar() throws Exception {
WebElement seek_bar=driver.findElement(By.id("seek_bar"));
int start=element.getLocation().getX();
int end1=element.getSize().getWidth();
int end=(int)(end1*0.4);
int y=element.getLocation().getY();
PointOption point = PointOption.point(start, y);
PointOption point2 = PointOption.point(end, y);
TouchAction action=new TouchAction(driver);
action.press(point).moveTo(point2).release().perform();
}

Object Animator in Android

I want to mimic the animation on 4 Pics 1 Word, wherein the user taps a letter and flies somewhere in the screen. This letter can be a Button or an ImageView. Please refer to the image below:
Is this only possible with Corona SDK / Lua? How will I do it in Java? I'm just a beginner so I don't know how to start this so please don't ask any code that I already tried.
You can use ObjectAnimator class for performing animations on Android views. For example, if you have to animate the y property of the view mView you can write:
float finalValue = 10f;
ObjectAnimator yAnimator = ObjectAnimator.ofFloat(mView, View.Y, finalValue);
yAnimator.start();
For animating more than one property you can write:
float finalX = 20f;
float finalY = 30f;
ObjectAnimator xAnimator = ObjectAnimator.ofFloat(mView, View.X, finalX);
ObjectAnimator yAnimator = ObjectAnimator.ofFloat(mView, View.Y, finalY);
new AnimatorSet().playTogether(xAnimator, yAnimator);

Sprite Jump in my Game Play Andengine

Hy, I have started a Game in andengine for android, I want to apply a jump functionality on a sprite which will be like some projectile motion , up and forward as well . I have applied move modifier to gain that functionality. But that is not in projectile motion.
How can i achieve that and while the jump of my sprite by tapping again it cant jump again until it completes it jump.i dont want to use delay function on scene touch .any help .
final Entity playerEntity = move;
final float jumpDuration = 1;
final float startX = playerEntity.getX();
final float jumpHeight = 60;
move.getTextureRegion().setFlippedVertical(true);
final MoveModifier jumpForwardUp = new MoveModifier(jumpDuration/2, startX, startX - jumpHeight, playerEntity.getY(), playerEntity.getY() + 130);
final MoveModifier jumpForwardDown = new MoveModifier(jumpDuration/2, startX - jumpHeight, startX, playerEntity.getY() + 130, playerEntity.getY() + 170);
final SequenceEntityModifier modifier = new SequenceEntityModifier(jumpForwardUp ,jumpForwardDown);
playerEntity.registerEntityModifier(modifier);
Try using JumpModifier and for some real physics work, go for Box2d Extension of AndEngine and go through some of AndEngine examples code.
Edit:
AndEngine have list of modifiers. JumpModifier is one of them. Maintain a class level or project level flag that is true when your object is in jump and is false when object is in normal state.
Every modifier takes an object of IModifierListner set that flag to true in onStart of IModifierListner and set that to false at onEnd of IModifierListner. at every tap check if that flag is true then simple return and do nothing.

How to animate text over another View in Android?

I'm trying to animate some text 'zooming out' on top of another view. My code looks something like:
class BoardView extends View {
private TextView animText;
...
private void animText(String text, int color, int xBlocks, int yBlocks) {
animText.setText(text);
animText.setTextColor(color);
animText.setVisibility(View.VISIBLE);
final int x = BOARD_X_OFFSET + xBlocks * xBlockSize;
final int y = BOARD_Y_OFFSET + yBlocks * yBlockSize;
final float SCALE_FROM = (float) 0.25;
final float SCALE_TO = (float) 5.0;
ScaleAnimation anim = new ScaleAnimation(SCALE_FROM, SCALE_TO, SCALE_FROM, SCALE_TO, x, y);
anim.setDuration(500);
animText.setAnimation(anim);
this.setAnimation(null);
startAnimation(anim);
}
}
with animText being invoked in the onDraw() routine of the BoardView. What I'm seeing, however, is the board zooming out, not the text, despite the above calls to setAnimation().
I've looked in the main android docs and at one other example. Even pointers in the right direction would be helpful.
Well, I'm glad I'm not the only one who had a difficult time finding the very subtle bug in the above. The bug is:
startAnimation(anim);
which isn't too obvious until you expand it into:
this.startAnimation(anim);
at which point it's clear why the outer view (ie. 'this') is animating as well as the text.

Categories

Resources