-1

私はコンパス型の Android アプリケーションに取り組んでいます。コンパスのイメージがあり、さまざまな方向を示す文字 (北は「N」、東は「E」など) があります。ユーザーは任意の文字を押すと、特定の方向に関する情報を取得できます。タッチ座標を画像にバインドする方法を探しています。つまり、ユーザーが文字「N」を押すと、ハンドセットが向いている方向に関係なく、常に同様の座標が取得されます。以下は、私が現在使用しているコードです。

@Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
   View v=inflater.inflate(R.layout.compass_fragment, container, false);

   compassView = (CompassView) v.findViewById(R.id.compass_view);



   compassView.setOnTouchListener(new OnTouchListener() {
       @Override
       public boolean onTouch(View view, MotionEvent motion) {

           Log.i(TAG, "In onTouch");

           // Get the action that was done on this touch event
           switch (motion.getAction())
           {

                case MotionEvent.ACTION_DOWN:
                {
                    return true;
                }

               case MotionEvent.ACTION_UP:
               {
                   float x = motion.getX();
                   float y = motion.getY();

                   Log.i(TAG, "ACTION UP x = " + x + " y = " + y);
                   break;
               }
           }
           // if you return false, these actions will not be recorded
           return true;
       }
   });


    mSensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
    accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    return(v);
  }
4

1 に答える 1

0

私はそれを私の質問への答えにしました。ボタンを使わなくても思い通りに動作します。コードの下に添付しました。他の人に役立つことを願っています。

コンパスでタッチされた方向を取得するために、現在のコンパスの方位 (別のクラスから呼び出された setDegrees メソッドによって更新される度変数) と、Y 軸に対するタッチ位置の角度の間のオフセット角度を計算します。

public class CompassView extends ImageView {
    private float degrees=0;
    private String touchDirection;

    public CompassView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int height = this.getHeight();
        int width = this.getWidth();

        canvas.rotate(360-degrees, width/2, height/2);
        super.onDraw(canvas);
    }

    public void setDegrees(float degrees) {
        this.degrees = degrees;
        this.invalidate();
    }

    @Override
      public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return true;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                float eventX = event.getX();
                float eventY = event.getY();

                touchDirection = getTouchDirection(eventX, eventY);

                Context context = getContext();
                Intent intent = new Intent(context, CompassDirectionInfo.class);
                Bundle bundle = new Bundle();   
                bundle.putString("DIRECTION", touchDirection);
                intent.putExtras(bundle); 
                context.startActivity(intent);
                break;
            default:
                return false;
        }
        return true;
      }

    private String getTouchDirection (float eventX, float eventY) {

        String direction = "";

        float centreX = getWidth()/2, centreY = getHeight()/2;
        float tx = (float) (eventX - centreX), ty = (float) (eventY - centreY);
        float radius = (float) Math.sqrt(tx*tx + ty*ty);

        float offsetX = 0, offsetY = radius, adjEventX = eventX - centreX, adjEventY = centreY - eventY;

        double cosaU = ((offsetX * adjEventX) + (offsetY * adjEventY));
        double cosaD = ( Math.sqrt((offsetX * offsetX) + (offsetY * offsetY)) * Math.sqrt((adjEventX * adjEventX) + (adjEventY * adjEventY)));
        double cosa = cosaU / cosaD;

        double degr = ( Math.acos(cosa) * (180 / Math.PI));

        if (adjEventX < 0)
            degr = 360 - degr;

        float offsetDegrees = (float) (degrees + degr);

        if (offsetDegrees > 360)
            offsetDegrees = offsetDegrees - 360;

         if (offsetDegrees < 22.5 || offsetDegrees > 336.5)
             direction = "NORTH";
         else if (offsetDegrees > 22.5 && offsetDegrees < 67.5)
             direction = "NORTHEAST";
         else if (offsetDegrees > 67.5 && offsetDegrees < 112.5)
             direction = "EAST";
         else if (offsetDegrees > 112.5 && offsetDegrees < 156.5)
             direction = "SOUTHEAST";
         else if (offsetDegrees > 156.5 && offsetDegrees < 201.5)
             direction = "SOUTH";
         else if (offsetDegrees > 201.5 && offsetDegrees < 246.5)
             direction = "SOUTHWEST";
         else if (offsetDegrees > 246.5 && offsetDegrees < 291.5)
             direction = "WEST";
         else if (offsetDegrees > 291.5 && offsetDegrees < 336.5)
             direction = "NORTHWEST";

         return direction;
    }
}
于 2013-09-05T01:55:54.887 に答える