0

我是android编程的新手。在这里,我有一个带有后退、快速菜单和确定按钮的页脚。通过按下菜单按钮,我希望另一个线性布局向上滑动。当它再次按下时,它会向下滑动。

基本活动:

package com.app.getconnected.activities;

import com.app.getconnected.R;
import com.app.getconnected.animations.CollapseAnimation;
import com.app.getconnected.animations.ExpandAnimation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

abstract class BaseActivity extends Activity {

protected TextView txtHeading;
protected Button buttonBack;
private Button buttonMenu;
protected Button buttonOk;
protected Button buttonHome;

private LinearLayout MenuList;
private int screenHeight;
private boolean isExpanded;

protected static final String activityPackage = "com.app.getconnected.activities";
protected static Boolean loggedIn=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.footer);

}


protected void initLayout(int resId, boolean homeButton,
        boolean backButton, boolean menuButton, boolean okButton) {

    if (txtHeading == null)
        txtHeading = (TextView) findViewById(R.id.header_text);
    if (txtHeading != null)
        txtHeading.setText(resId);

    buttonHome = (Button) findViewById(R.id.header_button_home);
    buttonBack = (Button) findViewById(R.id.footer_button_back);
    buttonMenu = (Button) findViewById(R.id.footer_button_menu);
    buttonOk = (Button) findViewById(R.id.footer_button_ok);


    buttonBack.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            goBack();
        }
    });

    buttonMenu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            handleMenu();
        }
    });


    buttonHome.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(BaseActivity.this,
                    MainActivity.class);
            startActivityForResult(intent, 1);
            finish();
        }
    });

    this.buttonHome.setVisibility(homeButton ? View.VISIBLE
            : View.INVISIBLE);
    this.buttonBack.setVisibility(backButton ? View.VISIBLE
            : View.INVISIBLE);
    this.buttonMenu.setVisibility(menuButton ? View.VISIBLE
            : View.INVISIBLE);
    this.buttonOk.setVisibility(okButton ? View.VISIBLE : View.INVISIBLE);
}

protected void goBack() {
    super.onBackPressed();
}

private void handleMenu() {
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    screenHeight = metrics.heightPixels;

    buttonMenu.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                System.out.println("Quick Menu clicked");

                if (isExpanded) {
                    isExpanded = false;
                    MenuList.startAnimation(new CollapseAnimation(MenuList, 0,         (int)(screenHeight*0.7), 20));
                }else {
                    isExpanded = true;
                    MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenHeight*0.7), 20));
                }
            }
    });
}


protected void disableBackButton() {

}

/**
 *
 * @param view
 */
public void startIntentByButton(View view) {
    Button button = (Button) view;
    if(!button.getTag().equals("")) {
        try {
            Intent intent = new Intent(getApplicationContext(), Class.forName(BaseActivity.activityPackage + "." + button.getTag().toString()));
            startActivityForResult(intent, 1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

}

这是两个动画类。

坍塌:

package com.app.getconnected.animations;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;

public class CollapseAnimation extends Animation implements Animation.AnimationListener     {

private View view;
private static int ANIMATION_DURATION;
private int LastWidth;
private int FromWidth;
private int ToWidth;
private static int STEP_SIZE=30;

public CollapseAnimation(View v, int FromWidth, int ToWidth, int Duration) {

    this.view = v;
    LayoutParams lyp =  view.getLayoutParams();
    ANIMATION_DURATION = 1;
    this.FromWidth = lyp.height;
    this.ToWidth = lyp.height;
    setDuration(ANIMATION_DURATION);
    setRepeatCount(20);
    setFillAfter(false);
    setInterpolator(new AccelerateInterpolator());
    setAnimationListener(this);
}

@Override
public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub
    LayoutParams lyp =  view.getLayoutParams();
    lyp.height = lyp.height - ToWidth/20;
    view.setLayoutParams(lyp);
}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub
    LayoutParams lyp =  view.getLayoutParams();
    LastWidth = lyp.height;
}

}

延长:

package com.app.getconnected.animations;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.Toast;

public class ExpandAnimation extends Animation implements Animation.AnimationListener {

private View view;
private static int ANIMATION_DURATION;
private int LastWidth;
private int FromWidth;
private int ToWidth;
private static int STEP_SIZE=30;

public ExpandAnimation(View v, int FromWidth, int ToWidth, int Duration) {

    this.view = v;
    ANIMATION_DURATION = 1;
    this.FromWidth = FromWidth;
    this.ToWidth = ToWidth;
    setDuration(ANIMATION_DURATION);
    setRepeatCount(20);
    setFillAfter(false);
    setInterpolator(new AccelerateInterpolator());
    setAnimationListener(this);
}

@Override
public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub
    LayoutParams lyp =  view.getLayoutParams();
    lyp.height = LastWidth += ToWidth/20;
    view.setLayoutParams(lyp);
}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub
    LayoutParams lyp =  view.getLayoutParams();
    lyp.height = 0;
    view.setLayoutParams(lyp);
    LastWidth = 0;

}
}

日志猫:

10-24 09:08:52.210: D/dalvikvm(1095): GC_FOR_ALLOC freed 159K, 9% free 2860K/3120K,  paused 41ms, total 59ms
10-24 09:08:52.350: D/gralloc_goldfish(1095): Emulator without GPU emulation detected.
10-24 09:08:56.489: D/dalvikvm(1095): GC_FOR_ALLOC freed 50K, 5% free 3321K/3472K,   paused 51ms, total 68ms
10-24 09:09:14.550: I/System.out(1095): Quick Menu clicked
10-24 09:09:14.580: D/AndroidRuntime(1095): Shutting down VM
10-24 09:09:14.580: W/dalvikvm(1095): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-24 09:09:14.680: E/AndroidRuntime(1095): FATAL EXCEPTION: main
10-24 09:09:14.680: E/AndroidRuntime(1095): java.lang.NullPointerException
10-24 09:09:14.680: E/AndroidRuntime(1095):     at com.app.getconnected.activities.BaseActivity$4.onClick(BaseActivity.java:109)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at android.view.View.performClick(View.java:4240)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at android.view.View$PerformClick.run(View.java:17721)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at android.os.Handler.handleCallback(Handler.java:730)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at android.os.Looper.loop(Looper.java:137)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at java.lang.reflect.Method.invoke(Method.java:525)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 09:09:14.680: E/AndroidRuntime(1095):     at dalvik.system.NativeStart.main(Native Method)
10-24 09:11:49.983: I/Process(1095): Sending signal. PID: 1095 SIG: 9
10-24 09:11:52.441: D/gralloc_goldfish(1125): Emulator without GPU emulation detected.

非常欢迎任何帮助或建议,

丹尼尔

4

1 回答 1

2

就像@Mike 在评论中所说,您需要启动 MenuList 对象。

MenuList = (LinearLayout)this.findViewById(R.id.menu_list_layout);

忽略this尤其是如果您将此行放在onClick. 将线放在您启动按钮的位置,您可以删除this

于 2013-10-24T13:59:39.083 回答