2014-03-27 4 views
-4

회전하는 화살표를 만들려고합니다. 그게 onclick 회전.임의의 시간이 지나면 회전하는 이미지

랜덤 시간 후에 멈 춥니 다.

하드 비트 임의의 위치에서 중지 할 화살표 이미지를 얻는 것입니다. 여기

지금까지 내 코드입니다 :

private Button btnRotate, btnstop; 
private ImageView imgview; 
AnimationListener listener; 
Random rand = new Random(); 
int milis = (rand.nextInt(10) + 2)*1000; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_what, container, false); 
    TextView txt = (TextView) rootView.findViewById(R.id.textView1); 
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/harabara.ttf"); 
    txt.setTypeface(font); 

    final ImageView iv = (ImageView) rootView.findViewById(R.id.image_arrow); 

    btnRotate = (Button) rootView.findViewById(R.id.start); 

    final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.button_rotate); 
    rotation.setRepeatCount(Animation.INFINITE); 
    rotation.setAnimationListener(listener); 
    btnRotate.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      iv.startAnimation(rotation); 

      new java.util.Timer().schedule( 
        new java.util.TimerTask() { 
         @Override 
         public void run() { 
         iv.clearAnimation(); 
         } 
       }, milis); 

    } 
}); 





    return rootView; 
} 
+0

나중에 당신이 할 수있는 내가 오래 전' – MilapTank

+0

Timer.schedule (taskPlaySound를 대답을 게시 할 예정입니다 잠시 솔루션 대기를() (이 5000 + rnd.nextInt (45000))' –

답변

1

받기 무작위로 시간 :

: 당신에게 5 및 50 옆를 사용하여 A의 TimerTask 사이의 시간을 줄 것이다

Random rand = new Random(); 

int milis = (rand.nextInt(45) + 5)*1000; 
new java.util.Timer().schedule( 
     new java.util.TimerTask() { 
      @Override 
      public void run() { 
       // play your song here 
      } 
     }, 
     milis 
); 

편집 : 방금 시도했는데 작동합니다.

음악을 재생

Random rand = new Random(); 

    final int milis = (rand.nextInt(45) + 5) * 1000; 

    Button b = (Button) findViewById(R.id.button1); 

    b.setOnClickListener(new OnClickListener() { 
     MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.path); 

     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      new java.util.Timer().schedule(new java.util.TimerTask() { 
       @Override 
       public void run() { 
        try { 
         mp.start(); 
        } catch (Exception e) { 
         Log.e("Failed", e.getMessage()); 
        } 
       } 
      }, milis); 

     } 
    }); 
+0

덕분에 내가. 많이 – user3380879

+0

어디에서 onclick 리스너와 사운드 경로를 추가해야합니까? 미안 해요, 안드로이드 응용 프로그램을 처음 사용했습니다. – user3380879

+0

소리가 어떤 형식입니까? – user3380879

0

이이 솔루션을 시도 제대로 나를 위해 작동!

private static final int MSG_PLAY = 1; 
Timer timer = null; 

btnPlay.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Random randomTime = new Random(); 
     // where min = 5 and max = 50 its for example set your value 
      int time = randomTime.nextInt((max - min) + 1) + min; 

      if (timer != null) 
       timer.cancel(); 

      timer = new Timer(); 
      timer.schedule(new TimerTask() { 

       @Override 
       public void run() { 
        handler.sendEmptyMessage(MSG_PLAY); 
       } 
      }, time);// time is millisecond set according to your value 
     } 
    }); 




private Handler handler = new Handler() { 
    public void handleMessage(android.os.Message msg) { 

     switch (msg.what) { 
     case MSG_PLAY: 

         // code for play sound 

      break; 

     default: 
      break; 
     } 
    }; 
}; 
+0

그리고 어떻게하면 클릭 할 때마다 앱 플라이 바이 비트 사운드를 매초 만들 수 있습니까? – user3380879

+0

새 타이머(). scheduleAtFixedRate (작업, 0, 1000); – JossVAMOS

관련 문제