按钮没有动画的android后工作 [英] Button does not work after animation android
问题描述
我创建android的美景,我需要从底部动画它到顶部,反之亦然。我已经成功地做到了这一点使用TranslateAnimation。但问题是,我对观的几个按键。当动画有触摸点保持在原来的地方,不会被移动到新位置。所以,当我再次点击该按钮原来现在的位置从上到下动画运行,但按钮没有present那里。
I have created a view in android and I need to animate it from bottom to top and vice versa. I have succeeded in doing this using TranslateAnimation. But the problem is that I have a few buttons on the view. WHen animated there touch point remains at the original place and doesn't get moved to the new position. So when I click the original postion of the button again the top to bottom animation runs but the button is not present there.
我在网上搜索,人们说,叫view.layout的参数,但我的问题是如何获取视图的最新情况,因为我曾试图在动画开始和结束的动画,并获取现在的位置保持不变。
I have searched the internet and people are saying that call view.layout with the parameters, but my question is how to get the latest position of the view because I have tried to fetch the postion on animation start and animation end and it remains same.
另外,请不要放弃,它实际上不动它创建一个副本的观点,并将其移动答案等等等等监守我已搜查,但找不到描述或实施一个妥善的解决办法。
Also please dont give the answer that it actually does not move the view it creates a copy and move it etc etc becuase I have searched but could not find a proper solution described or implemented.
下面是code:
import android.content.Context;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.Card_Android.R;
public class GameMenuScreenAnimated extends LinearLayout {
private final View mainView;
public GameMenuScreenAnimated(Context context,
final GameViewController dashboardVC) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
mainView = inflater.inflate(R.layout.main_menu, this);
ImageButton btn = (ImageButton) mainView.findViewById(R.id.trade);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("yaaaaaay!!!!");
}
});
slideDown(mainView);
}
public View getMainView() {
return mainView;
}
private void slideUp(final View view) {
Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.5f);
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setFillEnabled(true);
view.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
int[] startPosition = new int[2];
view.getLocationOnScreen(startPosition);
System.out.println("onAnimationStart " + startPosition[0]
+ " , " + startPosition[1]);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
int[] endPosition = new int[2];
view.getLocationOnScreen(endPosition);
System.out.println("onAnimationEnd " + endPosition[0] + " , "
+ endPosition[1]);
}
});
}
private final AnimationListener slideDownAnimationListener = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
final int left = mainView.getLeft();
final int top = mainView.getTop();
final int right = mainView.getRight();
final int bottom = mainView.getBottom();
mainView.layout(left, (int) Math.round(top + 0.25 * top), right,
(int) Math.round(bottom + 0.25 * bottom));
}
};
private final Animation slideDownAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.25f);
private void slideDown(final View view) {
slideDownAnimation.setDuration(1000);
slideDownAnimation.setFillAfter(true);
slideDownAnimation.setFillEnabled(true);
slideDownAnimation.setAnimationListener(slideDownAnimationListener);
view.startAnimation(slideDownAnimation);
}
}
中的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="bottom"
>
<LinearLayout
android:id="@+id/tableLayout1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center"
>
<ImageButton
android:id="@+id/trade"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="0dp"
android:background="@null"
android:scaleType="centerCrop"
android:src="@drawable/upgrade_btn" />
</LinearLayout>
<LinearLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/evolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@null"
android:scaleType="centerCrop"
android:src="@drawable/sell_btn" />
<ImageButton
android:id="@+id/trade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@null"
android:scaleType="centerCrop"
android:src="@drawable/upgrade_btn"
/>
</LinearLayout>
</LinearLayout>
提拉XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/evolution">
<item android:state_pressed="true" android:drawable="@drawable/menu_off" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/menu_on" /> <!-- focused -->
<item android:drawable="@drawable/menu_on" /> <!-- default -->
</selector>
所有我想要做的是创造与该幻灯片和缩小按钮点击几个按钮简单的菜单。
All I want to do is to create is a simple menu with few buttons which slides in and out on the button click.
推荐答案
我遇到了同样的问题,前一阵子。您会在这里找到我的答案:
I ran into the same problem a while ago. You'll find my answer here:
<一个href=\"http://stackoverflow.com/questions/4397443/how-can-i-apply-the-changes-to-the-position-of-a-view-after-an-animation/7929404#7929404\">How动画后,我可以申请更改视图的位置?
的另一种方法是使用动画() - 功能或ObjectAnimator。这两个动画这些方式影响视图对象本身,而不仅仅是动画在屏幕上的像素,而无需更新视图的下落的内部值。您在此处找到有关这些功能的详细信息:
Another way is to use the animate()-Function or an ObjectAnimator. Both of these ways of animation affect the view-object itself, instead of only animating the pixels on the screen without updating the internal values of the whereabouts of the view. You find more info about these functions here:
动画()(又称ViewPropertyAnimator):
的http://developer.android.com/guide/topics/graphics/prop-animation.html#view-prop-animator
animate() (a.k.a. ViewPropertyAnimator): http://developer.android.com/guide/topics/graphics/prop-animation.html#view-prop-animator
ObjectAnimator:的http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator
ObjectAnimator: http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator
整个动画章是一个很好的阅读。它可能需要20分钟,但在这之后,你有什么事情与动画在Android上的一个更好的图片。
The whole Animation-chapter is a very good read. It takes maybe 20 minutes, but after that you have a much better picture of what's going on with animations on Android.
这篇关于按钮没有动画的android后工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!