动画图像并使其在最后停止?

发布于 2025-01-03 22:39:54 字数 2112 浏览 0 评论 0原文

我正在尝试制作一个动画,单击右键将图像从布局的右侧滑动到布局的右侧,单击左侧的按钮将图像从布局的左侧滑动。

到目前为止我已经这样做了:

public class DenemeActivity extends Activity implements View.OnClickListener {

Button LeftSlide, RightSlide;
View image;
int width;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LeftSlide = (Button) findViewById(R.id.slideLeft);
    RightSlide = (Button) findViewById(R.id.slideRight);
    image = (View) findViewById(R.id.imageV);
    width = getWindowManager().getDefaultDisplay().getWidth(); ;
    LeftSlide.setOnClickListener(this);
    RightSlide.setOnClickListener(this);      

}

public void onClick(View v) {
    runOnAnimation(v.getId(), image);
}

void runOnAnimation(final int id, final View view) {

    AnimationSet animation = new AnimationSet(true);
    TranslateAnimation translateAnimation = null;

    int fromXDelta = view.getLeft();

    if(id == LeftSlide.getId())
        translateAnimation = new TranslateAnimation(fromXDelta, 0, 0, 0);
    else if(id == RightSlide.getId())
        translateAnimation = new TranslateAnimation(fromXDelta, width - view.getWidth() - fromXDelta , 0, 0);

    translateAnimation.setDuration(1000);
    animation.addAnimation(translateAnimation);
    animation.setAnimationListener(new AnimationListener() {

        public void onAnimationEnd(Animation v) {
            v.reset();
            view.clearAnimation();

            // Change view to state B by modifying its layout params and scroll
            if(id == LeftSlide.getId())
                view.layout(0, 0, view.getWidth(), view.getHeight());
            else if(id == RightSlide.getId())
                view.layout(width - view.getWidth(), 0, width, view.getHeight());               
        }

        public void onAnimationRepeat(Animation v) {}
        public void onAnimationStart(Animation v) {}
    });
    view.startAnimation(animation);
}    
}

我已经检查了这些点:

width = 480

fromXDelta = 408(当单击左侧时)

所以它确实没有超出边界,但是当我单击左侧按钮时,图像来自边界外??这能造成什么后果呢? 提前致谢...

I'm trying to do an animation which on click right button slides an image from where it is to right of layout, and on click left button slides the image from where it is to left of layout.

I've done this so far:

public class DenemeActivity extends Activity implements View.OnClickListener {

Button LeftSlide, RightSlide;
View image;
int width;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LeftSlide = (Button) findViewById(R.id.slideLeft);
    RightSlide = (Button) findViewById(R.id.slideRight);
    image = (View) findViewById(R.id.imageV);
    width = getWindowManager().getDefaultDisplay().getWidth(); ;
    LeftSlide.setOnClickListener(this);
    RightSlide.setOnClickListener(this);      

}

public void onClick(View v) {
    runOnAnimation(v.getId(), image);
}

void runOnAnimation(final int id, final View view) {

    AnimationSet animation = new AnimationSet(true);
    TranslateAnimation translateAnimation = null;

    int fromXDelta = view.getLeft();

    if(id == LeftSlide.getId())
        translateAnimation = new TranslateAnimation(fromXDelta, 0, 0, 0);
    else if(id == RightSlide.getId())
        translateAnimation = new TranslateAnimation(fromXDelta, width - view.getWidth() - fromXDelta , 0, 0);

    translateAnimation.setDuration(1000);
    animation.addAnimation(translateAnimation);
    animation.setAnimationListener(new AnimationListener() {

        public void onAnimationEnd(Animation v) {
            v.reset();
            view.clearAnimation();

            // Change view to state B by modifying its layout params and scroll
            if(id == LeftSlide.getId())
                view.layout(0, 0, view.getWidth(), view.getHeight());
            else if(id == RightSlide.getId())
                view.layout(width - view.getWidth(), 0, width, view.getHeight());               
        }

        public void onAnimationRepeat(Animation v) {}
        public void onAnimationStart(Animation v) {}
    });
    view.startAnimation(animation);
}    
}

I've checked the points:

width = 480

fromXDelta = 408 (when click on left)

So it's not out of border indeed, but when i click on left button, the image comes from out of border ?? What can it cause to this?
Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-01-10 22:39:54

我认为您想要移动的视图的位置类型错误。
尝试获取视图的位置并按如下方式实现翻译:

int fromPos = view.getLeft();
int toPos   = 0;
if(id == LeftSlide.getId()) {
  translateAnimation = new TranslateAnimation(fromPos, toPos, 0, 0);
} else {
  toPos = getWidth()-view.getWidth();
  translateAnimation = new TranslateAnimation(fromPos, toPos, 0, 0);
}

这应该可以解决问题。

I think you got the wrong position type for the View you want to move.
Try to get the position of your View and implement the translation as follows:

int fromPos = view.getLeft();
int toPos   = 0;
if(id == LeftSlide.getId()) {
  translateAnimation = new TranslateAnimation(fromPos, toPos, 0, 0);
} else {
  toPos = getWidth()-view.getWidth();
  translateAnimation = new TranslateAnimation(fromPos, toPos, 0, 0);
}

This should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文