【问题标题】:Displaying Compass At specific location on Device在设备上的特定位置显示指南针
【发布时间】:2013-03-21 19:54:18
【问题描述】:

我想在设备上的特定位置显示指南针(图像视图)。我尝试像下面的代码一样在设备上显示指南针视图,但问题是我只需要在特定位置和小视图显示,但它占据整个屏幕空间。你能帮我在特定位置修复指南针图像吗?使用这行代码我得到一个图像形式的 Drawable 文件夹。 this.setImageResource(R.drawable.compassrose);那么如何在特定位置修复该图像。

Class1:-
         public class Compass extends Activity implements SensorListener {
  SensorManager sensorManager;
  static final int sensor = SensorManager.SENSOR_ORIENTATION;
  Rose rose;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//http://ofps.oreilly.com/titles/9781449390501/Android_System_Services.html
    // Set full screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
//FLAG_FULLSCREEN    
//FLAG_SCALED
    rose = new Rose(this);

    setContentView(rose);

    // get sensor manager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  }

  // register to listen to sensors
  @Override
  public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor);
  }

  // unregister
  @Override
  public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
  }

  // Ignore for now
  public void onAccuracyChanged(int sensor, int accuracy) {
  }

  // Listen to sensor and provide output
  public void onSensorChanged(int sensor, float[] values) {
    if (sensor != Compass.sensor)
      return;
    int orientation = (int) values[0];
    rose.setDirection(orientation);
  }
}


Class 2:-
             public class Rose extends ImageView {
  Paint paint;
  int direction = 0;

  public Rose(Context context) {
    super(context);

    paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(2);
    paint.setStyle(Style.STROKE);

    this.setImageResource(R.drawable.compassrose);
  }

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

    canvas.rotate(direction, width / 2, height / 2);
    super.onDraw(canvas);
  }

  public void setDirection(int direction) {
    this.direction = direction;
    this.invalidate();
  }

}

【问题讨论】:

    标签: android


    【解决方案1】:
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rose = new Rose(this);
        txtorientation = (TextView) findViewById(R.id.textView1);
        compassLayout = (RelativeLayout)findViewById(R.id.CompassLayout);
        compassLayout.addView(rose); 
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        Log.d("Compass", "onCreated");
    }
    

    【讨论】:

      【解决方案2】:

      您是在问如何在 Android 布局中定位 ImageView?您是否尝试过使用 XML,还是需要以编程方式设置?

      如果您只是尝试定位 ImageView,请尝试阅读一些布局教程:http://developer.android.com/resources/tutorials/views/index.html

      如果没有,请在您的问题中添加更多详细信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多