动画侦听器无法在FragmentActivity中工作

时间:2014-08-13 05:55:13

标签: android android-animation

我有一个动画自定义抽屉菜单类,来自5个不同的活动,其中4个工作正常,但在其中一个我使用FragmentActivity而不是Activity没有工作。

FragmentActivity中菜单的行为很奇怪,在调试模式下代码运行良好但是抽屉菜单类似乎没有UIthread,即使我正在使用runOnUiThread。

我在每个onCreate()的{​​{1}}方法上初始化自定义抽屉类,然后在按钮单击中调用Drawer类中的Activity方法。

MainActivity类:

DraverVisibility()

类DraverMenu:

public class MainActivity extends FragmentActivity {
 DraverMenu DraverMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DraverMenu = new DraverMenu(this,
                findViewById(android.R.id.content),
                findViewById(R.id.draver_l1),
                findViewById(R.id.activity_main_app_name),
                findViewById(R.id.activity_main_app_icon),
                findViewById(R.id.draver_change_language));

...

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
    DraverMenu.DraverVisibility();
    return false;
}

DrawerMenu的XML在MainActivity XML中发布 DrawerMenu XML:

public class DraverMenu {
    private final Activity A;
    private final int LEFT_DRAVER_MARGIN = 220;
    private final int TOP_DRAVER_MARGIN = 45;
    private final int OPEN_DRAVER_DURATION = 300;
    private final int CLOSE_DRAVER_DURATION = 200;
    private float left_px;
    private float top_px;
    private LinearLayout draver;
    private View content;


public DraverMenu(Activity a, View content, View draver,
                  final View name, View icon, View language) {
    this.A = a;
    this.draver = (LinearLayout) draver;
    left_px = ...;
    top_px = ...;
    FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) draver.getLayoutParams();
    // if api<
    parm.gravity = Gravity.TOP;
    parm.leftMargin = -(int) left_px;
    parm.topMargin = (int) top_px;
    draver.setLayoutParams(parm);
    this.content = content;
...
}


public void DraverVisibility() {
    int vis = draver.getVisibility();
    if (vis == View.VISIBLE) {
        TranslateAnimation movetoleft = new TranslateAnimation(0, -left_px, 0, 0);
        movetoleft.setDuration(CLOSE_DRAVER_DURATION);

        movetoleft.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams)
                        draver.getLayoutParams();
                // if api<
                parm.gravity = Gravity.TOP;
                parm.leftMargin = -(int) left_px;
                parm.topMargin = (int) top_px;
                draver.setLayoutParams(parm);
                draver.setVisibility(View.INVISIBLE);
                draver.setAnimation(null);
                draver.invalidate();
                content.setAnimation(null);
                content.invalidate();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        draver.startAnimation(movetoleft);
    }
    if (vis == View.INVISIBLE) {
        InputMethodManager imm = (InputMethodManager) A.getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(draver.getWindowToken(), 0);

        TranslateAnimation movetoRight = new TranslateAnimation(0, left_px, 0, 0);
        movetoRight.setDuration(OPEN_DRAVER_DURATION);
        movetoRight.setFillEnabled(true);

        movetoRight.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                draver.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                FrameLayout.LayoutParams parm = (FrameLayout.LayoutParams) draver.getLayoutParams();
                // if api<
                parm.gravity = Gravity.TOP;
                parm.leftMargin = 0;  //-(int)left_px;
                parm.topMargin = (int) top_px;
                draver.setLayoutParams(parm);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        draver.startAnimation(movetoRight);
    }
 }
}

任何建议?

0 个答案:

没有答案