I want to move a button(button1) when I click on it threw the x-axis.
Here is my code:
btn=(Button)findViewById(R.id.button1);
currentX=btn.getX();
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX,currentX+10);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
currentX=v.getX();
currentY=v.getY();
}
});
It moves at the first click, but after that it just starts from the beginning, instead of moving 10dp further on the x-axis. what am I missing?
You need to either create a new animator or you need to update the values of the moveX and moveY animations. (Actually, you do not need the moveY animation since you are not changing the Y position.)
Also, Views have a more performant animation capability using .animate() to acquire a ViewPropertyAnimator. I would refactor your code this way:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.animate().xBy(10).start()
}
});
If later you decide you also want to animate the Y position, you can simple add .yBy(value) before the .start().
i don't think the v.getx will change after the anime
try this :
int multi =1;
btn=(Button)findViewById(R.id.button1);
currentX=btn.getX();
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX+10*(multi-1),currentX+10*multi);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
multi++;
currentY=v.getY();
}
});
It is because you are not holding the position of x-axis to which your button has moved,after click you set it again to intial value.
Change these lines
use a static variable.
static int x_axis=10+btn.getX();
currentX=x_axis;
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX,currentX);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
x_axis=x_axis+10;
currentY=v.getY();
}
});
Related
I want to make it so that once I press a button, it moves and does an animation by quickly swapping the src of the imageview mid animation.
I've tried to use Thread.sleep .
first.setImageResource(R.drawable.cata2);
first.setImageResource(R.drawable.catb2);
ObjectAnimation Code
x += 10
first.setImageResource(R.drawable.cata2);
ObjectAnimator d = ObjectAnimator.ofFloat(first, "translationX", x);
d.setDuration(50);
d.start();
It always just switches to cata2.
Try this one. If the changing image resource not working, try to change it in background resource. Just adjust some duration and the thread sleep.
in C#:
Try to put this all inside the listener of your button
Setting up the Animation
var clockwise = AnimationUtils.LoadAnimation(this.Activity, Resource.Animation.clockwise);
clockwise.Duration = 2000;
To set your first to cata2
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.cata2);
});
To change with animation from cata2 to catb2
Thread t = new Thread(delegate ()
{
first.StartAnimation(clockwise);
Thread.Sleep(500);
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.catb2);
first.ClearAnimation();
return;
});
});
t.Start();
We can add a listeter to your animation
d.addListener(this);
Then generate overridess
#Override
public void onAnimationStart(Animator animation){
//add some codes
}
#Override
public void onAnimationEnd(Animator animation){
//add some codes
}
#Override
public void onAnimationRepeat(Animator animation){
//add some codes
}
#Override
public void onAnimationCancel(Animator animation){
}
I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?
I've got a simple View property animation attached to a button. The animation works perfectly on the first button press. However, on subsequent clicks no animation occurs. The log proves the button is being pressed. Is there something I'm missing? Here is the code:
Button handle = (Button) findViewById(R.id.HandleButton);
Button wheel1 = (Button) findViewById(R.id.Wheel1);
handle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Button Clicked");
ViewPropertyAnimator spinWheel1 = wheel1.animate().rotationX(360*8).setDuration(2000);
}
});
Button handle = (Button) findViewById(R.id.HandleButton);
handle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Button Clicked");
spinWheel1 = wheel1.animate().rotationXBy(360*8).setDuration(2000);
}
});
Try using rotationXBy instead of rorationX, you may also want to try using object animator as you can set start and end rotation degree.
try this:
ObjectAnimator animator = ObjectAnimator.ofFloat(spinWheel1, View.ROTATION, 0, 360*8);
animator.setDuration(2000);
animator.start();
your button already has rotated to 360*8
By adding the 0 your animation will start all over again
Sorry for the weird question, but is it possible to click on two buttons at the same time in android(having two logs, "clicked on b1" and "clicked on b2"), if one totally covers the other one?
This is not ordinarily possible; the top button will absorb the button click event and not pass it on to the one behind it. It is not clear whether or not you want to obtain this behaviour or avoid it, nonetheless, you can force it by propagating the event manually across the click listeners.
Here is one way (there are a few); assume buttonOne is on top of buttonTwo:
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
buttonTwo.performClick();
}
});
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
});
The click event enters the listener on button one, which then causes a click event on button two.
Here is another way which would work (and could be changed to support long click events easily):
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
final OnClickListener listenerTwo = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
};
final OnClickListener listenerOne = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
listenerTwo.onClick(buttonTwo);
}
};
buttonOne.setOnClickListener(listenerOne);
buttonTwo.setOnClickListener(listenerTwo);
Yes, it is possible. You will need to pass the click event that occurs on the foreground view to the background view. You can do this by checking where the click occurs and if it occurs within the view's bounds.
I have a view in the UI that is technically an extended ImageView inside a RelativeLayout that I'd rather not change, if possible. I want to show a Drawable from resources (a small PNG with a gradient or a frame) at specific coordinates as a visual feedback when user is tapping the screen (there even is a onShowPress callback). What is the simplest way to do this? I don't want to modify the layout.xml from which UI is inflated, and I'm not too enthusiastic about overriding onDraw to do extra job there.
This is one easy way to do it using view animation class:
final ImageView aboutButton = (ImageView) findViewById(R.id.AboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation myAnim2 = AnimationUtils.loadAnimation(LaunchActivity.this, R.anim.buttons);
aboutButton.startAnimation(myAnim2);
myAnim2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//do on click after animation ends
Intent intent = new Intent(LaunchActivity.this, MyDialog.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
//change the backround of the button etc on click etc...
}
});
}});