如何在Rotatory Dialer android中达到特定点时停止旋转

时间:2015-11-18 10:10:04

标签: android animation rotation

这是我的源代码。

/*
 * This code is a modification of the Android LunarLander example.
 */

package net.simplifiedcoding.detectoutgoingcalls.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;

import net.simplifiedcoding.detectoutgoingcalls.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * View that draws, takes keystrokes, etc. for a simple LunarLander game.
 * <p/>
 * Has a mode which RUNNING, PAUSED, etc. draw() renders the
 * ship, and does an invalidate() to prompt another draw() as soon as possible
 * by the system.
 */
public class RotaryDialerView extends View {

    private float rotorAngle = 0F;
    private final Drawable rotorDrawable;
    private final int r1 = 50;
    private final int r2 = 160;
    private double lastFi;
    private int startN;
    public MediaPlayer mPlayer;

    private int width;
    private int height;
    private boolean soundRotating;

    android.media.MediaPlayer.OnCompletionListener soundListener;
    private boolean playSound;
    MainActivity activity = new MainActivity();

    public RotaryDialerView(Context context) {
        this(context, null);
        soundListener = new android.media.MediaPlayer.OnCompletionListener() {

            public void onCompletion(MediaPlayer mediaplayer) {
                stop();
            }
        };

    }

    public RotaryDialerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RotaryDialerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        playSound = false;
        startN = -1;
        soundRotating = false;

        rotorDrawable = context.getResources().getDrawable(R.drawable.dialer);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int availableWidth = getRight() - getLeft();
        int availableHeight = getBottom() - getTop();

        int x = availableWidth / 2;
        int y = availableHeight / 2;

        canvas.save();

        rotorDrawable.setBounds(0, 0, rotorDrawable.getIntrinsicWidth(),
                rotorDrawable.getIntrinsicHeight());

        if (rotorAngle != 0) {
            canvas.rotate(rotorAngle, x, y);
        }

        rotorDrawable.draw(canvas);

