Android Animation : View directly change to end position - android

I get an assignment to create a game like candy crush saga and flow for android platform. I've try some tutorial about event listener and animation, it work well but I got problem when I animate ImageView to change its position.
I want to make change position between two ImageView when I swipe on of ImageView (candy crush gameplay), it was changed position perfectly but the problem is the animation movement is not working. When I swipe one of the ImageView (Right swipe for example), that ImageView changed position with ImageView which located beside it but I don't see movement animation like when I play candy crush saga.
I use 2 ObjectAnimator to animate that ImageView and play it together when Swipe Event was trigger on that ImageView. this is my animation code.
final String name = "x";
ImageView target = (ImageView) findViewById(id[x-1][y-1]);
ImageView next = (ImageView) findViewById(id[x-1][y]);
ObjectAnimator aTarget = ObjectAnimator.ofFloat(target, name, target.getX(), (target.getX()+target.getWidth()));
aTarget.setDuration(1000);
aTarget.setInterpolator(new LinearInterpolator());
ObjectAnimator aNext = ObjectAnimator.ofFloat(next, name, next.getX(), (next.getX()-next.getWidth()));
aNext.setDuration(1000);
aNext.setInterpolator(new LinearInterpolator());
AnimatorSet moveset = new AnimatorSet();
moveset.playTogether(aTarget, aNext);
moveset.start();
I use ID array to save my ImageView's ID and I using 'for' to compare View's ID which trigger swipe event with list ID inside ID array so I can find where ImageView which choosen to be moved (target) and other ImageView which will changed position with choosen one.
I don't understand what wrong with that code, I get that code from animation tutorial source code from
http://www.101apps.co.za/articles/a-property-animation-tutorial.html
It works perfectly when I run the application but when I try to implement it to my application I got this problem.
Hope somebody can help me, Thanks so much before.

Related

Custom textview with rotate, zoom in,zoom out and move on touch in android?

Any buddy have Custom Text View with Rotate, Zoom In, Zoom Out & Move functionality like MutiTouch Image View like this http://judepereira.com/blog/multi-touch-in-android-translate-scale-and-rotate/ in android?
I Want exactly like this screen shots.
1. In that screen draw simple text.
2. In that screen when i zoom this view Text auto wrap.
3. In that screen Move Text on view.
4. In that screen Rotate Text.
This all functionality do on TextView touch.
I used https://github.com/jcmore2/Collage to achieve a rotated textview
but i changed the whole code since the plugin works with images
CardView extends ImageView
i chnaged to MyCardView extends RelativeLayout then removed all the code related to images
and instead of using collage.createCollageResources(listRes);
i created my own function in my own CollageView class
public void createCollage(List<MyCardView> list) {
for (MyCardView layout : list) {
addCard(layout);
}
}
Now in the Activity you can add to the collageview a complete RelativeLayout with it's children not just images
this also will allow dealing with single view or multiple .. TextView or any thing else .. finally this will make the view not just rotated but dragged and zoomed with multi touch gesture.
Yes you can create it see below image and follow Here.
ImageView: To add ImageView
// add a stickerImage to canvas
StickerImageView iv_sticker = new StickerImageView(MainActivity.this);
iv_sticker.setImageDrawable(getResources().getDrawable(R.drawable.c10));
canvas.addView(iv_sticker);
TextView: To add TextView
// add a stickerText to canvas
StickerTextView tv_sticker = new StickerTextView(MainActivity.this);
tv_sticker.setText("nkDroid");
canvas.addView(tv_sticker);
However, build this did an awesome job.

Android animation only works once?

So i'm trying to do 2 animations simultaneously, one to move a textview, and one to show a linearlayout (also 2 animations to hide them). I have another animation working as intended to show/hide a seperate layout. When i execute showing the view with 2 animations, it works once, it hides fine, but then doesn't work again. Then when i show the other view, it plays all 3 animations (not intended). I can't figure out why this is happening? When i try to show the 2 animations it does nothing, but then when i try the other show view its like it was added to a queue and it shows all 3.
My code for initiating the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_show);
anim.setAnimationListener(new AnimListener(layoutMsgs, View.VISIBLE)); // sets visibility on animation end
layoutMsgs.startAnimation(anim);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation tvAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_show);
tvMsgs.startAnimation(tvAnim);
My code for hiding the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation animLayout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_hide);
animLayout.setAnimationListener(new AnimListener(layoutMsgs, View.INVISIBLE));
layoutMsgs.startAnimation(animLayout);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation animMsgs = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_hide);
tvMsgs.startAnimation(animMsgs);
Then this is the other animation that is working fine, it's only one animation, no textView, just a layout
LinearLayout pokeLayout = (LinearLayout)findViewById(R.id.layoutPokes);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.poke_show);
anim.setAnimationListener(new AnimListener(pokeLayout, View.VISIBLE));
pokeLayout.startAnimation(anim);
So how can i fix this? Sorry if my explanation is bad i'm finding it difficult to explain all the details, please ask for any missing information.
solved by using solution in this post Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension
wasn't pretty as I had to create seperate translate X and translate Y res files for each view I need to move.. ended up with like 15 anim files. Would still like to know if theres a better solution

