2012-03-05 45 views
0

我想在特定location.I顯示設備上的羅盤(圖片瀏覽)試圖像這下面的代碼來顯示在設備上的羅盤視圖但事情是我需要在特定顯示位置和小視圖,但它佔據了整個屏幕空間。可以幫助我在特定位置修復指南針圖像。使用這一行代碼,我得到一個圖像形式的可繪製文件夾。 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(); 
    } 

} 

回答

0

您是否在問如何在Android佈局中放置ImageView?您是否嘗試過使用XML,還是需要以編程方式進行設置?

如果你只是想以定位的ImageView,然後嘗試通過一些佈局教程的打算:http://developer.android.com/resources/tutorials/views/index.html

如果沒有,那麼請添加更多細節,你的問題

2
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"); 
} 
相關問題