Android按钮动画进入PopupView

时间:2012-12-02 15:53:30

标签: android animation button

我的PopupView中有一个LinearLayout,还有一个进入此LinearLayout的按钮。此按钮的事件监听器正常工作,但按下(突出显示)的动画未启动。我做错了什么?请,gelp!

    public class PopupView extends View 
{
    private PopupWindow popUp;
    private LinearLayout popUpLayout;
    private TextView titleTextView;
    private TextView lengthTextView;
    private TextView heighTextView;
    private TextView percentTextView;
    private Button buttonOk;
    private Button buttonDelete;
    private EditText edTextLength, edTextHeight;
    private OnClickListener onClick;
    private LayoutParams params;
    private int popUpWidth = 0;
    private int popUpHeight = 0;
    private int lastPopupped;
    private boolean isCreated = false;
    private int popUpX, popUpY;

    public static final int POINT_MOVE = 1;

    public PopupView(Context context)
    {
        super(context);
        popUp = new PopupWindow(context);
        popUpLayout = new LinearLayout(context);
        titleTextView = new TextView(context);
        lengthTextView  = new TextView(context);
        heighTextView  = new TextView(context);
        percentTextView = new TextView(context);
        buttonOk = new Button(context);
        buttonDelete = new Button(context);
        titleTextView.setTextColor(Color.BLACK);
        lengthTextView.setTextColor(Color.BLACK);
        heighTextView.setTextColor(Color.BLACK);
        percentTextView.setTextColor(Color.BLACK);
        buttonOk.setText("OK");
        buttonOk.setId(1);
        buttonDelete.setText("Delete");
        buttonDelete.setId(2);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        popUpLayout.setClickable(true);
        popUpLayout.setOrientation(LinearLayout.VERTICAL);

        popUp.setContentView(popUpLayout);
    }

    public void createPopup (View parent, int typeOfPopUp, DataContainer dContainer, int numOfPoint, int statusBar, 
            float scaleFactor, int scrollX, int scrollY)
    {
        switch (typeOfPopUp)
        {       
        case 1: 
            titleTextView.setText("Ground Point # " + (1 + numOfPoint));
            lengthTextView.setText("Length = " + dContainer.groundPoints.get(numOfPoint).length);
            heighTextView.setText("Heigh = " + dContainer.groundPoints.get(numOfPoint).heigh);

            onClick = new OnClickListener()
            {
                public void onClick (View v)
                {
                    switch (v.getId())
                    {
                    case 1:
                        System.out.println("OK pressed!");
                        break;

                    case 2:

                        break;
                    }
                }
            };

           buttonOk.setOnClickListener(onClick);
           buttonOk.setFocusable(true);
           buttonDelete.setOnClickListener(onClick);

            popUpLayout.removeAllViews();
            popUpLayout.addView(titleTextView, params);
            popUpLayout.addView(lengthTextView, params);
            popUpLayout.addView(heighTextView, params);
            buttonDelete.setEnabled(true);
            popUpLayout.addView(buttonDelete, params);
            popUpLayout.addView(buttonOk, params);


            popUpWidth = (int)(titleTextView.getTextSize() / 2 * 18);
            popUpHeight = (int)(titleTextView.getTextSize() * 13);

            popUpX = (int)((dContainer.groundPoints.get(numOfPoint).length) * scaleFactor - scrollX);
            popUpY = (int)((dContainer.groundPoints.get(numOfPoint).heigh) * scaleFactor + statusBar - scrollY);

            popUpX += statusBar;
            popUpY += statusBar;

            popUp.showAtLocation(parent, Gravity.NO_GRAVITY, popUpX, popUpY);
            popUp.update(popUpX, popUpY, popUpWidth, popUpHeight);

            lastPopupped = numOfPoint;
            isCreated = true;
            break;
        }
    }

    public void removePopup ()
    {
        if (isCreated)
        {
            popUp.dismiss();
        }
    }

    public void updatePopup (DataContainer dContainer, int statusBar, 
            float scaleFactor, int scrollX, int scrollY)
    {
        if (isCreated)
        {
            popUp.update((int)((dContainer.groundPoints.get(lastPopupped).length) * scaleFactor - scrollX),
                    (int)((dContainer.groundPoints.get(lastPopupped).heigh) * scaleFactor + statusBar - scrollY),
                    popUpWidth, popUpHeight);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

默认情况下,PopupView不可聚焦。需要调用方法popup.setFocusable(true)。谢谢大家!