在android中如何一个接一个地执行任务

时间:2015-02-05 04:09:12

标签: 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;  
        Context ctx;

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

    EditText name1,name2;

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

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

        Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {

               pd.show();
            }
        });


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



    }



    @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);

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



                }

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


                     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);
                     mChartView = ChartFactory.getPieChartView(ctx, mSeries, mRenderer); 

                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);

                          name1=(EditText) findViewById(R.id.ext1);
                          name2=(EditText) findViewById(R.id.ext2);
                          x1=name1.getText().toString();
                          y1=name2.getText().toString();
                          NAME_LIST = new String[] { x1 , y1 };
                          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
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub


                }


    });
    }
    }
}

但问题是当我打开程序时,动画正在运行并且正在运行。结果不会到来。 我的要求是第一个动画然后结果..但只有动画正在运行..我的代码中的问题在哪里?提前谢谢

我编辑的代码是:---

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;  
    Context ctx;

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

     EditText name1,name2;

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

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

        Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {

               pd.show();
            }
        });


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



    }



    @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);

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



                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //here display data
                     h.postDelayed(r, 100);
                     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);
                     mChartView = ChartFactory.getPieChartView(ctx, mSeries, mRenderer); 

                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);

                          name1=(EditText) findViewById(R.id.ext1);
                          name2=(EditText) findViewById(R.id.ext2);
                          x1=name1.getText().toString();
                          y1=name2.getText().toString();
                          NAME_LIST = new String[] { x1 , y1 };
                          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
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub


                }



    });

    }
    }
}

1 个答案:

答案 0 :(得分:1)

在调用iv.startAnimation(anim)之前尝试调用anim.setAnimationListener();