        canvas.restore();
    }

    protected void onMeasure(int i, int j) {
        height = android.view.View.MeasureSpec.getSize(j);
        width = android.view.View.MeasureSpec.getSize(i);
        setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final float x0 = width / 2;
        final float y0 = height / 2;
        float x1 = event.getX();
        float y1 = event.getY();
        float x = x0 - x1;
        float y = y0 - y1;
        double r = Math.sqrt(x * x + y * y);
        double sinfi = y / r;
        double fi = Math.toDegrees(Math.asin(sinfi));

        if (x1 > x0 && y0 > y1) {
            fi = 180D - fi;
        } else if (x1 > x0 && y1 > y0) {
            fi = 180D - fi;
        } else if (x0 > x1 && y1 > y0) {
            fi += 360D;
        }
        boolean isComplete = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                Log.e("MotionEvent.ACTION_MOVE", "" + MotionEvent.ACTION_MOVE);
                if (r > r1 && r < r2) {

                    if (!soundRotating) {
                        soundRotating = true;
                        playSound(startN);
                    }


                    final float angle2 = rotorAngle % 360;
                    int number2 = Math.round(angle2 - 20) / 30;
                    rotorAngle += Math.abs(fi - lastFi) + 0.25f;
                    rotorAngle %= 360F;
                    lastFi = fi;
                    if (startN >= 10 && !isComplete) {
                        isComplete = true;
                    }
                    // if (number2 < startN || startN == 0 && !isComplete) {
                    invalidate();
                    //}
                    return false;
                }

            case MotionEvent.ACTION_DOWN:
                // playMusic();
                // mPlayer2.pause();
                calculateStartN(rotorAngle);
                rotorAngle = 0F;
                lastFi = fi;
                return true;
            // }
            //return false;


            case MotionEvent.ACTION_UP:
                //mPlayer2.pause();
                soundRotating = false;
                final float angle = rotorAngle % 360;
                int number = Math.round(angle - 20) / 30;
                playSound_2(number);
                final float fl22 = Math.round(angle - 20) - number * 30;
                Log.e("angle", "" + fl22);
                if (number > 0) {
                    if (number == 10) {
                        number = 0;
                    }
                    // if (fl2 < 8.0 && fl2 > 4.0) {
                    fireDialListenerEvent(number);
                    //return true;
                    //}
                }

                rotorAngle = 0F;

                post(new Runnable() {
                    public void run() {
                        float fromDegrees = angle;
                        Animation anim = new RotateAnimation(fromDegrees, 0, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
                        anim.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.decelerate_interpolator));
                        anim.setAnimationListener(animListener);
                        anim.setDuration((long) (angle * 5F));
                        startAnimation(anim);
                        // playSound();
                        // angle = 00;//x0;
                    }

                });
                startN = -1;
                isComplete = false;
                return true;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    public interface DialListener {
        void onDial(int number);
    }

    private final List<DialListener> dialListeners = new ArrayList<DialListener>();

    public void addDialListener(DialListener listener) {
        dialListeners.add(listener);
    }

    public void removeDialListener(DialListener listener) {
        dialListeners.remove(listener);
    }

    private void fireDialListenerEvent(int number) {
        for (DialListener listener : dialListeners) {
            listener.onDial(number);
            Log.e("position ", "Reached");
        }
    }

    private android.view.animation.Animation.AnimationListener animListener = new android.view.animation.Animation.AnimationListener() {


        public void onAnimationEnd(Animation animation) {
            activity.stopVibrate();
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationStart(Animation animation) {
        }

    };

    public void playSound(int n) {
        try {
            Log.e("n value is", "" + n);
            boolean mStartPlaying = true;
            if (mStartPlaying == true) {
                mPlayer = new MediaPlayer();

                Uri uri = Uri.parse("android.resource://net.simplifiedcoding.detectoutgoingcalls/" + activity.SOUNDS_R[n]);
                mPlayer.setOnCompletionListener(soundListener);
                mPlayer.setDataSource(getContext(), uri);
                mPlayer.prepare();
                mPlayer.setLooping(false);
                mPlayer.start();

            } else {
                //   stopPlaying();
                //rePlay.setText("Replay");
                mPlayer.stop();
                mPlayer.release();
                mPlayer = null;
            }
            mStartPlaying = !mStartPlaying;


        } catch (IOException e) {
            Log.e("ERR", "prepare() failed");
        }
    }


    public void playSound_2(int n) {
        try {
            boolean mStartPlaying = true;
            if (mStartPlaying == true) {

                mPlayer = new MediaPlayer();

                Uri uri = Uri.parse("android.resource://net.simplifiedcoding.detectoutgoingcalls/" + activity.SOUNDS_S[n]);
                mPlayer.setOnCompletionListener(soundListener);
                mPlayer.setDataSource(getContext(), uri);
                mPlayer.prepare();
                mPlayer.setLooping(false);
                mPlayer.start();

            } else {
                //   stopPlaying();
                //rePlay.setText("Replay");
                mPlayer.stop();
                mPlayer.release();
                mPlayer = null;
            }
            mStartPlaying = !mStartPlaying;


        } catch (IOException e) {
            Log.e("ERR", "prepare() failed");
        }
    }


    public void stop() {
        try {
            mPlayer.release();
            mPlayer.stop();
            return;
        }
        // Misplaced declaration of an exception variable
        catch (Exception mediaplayer) {
            return;
        }
    }

    private void calculateStartN(double d) {
        if (d >= 0.0D && d < 30D) {
            startN = 5;
        } else {
            if (d >= 30D && d < 60D) {
                startN = 4;
                return;
            }
            if (d >= 60D && d < 90D) {
                startN = 3;
                return;
            }
            if (d >= 90D && d < 120D) {
                startN = 2;
                return;
            }
            if (d >= 120D && d <= 150D) {
                startN = 1;
                return;
            }
            if (d >= 210D && d < 240D) {
                startN = 0;
                return;
            }
            if (d >= 240D && d < 270D) {
                startN = 9;
                return;
            }
            if (d >= 270D && d < 300D) {
                startN = 8;
                return;
            }
            if (d >= 300D && d < 330D) {
                startN = 7;
                return;
            }
            if (d >= 330D && d <= 360D) {
                startN = 6;
                return;
            }
        }
    }

}

问题是,当我旋转拨号号码时,它不会在到达该点时弯腰。它会有争议地旋转出来。

这是示例源代码 http://www.ssaurel.com/blog/how-to-create-a-rotary-dialer-application-for-android/  每件事情都是完美的,但唯一的问题是旋转在到达终点时没有停止

0 个答案:

没有答案