Android moving buttons and background in runtime - android

I'm trying to make a tab look-A-like buttons row with a background that is an ImageView.
Here is the ImageView:
now on this image there are three buttons when the fragment loads.
when i click button one:
there are two situations:
1) when screen looks like the image above, then i want to move the buttons to the right so the screen looks like this:
and when it looks like the image above, i want it to return to the first image state.
the last case is when a user press's B3 i want it to react the same as B1 but to the other side, like this:
I tried so far lots of methods to try this, all have not worked, it seems that when i animate the movement the buttons stay in the same place:
here is my code so far:
btn_spa.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
isMapPressed=false;
isInfoPressed=false;
int toMoveNow=0;
if(!isSpaPressed)
{
if(isMapPressed)
toMoveNow=(toMove/2)+40;
else
toMoveNow=-(toMove/2+40);
int[] arr = new int[]{0,0};
btn_spa.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
//rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button spa clicked", Toast.LENGTH_SHORT).show();
}
}
});
btn_map.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
isSpaPressed=false;
isInfoPressed=false;
int toMoveNow=0;
if(!isMapPressed && isSpaPressed)
{
toMoveNow=-(toMove/2+40);
int[] arr = new int[]{0,0};
btn_map.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
// rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button map clicked", Toast.LENGTH_SHORT).show();
}
else
{
toMoveNow=(toMove/2+40);
int[] arr = new int[]{0,0};
btn_map.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
// rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button map clicked", Toast.LENGTH_SHORT).show();
}
}
});
btn_info.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
int toMoveNow=0;
if(!isInfoPressed && isSpaPressed)
{
toMoveNow=(toMove/2)+40;
int[] arr = new int[]{0,0};
btn_info.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button info clicked", Toast.LENGTH_SHORT).show();
}
else
{
toMoveNow=-(toMove/2);
int[] arr = new int[]{0,0};
btn_info.getLocationOnScreen(arr);
Animation animation = new TranslateAnimation(arr[0], toMoveNow, 0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
rel_buttons_wrapper.startAnimation(animation);
btn_info.startAnimation(animation);
btn_map.startAnimation(animation);
btn_spa.startAnimation(animation);
Toast.makeText(getActivity(), "Button info clicked", Toast.LENGTH_SHORT).show();
}
isSpaPressed=false;
isMapPressed=false;
}
});
Sorry for the long question, I wanted to be as informative as possible.
any help would be good guys,
Kind Regards.

you can use ObjectAnimator class, it moves views and their bounds so they actually move when you move them, here is a tutorial :
http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator

Related

How to make a proper move in/out animation?

In onViewCreate I set the view behind the screen:
holderMenus.post(new Runnable() {
#Override public void run() {
if (holderMenus != null) {
if (DeviceType.INSTANCE.tablet) {
holderMenus.animate().translationY(-holderMenus.getHeight());
} else {
holderMenus.animate().translationY(holderMenus.getHeight());
}
}
}
});
Then, when I want to move the view in:
holderMenus.animate().translationY(0).setDuration(300);
That works well; the view slides in. Next I want to move the view off the screen:
if (DeviceType.INSTANCE.tablet) {
holderMenus.animate().translationY(-holderMenus.getHeight()).setDuration(300);
} else {
holderMenus.animate().translationY(holderMenus.getHeight()).setDuration(300);
}
But instead of sliding, the view disappears, as if I had set the property to .setVisibility(View.GONE);
How can I make it slide instead?
I have made a custom action button that slides from below the screen and slides back under the screen when listview is scrolled.
Hides:
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 500);
anim.setDuration(300l);
anim.setFillAfter(true);
layout.startAnimation(anim); //layout is the layout with the button
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
layout.clearAnimation();
layout.setVisibility(View.GONE);
}
}, 300l);
Shows:
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
try {
layout.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(0, 0, 500, 0);
anim.setDuration(300l);
anim.setFillAfter(true);
layout.startAnimation(anim);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 500l);
I guess that slide out animation is not starting from the point that you think it is. It probably "animates" in the same position off the screen.
You might want to try ObjectAnimator, where you can specify both starting and ending points for your animation using ofFloat method for example.

ImageSlider + animation views

