旋转随机时间后停止的图像

时间:2014-03-27 13:09:28

标签: android audio timer

我正在尝试制作旋转箭头。点击旋转。

随机时间后停止。

硬位是让箭头图像在随机位置停止。

到目前为止,这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

获取随机时间:

Random rand = new Random();

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

这会给你5到50之间的时间 然后使用timerTask:

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

        }
    });

答案 1 :(得分: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;
        }
    };
};