从菜单中激活一个在Fragment中设置动画的CountDownTimer

时间:2014-01-16 18:56:22

标签: android menu android-activity android-viewpager fragment

我有一个View Pager,其中包含2个片段,这些片段有几个需要动画的量表。 我想从应用程序操作栏菜单中打开或关闭倒数计时器以设置这些仪表的动画。

附上我的代码:

FragmentActivity中的菜单项:

@Override
public boolean onCreateOptionsMenu(Menu menu)
  {
    getMenuInflater().inflate(R.menu.main, menu);
    demoItem = menu.findItem(R.id.menu_demo);
    demoItem.setChecked(false);

    return true;
  }

@Override
public boolean onOptionsItemSelected(MenuItem item)
  {

    switch (item.getItemId())
      {
      case R.id.menu_enabler:
        Intent enablerIntent = new Intent();
        PackageManager manager = getPackageManager();
        try
          {
            enablerIntent = manager
                .getLaunchIntentForPackage("com.openxc.enabler");
            enablerIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            startActivity(enablerIntent);
          }
        catch (Exception e)
          {
            String packageName = OPENXC_ENABLER;
            Uri uri = Uri.parse("market://details?id=" + packageName);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
          }
        break;

      case R.id.menu_demo:
        if (demoItem.isChecked())
          {

            demoItem.setChecked(false);
            // onDemoActivated.activateDemo(false);
          }
        else
          {
            demoItem.setChecked(true);
            // onDemoActivated.activateDemo(true);
          }
        break;
      case R.id.menu_settings:

        break;
      case R.id.menu_app_info:

        break;
      default:
        break;
      }
    return false;
  }

}

我制作片段的片段:

public class FragmentGaugeCartoon extends Fragment {
public static final String ARG_OBJECT = "object";
public static final String TAG = "GaugeCartoon";
private Context context;
private final Random RAND = new Random();
private GaugeView gauge1;
private GaugeView gauge2;
private GaugeView gauge3;

// getter for returning fragment instance
public static FragmentGaugeCartoon getInstance() {
return new FragmentGaugeCartoon();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(R.layout.fragment_gauge_cartoon,
    container, false);
gauge1 = (GaugeView) rootView.findViewById(R.id.gauge_view1);
gauge2 = (GaugeView) rootView.findViewById(R.id.gauge_view2);
gauge3 = (GaugeView) rootView.findViewById(R.id.gauge_view3);

gaugeTimer.start();
return rootView;
  }

   // dountdown timer for the demo
  private final CountDownTimer gaugeTimer = new CountDownTimer(30000, 2000) {
@Override
public void onTick(long arg0) {
    gauge1.setTargetValue(RAND.nextInt(101));
    gauge2.setTargetValue(RAND.nextInt(201));
    gauge3.setTargetValue(RAND.nextInt(2000));
}

@Override
public void onFinish() {
    gaugeTimer.start();
}
   };

  @Override
   public void onResume() {
super.onResume();

}

@Override
  public void onPause() {
super.onPause();

 }

   @Override
   public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {

} else {
}
   }
  }

1 个答案:

答案 0 :(得分:0)

Solved。我已经实现了随机数选择器。