开发者

Android animations

开发者 https://www.devze.com 2023-03-21 09:21 出处:网络
I need to specify two animations for class that extends LinearLayout: one of them is translate animation (X axis only), second is translate too, but using Y axis. The problem is that first must start

I need to specify two animations for class that extends LinearLayout: one of them is translate animation (X axis only), second is translate too, but using Y axis. The problem is that first must start when I press A button, second must start when I press button B. Here's what I've tried:

TranslateAnimation mMoveRight = new Tr开发者_运维百科anslateAnimation(Animation.RELATIVE_TO_SELF,     300, Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF);    
TranslateAnimation mMoveDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF, 50);

mMoveRight.setDuration(6000);
mMoveDown.setDuration(1000);

AnimationSet mAnimationsSet = new AnimationSet(true);

mAnimationsSet.addAnimation(mMoveRight);
mAnimationsSet.addAnimation(mMoveDown);
mAnimationsSet.setFillEnabled(true);
mAnimationsSet.setFillAfter(true);

this.setAnimation(mAnimationsSet);
  1. This is my class that extends LinearLayout.
  2. Animations start when view is drawn - I don't want it.
  3. I want to run moveRight animation by myself, same moveDown animation (but in different time than moveRight).

Would appreciate any help.


It is simple. Create two .xml files in the res/anim folder, one xml file will translate along x-axis and another xml file will translate along y-axis. Now set in the onClickListener() for button A call the first xml file and for second onClickListener() for button B call the second xml file. Though this process is a bit length. I think this is the way to achieve it.


AnimationSet combines animations and runs them in parallel. You should create 2 methods in your custom view, for example:

public void moveRight() {
    TranslateAnimation mMoveRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 300, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);   
    mMoveRight.setDuration(6000);
    mMoveRight.setFillEnabled(true);
    mMoveRight.setFillAfter(true);
    this.startAnimation(mMoveRight);
}

...

public void moveDown() {
    TranslateAnimation mMoveDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF, 50);
    mMoveDown.setDuration(6000);
    mMoveDown.setFillEnabled(true);
    mMoveDown.setFillAfter(true);
    this.startAnimation(mMoveDown);
}

Having that, you will be able to start animation (down and right animation separately) on button click.

CustomLinearLayout testLayout = ...; // initialize your layout

buttonA.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
      testLayout.moveRight();
    }
});

buttonA.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
      testLayout.moveDown();
    }
});

P.S.: I haven't tested your translate animations if they work properly, just providing you an idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消