我已經在android中創建一個視圖,我需要從底部到頂部,反之亦然動畫。我已經成功地使用TranslateAnimation來做到這一點。但問題是我在視圖上有幾個按鈕。當動畫那裏接觸點保持在原來的位置,並沒有移動到新的位置。所以當我再次單擊按鈕的原始位置時,頂部到底部的動畫將運行,但按鈕不存在。按鈕不動後動畫android
我搜索了互聯網和人們說,調用view.layout與參數,但我的問題是如何獲得視圖的最新位置,因爲我試圖獲取位置動畫開始和動畫結束和它保持不變。
另外請不要給出答案,它實際上不會移動視圖它創建一個副本並移動它等等因爲我已經搜索,但無法找到一個合適的解決方案描述或實施。
下面是代碼:
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>
所有我想要做的是創造是幾個簡單的菜單在按鈕點擊時滑入和滑出的按鈕。
no xml?沒有代碼? –
在這裏你去!我已經添加了代碼。 –
順便說一句,還有兩個其他drwable XML,我沒有提到幾乎相同與圖像被改變的差異。 –