I have ViewPager + PageAdapter for slide images. I'm trying add Alpha animation button which appears when user tap on screen and disappears when tap again.
When I add clickListner for ImageView or for some invisible layout over ImageView animation button works fine, but slider doesn`t work now. What I should do for combine these 2 functions.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Animation aAnim1 = new AlphaAnimation(1.0f, 0.0f);
final Animation aAnim2 = new AlphaAnimation(0.0f, 1.0f);
if (VISIBLE_FLAG == true) {
aAnim1.setDuration(250);
aAnim1.setFillAfter(true);
save_btn.startAnimation(aAnim1);
exit_btn.startAnimation(aAnim1);
VISIBLE_FLAG = false;
}
else {
aAnim1.setDuration(250);
aAnim1.setFillAfter(true);
save_btn.startAnimation(aAnim2);
exit_btn.startAnimation(aAnim2);
VISIBLE_FLAG = true;
}
}
});

Why OnAnimationEnd moves the initial position of the button but also moves the button itself? #.#

I did some animations with regards to my created button. However, after the animation, the button is at its new position, but the onclick function is at its old initial position. I did the onAnimationEnd and set the button to the new position but the Button and the invisible-like button with the onclick function moves together. Any help will be awesome
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//move
TranslateAnimation animation = new TranslateAnimation(0.0f, ((widthPixels / 2) - 100),
0.0f, 0.0f);
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.setRepeatMode(0);
animation.setFillEnabled(true);
animation.setFillAfter(true);
//fade
// Play.setVisibility(View.VISIBLE);
Play.startAnimation(animationfadein);
Play.startAnimation(animation);
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DreamTales.this, Stories.class);
startActivity(i);
}
});
}
}, DELAY);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Button Playghost = (Button) findViewById(R.id.PlayBtn);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)Play.getLayoutParams();
lp.leftMargin = (widthPixels/100); // use topmargin for the y-property, left margin for the x-property of your view
Playghost.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

After translate animation button click not working

when i click a button the translate animation starts. then button placed where translate animation gets end. it working good but after animation button click not working.
thanks in advance
public class ButtonFragment extends Activity implements OnClickListener{
private Button btn;
private int width;
private Boolean flag = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_fragment);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
TranslateAnimation animation = null;
switch (v.getId()) {
case R.id.btn:
if(flag == true) {
animation = new TranslateAnimation(0, width-92 , 0, 0);
flag=false;
}
else{
animation = new TranslateAnimation(width-92,0 , 0, 0);
flag = true;
}
animation.setDuration(1000);
animation.setFillAfter(true);
btn.startAnimation(animation);
break;
default:
break;
}
}
}
code proper alignment was done
As given in this page:
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
Try using property animation and read the link given above.

How to set button not clickable while animating on Android?

How to make button not clickable while translate.animate is in progress. I tried with button.setclickable(false) and button.setEnabled(false) both dint worked for me.
Here is my code:
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(i%2==0)
{
//b2.setClickable( false );
// Toast toast1 = Toast.makeText(getBaseContext(), "Success", 1);
// toast1.show();
if(i==0)
{
//b2.setOnClickListener(null);
b2.setEnabled(false);
//b2.setClickable(false);
int modifierY;
modifierY=-70;
Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
translateAnimation.setDuration(600);
translateAnimation.setFillEnabled(true);
MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
translateAnimation.setAnimationListener(listener);
// b2.setOnClickListener(null);
Animation translateAnimation1=new TranslateAnimation(0, 0, 0,0 );
translateAnimation1.setDuration(1000);
translateAnimation1.setFillEnabled(true);
MyAnimationListener listener1=new MyAnimationListener(main, 0,SimpleViewPagerforAndroidActivity.this);
translateAnimation1.setAnimationListener(listener1);
main.setVisibility(RelativeLayout.VISIBLE);
/* long time =AnimationUtils.currentAnimationTimeMillis();
main.invalidate();
b2.invalidate();
translateAnimation.setStartTime(time);
translateAnimation1.setStartTime(time);*/
main.startAnimation(translateAnimation1);
b2.startAnimation(translateAnimation);
i++;
// b2.setOnClickListener(this);
}
else
{
//b2.setOnClickListener(null);
b2.setEnabled(false);
//b2.setClickable(false);
int modifierY;
modifierY=-70;
Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
translateAnimation.setDuration(1000);
translateAnimation.setFillEnabled(true);
MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
translateAnimation.setAnimationListener(listener);
Animation translateAnimation1=new TranslateAnimation(0, 0, 0,-150 );
translateAnimation1.setDuration(600);
translateAnimation1.setFillEnabled(true);
MyAnimationListener listener1=new MyAnimationListener(main, -150,SimpleViewPagerforAndroidActivity.this);
translateAnimation1.setAnimationListener(listener1);
main.setVisibility(RelativeLayout.VISIBLE);
/* long time =AnimationUtils.currentAnimationTimeMillis();
main.invalidate();
b2.invalidate();
translateAnimation.setStartTime(time);
translateAnimation1.setStartTime(time);*/
main.startAnimation(translateAnimation1);
b2.startAnimation(translateAnimation);
// b2.setOnClickListener(this);
i++;
}
}
else
{
//b2.setOnClickListener(null);
b2.setEnabled(false);
//b2.setClickable(false);
Animation translateAnimation=new TranslateAnimation(0, 0, 0,150 );
translateAnimation.setDuration(1000);
translateAnimation.setFillEnabled(true);
MyAnimationListener listener=new MyAnimationListener(main, 150,SimpleViewPagerforAndroidActivity.this);
translateAnimation.setAnimationListener(listener);
Animation translateAnimation1=new TranslateAnimation(0, 0, 0,70 );
translateAnimation1.setDuration(600);
translateAnimation1.setFillEnabled(true);
MyAnimationListener listener1=new MyAnimationListener(b2, 70,SimpleViewPagerforAndroidActivity.this);
translateAnimation1.setAnimationListener(listener1);
/* long time =AnimationUtils.currentAnimationTimeMillis();
main.invalidate();
b2.invalidate();
translateAnimation.setStartTime(time);
translateAnimation1.setStartTime(time);
main.startAnimation(translateAnimation1);
b2.startAnimation(translateAnimation);*/
b2.startAnimation(translateAnimation1);
main.startAnimation(translateAnimation);
i++;
//b2.setOnClickListener(this);
}
}
});
Check on button click whether animation is completed or not, using hasEnd property, check below code:
if (!(animation.hasEnded()) {
button.setClickable(false);
}
else{
button.setClickable(true);
}
I use button.setEnabled(false) to disable Touch event.
First make the button selectable.
Then those things will work.
Button.setSelected(true);

Categories

Resources