在android中顺序执行两个任务

时间:2015-02-04 12:26:06

标签: android animation

在我的课程中,基本是:

当程序打开时会显示一个动画,然后它会显示饼图结果。

我的代码是:

public class Popup_animation11 extends Activity {

    private static int[] COLORS = new int[] { Color.MAGENTA, Color.CYAN };  
    LinearLayout layout;

    private CategorySeries mSeries = new CategorySeries("");  
    private DefaultRenderer mRenderer = new DefaultRenderer();  
    private GraphicalView mChartView;  


    private TransparentProgressDialog pd;
    private Handler h;
    private Runnable r;

     EditText name1,name2;


    private  int[] VALUES = {40 , 60 };
     String x1,y1;

     String[] NAME_LIST=new String[] { "mangoes","apples"};


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_main);

        h = new Handler();
        pd = new TransparentProgressDialog(this, R.drawable.uktrafficlights);
        r =new Runnable() {
            @Override
            public void run() {
                if (pd.isShowing()) {
                    pd.dismiss();
                }
            }
        };

        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));  
        mRenderer.setChartTitleTextSize(20);  
        mRenderer.setLabelsTextSize(15);  
        mRenderer.setLegendTextSize(15);  
        mRenderer.setMargins(new int[] { 20, 30, 15, 0 });  
        mRenderer.setZoomButtonsVisible(true);  
        mRenderer.setStartAngle(90);

        final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);

        mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer); 


        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {

       pd.show();
    h.postDelayed(r,5000);


   LayoutInflater layoutInflater    = (LayoutInflater)getBaseContext()  .getSystemService(LAYOUT_INFLATER_SERVICE);  


       View popupView = layoutInflater.inflate(R.layout.main_piechart, null);  
             final PopupWindow popupWindow = new PopupWindow( popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
             layout = (LinearLayout)popupView.findViewById(R.id.chart); 
             layout.addView(mChartView);

             for (int i = 0; i < VALUES.length; i++) {  

                    mSeries.add(NAME_LIST[i] + "(" + VALUES[i]+"%)", VALUES[i]);
                    SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();  
                 renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);  
                 mRenderer.addSeriesRenderer(renderer);  
                 }  

                 if (mChartView != null) {  

                    mChartView.repaint();  

                 }  


             Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
             btnDismiss.setOnClickListener(new Button.OnClickListener(){

     @Override
     public void onClick(View v) {

      popupWindow.dismiss();
     }});

             popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

   }});
    }



    @Override
    protected void onDestroy() {
        h.removeCallbacks(r);
        if (pd.isShowing() ) {
            pd.dismiss();
        }
        super.onDestroy();
    }

    private class TransparentProgressDialog extends Dialog {

        private ImageView iv;

        public TransparentProgressDialog(Context context, int resourceIdOfImage) {
            super(context, R.style.TransparentProgressDialog);
            WindowManager.LayoutParams wlmp = getWindow().getAttributes();
            wlmp.gravity = Gravity.CENTER_HORIZONTAL;
            getWindow().setAttributes(wlmp);
            setTitle(null);
            setCancelable(false);
            setOnCancelListener(null);
            LinearLayout layout = new LinearLayout(context);
            layout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            iv = new ImageView(context);
            iv.setImageResource(resourceIdOfImage);
            layout.addView(iv, params);
            addContentView(layout, params);
        }


        @Override
        public void show() {
            super.show();
            RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
            anim.setInterpolator(new LinearInterpolator());
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(3000);
            iv.setAnimation(anim);
            iv.startAnimation(anim);
        }
    }

}

但问题是当我打开程序时,动画正在运行并且后台结果也同时显示。我想一个接一个。第一个动画,然后是结果。我的代码中的问题在哪里?

1 个答案:

答案 0 :(得分:4)

您需要使用animation listener。写下以onAnimationEnd方法显示数据的逻辑。

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

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        //here display data
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

希望这会有所帮助:)