get the position of a clicked button in Linear layout

So I am working on a linear layout where i add and remove Imagebuttons dynamically. So everytime an imagebutton is clicked and an action is taken, the imagebutton gets removed and changed with another imagebutton/s. The thing is i'd like to maintain the position of the clicked buttons so i can add a new imagebutton at the same location.
final ViewGroup.LayoutParams lpiv = iv.getLayoutParams();
il.removeView(iv);
int ilBtn = R.drawable.ta;
iv3.setImageResource(ilBtn);
iv5.setImageResource(ilBtn);
iv3.setClickable(false);
iv.setClickable(false);
il.addView(iv5, lpiv);
il.addView(iv3, lpiv);
//iv3.setLayoutParams(lpiv);
Correct me, but wouldnt it be easier just to change the image of the ImageButton instead
of adding a new button? To distinguish the status just check the image or a status variable.
customImageButton.setBackgroundResource(R.drawable.imageName);
this.status = this.NEW_STATUS;
Use LinearLayout.indexOfChild(View child) before you call remove, then call addView(View child, int index) with that index.
Is the code pasted in the question the body of your OnClickListener? If so, just modify it to do something like the following before you call removeView():
float originalX = view.getX();
float originalY = view.getY();
where view is the reference to the ImageButton that's being clicked, and then call setX() and setY() on your new buttons.
Just a heads-up, this will only work if the button being clicked and the ones being added are the same height and with. If they're not, you'll have to do some calculations based on the X/Y of the original one versus the height/width difference to find the same x,y point.

ObjectAnimator in android

I want to show a objectanimator in android for a imageview.This imageview should move from top of the screen to about 30 pixels down from top of the screen.Once it(imageview) reaches there and onclick of the imageview the image view should disappear.I have read that i cannot use translate animation as the onclick event wont get fired and hence i will have to use Objectanimator.
Can somebody please help me out in this ...stuck up for a long time now.
Following is my code i have used however it does not seem to work also i do not understand what parameters(for that points) i need to pass.
ObjectAnimator scaleXOut = ObjectAnimator.ofFloat(tv, "translationX", 1f, 9f);
AnimatorSet set = new AnimatorSet();
set.play(scaleXIn);
set.setDuration(1000);
set.start();
you can use translateAnimation and add an animationlistener so when animation ends its onAnimationEnd() method will fired up, so inside it you can put the codes that was in onclick method, or you can put an if statement inside your onclicked method to check whether animation ends, for more about animations you can check out these resources
http://www.youtube.com/playlist?list=PLxLa2tjhVPOZ9KMqanm12_xPDBjKPZWgM
and
http://www.vogella.com/articles/AndroidAnimation/article.html

Android animation moves the Layout, but not the buttons

Say I have a "main screen" Layout which has to move up, to reveal a "history bar".
if ( HistoryBar == false )
{
HistoryBar = true;
TranslateAnimation MainScreenPullUp = new TranslateAnimation(0, 0, 0, -200 );
MainScreenPullUp.setDuration(350);
MainScreenPullUp.setFillAfter(true);
LinLayMain.startAnimation(MainScreenPullUp);
}
Right now I am doing it like that. But the problem is, that while the main screen layout itself moves up, all the button places keep the same, i.e. the button picture moves, but the click-spot remains in place. Any ideas how to fix that?
I tried useing an animationListener, instead setFillAfter(true) , which would TanslateY the main screen layout, once the animation stops, but it makes the layout jump and flicker. Are there any other ways?
edit:
Thanks, Vineet.
Got it working with:
ObjectAnimator pullUp = ObjectAnimator.ofFloat(LinLayMain, "translationY", 0f, -200f);
pullUp.setDuration(350);
pullUp.start();
I faced the same problem. From 3.0 android has property animation. Below 3.0 I tackled this issue with the help of view's method offsetLeftAndRight (int offset) and offsetTopAndBottom (int offset). You can refer this link for more details.
For Property animation refer this link.

Categories

Resources