Android:如何一次旋转拨号器15度

时间:2013-09-30 12:00:58

标签: android

大家好请帮助我在每次旋转时旋转拨号器15度。下面我提供完整的代码。如果需要,我也可以提供我真正需要的图像。请尽快恢复。

public class Learning extends Activity {

    public static Bitmap imageOriginal, imageScaled;
    public static Matrix matrix;

    private int current = 0;
    private int previous = 0;

    private int tempCount = 0;

    public int dialerHeight, dialerWidth;

    public GestureDetector detector;
    public boolean[] quadrantTouched;

    public boolean allowRotating;

    public ImageView imageTop;
    public ImageView imageBottom;
    public ImageView dialer;
    public ImageView stackChange;

    public Button amphibian;
    public Button birds;
    public Button fish;
    public Button invertebrates;
    public Button mammals;
    public Button repitiles;
    public Button fruits;
    public Button vegitables;

    private ScrollView sV;

    private ImageView homeButton;

    public ArrayList<Button> callingOne = new ArrayList<Button>();

    public int number = 0;
    public int tempNumber = 0;

    public int startingAngle;
    public int endAngle;

    public TextView diaText;

    double currentAngle;

    public String[] alpha = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
            "X", "Y", "Z" };
    public HorizontalScrollView horizontalItem;

    DisplayMetrics dp = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.learn);

        horizontalItem = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
        horizontalItem.smoothScrollTo(0, 50);

        sV = (ScrollView) findViewById(R.id.vScrool);

        diaText = (TextView) findViewById(R.id.dialerText);

        amphibian = (Button) findViewById(R.id.btnForg);
        birds = (Button) findViewById(R.id.btncroco);
        fish = (Button) findViewById(R.id.btnReptile);
        invertebrates = (Button) findViewById(R.id.btnInvertebrate);
        mammals = (Button) findViewById(R.id.btnFrg);
        repitiles = (Button) findViewById(R.id.btnMammal);
        fruits = (Button) findViewById(R.id.btnFish);
        vegitables = (Button) findViewById(R.id.btnSawan);
        homeButton = (ImageView) findViewById(R.id.imageView1);

        homeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Animation slide = AnimationUtils.loadAnimation(
                        getApplicationContext(), R.anim.slide_up);
                sV.startAnimation(slide);
            }
        });

        callingOne.add(0, amphibian);
        callingOne.add(1, birds);
        callingOne.add(2, fish);
        callingOne.add(3, invertebrates);
        callingOne.add(4, mammals);
        callingOne.add(5, repitiles);
        callingOne.add(6, fruits);
        callingOne.add(7, vegitables);

        if (imageOriginal == null) {
            imageOriginal = BitmapFactory.decodeResource(getResources(),
                    R.drawable.dial_letters);
        }

        if (matrix == null) {
            matrix = new Matrix();
        } else {
            matrix.reset();
        }

        detector = new GestureDetector(this, new MyGestureDetector());

        quadrantTouched = new boolean[] { false, false, false, false, false };

        allowRotating = true;

        amphibian.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(0);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {

                }
                return false;
            }
        });

        birds.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(1);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {

                }
                return false;
            }
        });

        fish.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    // fish.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                    // R.anim.zoom_enter));
                    // System.out.println(horizontalItem.getId());
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(2);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                }
                return false;
            }
        });

        invertebrates.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    // invertebrates.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                    // R.anim.zoom_enter));
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(3);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                }
                return false;
            }
        });

        mammals.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    // mammals.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                    // R.anim.zoom_enter));
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(4);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {

                }
                return false;
            }
        });

        repitiles.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    // repitiles.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                    // R.anim.zoom_enter));
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(5);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                }
                return false;
            }
        });

        fruits.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    // fruits.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                    // R.anim.zoom_enter));
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(6);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                }
                return false;
            }
        });

        vegitables.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    // new AnimationUtils();
                    // vegitables.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
                    // R.anim.zoom_enter));
                    sV.setVisibility(View.VISIBLE);
                    Animation slide = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.slide_down);
                    sV.startAnimation(slide);
                    checkFocusable(7);
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                }
                return false;
            }
        });

        dialer = (ImageView) findViewById(R.id.idRing);
        dialer.setOnTouchListener(new MyOnTouchListener());
        dialer.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        if (dialerHeight == 0 || dialerWidth == 0) {
                            dialerHeight = dialer.getHeight();
                            dialerWidth = dialer.getWidth();

                            Matrix resize = new Matrix();
                            resize.postScale(
                                    (float) Math.min(dialerWidth, dialerHeight)
                                            / (float) imageOriginal.getWidth(),
                                    (float) Math.min(dialerWidth, dialerHeight)
                                            / (float) imageOriginal.getHeight());
                            imageScaled = Bitmap.createBitmap(imageOriginal, 0,
                                    0, imageOriginal.getWidth(),
                                    imageOriginal.getHeight(), resize, false);

                            float translateX = dialerWidth / 2
                                    - imageScaled.getWidth() / 2;
                            float translateY = dialerHeight / 2
                                    - imageScaled.getHeight() / 2;
                            matrix.postTranslate(translateX, translateY);

                            dialer.setImageBitmap(imageScaled);
                            dialer.setImageMatrix(matrix);

                        }
                    }
                });

        imageTop = (ImageView) findViewById(R.id.imgTop);
        imageBottom = (ImageView) findViewById(R.id.imageBottom);

        imageTop.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                horizontalItem.setVisibility(View.GONE);
                imageBottom.setVisibility(View.VISIBLE);
                // sv.setVisibility(View.GONE);
                imageTop.setVisibility(View.GONE);

            }
        });
        imageBottom.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                horizontalItem.setVisibility(View.VISIBLE);
                imageBottom.setVisibility(View.GONE);
                imageTop.setVisibility(View.VISIBLE);
                // sv.setVisibility(View.VISIBLE);

            }
        });

    }

    private void checkFocusable(int id) {

        if (tempCount == 0) {
            current = id;
            previous = current;
            tempCount++;
        } else {
            previous = current;
            current = id;
        }
        Animation scaling_zoom_in = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.zoom_enter);
        scaling_zoom_in.setFillAfter(true);

        Animation scaling_zoom_out = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.zoom_out);
        scaling_zoom_out.setFillAfter(true);

        System.out.println("Current : " + current);
        System.out.println("Previous : " + previous);
        if (current != previous) {
            callingOne.get(current).startAnimation(scaling_zoom_in);
            callingOne.get(previous).startAnimation(scaling_zoom_out);
        } else {
            callingOne.get(current).startAnimation(scaling_zoom_in);
        }

    }

    private void rotateDialer(float degrees) {
        // System.out.println("Degree : "+degrees);
        matrix.postRotate(degrees, dialerWidth / 2, dialerHeight / 2);
        dialer.setImageMatrix(matrix);

    }

    /**
     * @return The angle of the unit circle with the image view's center
     */
    private double getAngle(double xTouch, double yTouch) {
        double x = xTouch - (dialerWidth / 2d);
        double y = dialerHeight - yTouch - (dialerHeight / 2d);

        switch (getQuadrant(x, y)) {
        case 1:
            return Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;

        case 2:
        case 3:
            return 180 - (Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI);

        case 4:
            return 360 + Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;

        default:
            // ignore, does not happen
            return 0;
        }

    }

    /**
     * @return The selected quadrant.
     */
    private static int getQuadrant(double x, double y) {
        if (x >= 0) {
            return y >= 0 ? 1 : 4;
        } else {
            return y >= 0 ? 2 : 3;
        }
    }

    /**
     * Simple implementation of an {@link OnTouchListener} for registering the
     * dialer's touch events.
     */
    private class MyOnTouchListener implements OnTouchListener {

        private double startAngle;

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            float currentX = event.getX();
            float currentY = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                for (int i = 0; i < quadrantTouched.length; i++) {
                    quadrantTouched[i] = false;
                }

                allowRotating = true;

                startAngle = getAngle(currentX, currentY);
                startingAngle = (int) Math.abs(startAngle);
                System.out.println("Starting Angle : " + startingAngle);
                break;

            case MotionEvent.ACTION_MOVE:

                currentAngle = getAngle(currentX, currentY);
                endAngle = (int) Math.abs(currentAngle);

                rotateDialer((float) (Math.abs(startAngle - currentAngle)) / 20);

                startAngle = currentAngle;

                break;

            case MotionEvent.ACTION_UP:
                if (startingAngle != endAngle) {
                    number++;
                }
                if (number == 7) {
                    diaText.setText(alpha[tempNumber]);
                    tempNumber++;
                    number = 0;
                }
                if (tempNumber == 0) {
                    diaText.setText(alpha[tempNumber]);
                    tempNumber++;
                }
                if (tempNumber > 25) {
                    tempNumber = 0;
                }
                System.out.println("Current Angle : " + startAngle);
                allowRotating = false;
                break;
            }

            // set the touched quadrant to true
            quadrantTouched[getQuadrant(event.getX() - (dialerWidth / 2),
                    dialerHeight - event.getY() - (dialerHeight / 2))] = true;

            detector.onTouchEvent(event);

            return true;
        }
    }

    /**
     * Simple implementation of a {@link SimpleOnGestureListener} for detecting
     * a fling event.
     */
    private class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

            // get the quadrant of the start and the end of the fling
            int q1 = getQuadrant(e1.getX() - (dialerWidth / 2), dialerHeight
                    - e1.getY() - (dialerHeight / 2));
            int q2 = getQuadrant(e2.getX() - (dialerWidth / 2), dialerHeight
                    - e2.getY() - (dialerHeight / 2));

            // the inversed rotations
            if ((q1 == 2 && q2 == 2 && Math.abs(velocityX) < Math
                    .abs(velocityY))
                    || (q1 == 3 && q2 == 3)
                    || (q1 == 1 && q2 == 3)
                    || (q1 == 4 && q2 == 4 && Math.abs(velocityX) > Math
                            .abs(velocityY))
                    || ((q1 == 2 && q2 == 3) || (q1 == 3 && q2 == 2))
                    || ((q1 == 3 && q2 == 4) || (q1 == 4 && q2 == 3))
                    || (q1 == 2 && q2 == 4 && quadrantTouched[3])
                    || (q1 == 4 && q2 == 2 && quadrantTouched[3])) {
                dialer.post(new FlingRunnable(-1 * (velocityX + velocityY)));
            } else {
                // the normal rotation
                System.out.println("Velocity : "+(velocityX + velocityY));
                dialer.post(new FlingRunnable(velocityX + velocityY));
            }

            return true;
        }
    }

    /**
     * A {@link Runnable} for animating the the dialer's fling.
     */
    private class FlingRunnable implements Runnable {

        private float velocity;

        public FlingRunnable(float velocity) {
            this.velocity = velocity;
        }

        @Override
        public void run() {
            if (Math.abs(velocity) > 5 && allowRotating) {
                rotateDialer(velocity / 75);
                velocity = 1.0666F;

                // post this instance again
                dialer.post(this);
            }
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent home = new Intent(Learning.this, MainActivity.class);
        home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(home);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        finish();
    }
}

0 个答案:

没有答案