返回顶部

收藏

Android 创建一个旋转的拨号盘

更多

http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-a-rotating- dialer/

坐标系统

imgs/asCode/20075829_QFeX.jpg

项目结构

imgs/asCode/20075846_OA8n.png

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFCCCCCC">
    <ImageView
        android:src="@drawable/graphic_ring"
        android:id="@+id/imageView_ring"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"></ImageView>
</LinearLayout>

Activity

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

private ImageView dialer;
private int dialerHeight, dialerWidth;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // load the image only once
    if (imageOriginal == null) {
        imageOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.graphic_ring);
    }

    // initialize the matrix only once
    if (matrix == null) {
        matrix = new Matrix();
    } else {
        // not needed, you can also post the matrix immediately to restore the old state
        matrix.reset();
    }

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

        @Override
        public void onGlobalLayout() {
            // method called more than once, but the values only need to be initialized one time
            if (dialerHeight == 0 || dialerWidth == 0) {
                dialerHeight = dialer.getHeight();
                dialerWidth = dialer.getWidth();

                // resize
                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);
            }
        }
    });

}

触摸事件

/**
 * 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) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                startAngle = getAngle(event.getX(), event.getY());
                break;

            case MotionEvent.ACTION_MOVE:
                double currentAngle = getAngle(event.getX(), event.getY());
                rotateDialer((float) (startAngle - currentAngle));
                startAngle = currentAngle;
                break;

            case MotionEvent.ACTION_UP:

                break;
        }      

        return true;
    }

}

旋转处理

/**
 * @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:
            return 180 - Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
        case 3:
            return 180 + (-1 * 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:
            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;
    }
}

标签:Android,SDK

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Trinea 发表 2016-12-19 10:52:15 滴滴国际化项目 Android 端演进
  2. 草依山 发表 2013-12-13 08:00:00 手工下载Android的SDK
  3. 7 发表 2014-10-18 08:54:12 Android L SDK更新
  4. ReSur 发表 2014-10-23 13:28:57 Safari扩展获取安装包(最新版本)的方法
  5. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新
  6. ANIL K. 发表 2018-05-03 09:31:10 Android 4.0 SDK: Crash during stream subscription
  7. 南 靖男 发表 2018-07-06 02:35:08 flutter 免 Android Studio 安装 Android SDK
  8. fatkun 发表 2013-03-10 05:29:01 安装android sdk提示找不到JDK解决方法
  9. rain 发表 2016-03-10 02:46:26 意外:Android N preview 发布了
  10. Trinea 发表 2016-12-11 13:47:22 三方桌面支持 Android SDK 7.1 新特性 Shortcuts
  11. CppLive 发表 2014-12-24 07:27:26 Mac下部署Android开发环境附加NDK
  12. admin 发表 2018-02-25 03:46:02 Android Wear SDK 和模拟器更新

发表评论