gpt4 book ai didi

android - 动画图像并让它在最后停止?

转载 作者:行者123 更新时间:2023-11-29 02:07:31 25 4
gpt4 key购买 nike

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

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

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);
}
}

我检查了几点:

宽度 = 480

fromXDelta = 408(点击左侧时)

所以它确实没有超出边界,但是当我单击左键时,图像来自边界外??这会导致什么?提前致谢...

最佳答案

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

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);
}

这应该可以解决问题。

关于android - 动画图像并让它在最后停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9209248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com