2012-02-08 129 views
0

我想打印矩形形状和主要的xml以及我想要的形状和数字,这可能吗?如果没有,我怎么能打印一个形状从java代码和我的主要xml都到我的模拟器?谢谢是否有可能在一个类中有多个setContentView?

package com.example.accel; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.RectShape; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class AccelActivity extends Activity implements SensorEventListener 
{ 
    /** Called when the activity is first created. */ 
    CustomDrawableView mCustomDrawableView = null; 
    ShapeDrawable mDrawable = new ShapeDrawable(); 
    public static int x; 
    public static int y; 
    public static int z; 
    private SensorManager sensorManager = null; 
    TextView x1, y1, z1; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     // Get a reference to a SensorManager 
     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
     mCustomDrawableView = new CustomDrawableView(this); 
     setContentView(mCustomDrawableView); 
     setContentView(R.layout.main); 

    } 

    // This method will update the UI on new sensor events 
    public void onSensorChanged(SensorEvent sensorEvent) 
    { 
     { 
      if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 

       TextView tvX= (TextView)findViewById(R.id.x_axis); 
       TextView tvY= (TextView)findViewById(R.id.y_axis); 
       TextView tvZ= (TextView)findViewById(R.id.z_axis); 

       x = (int) Math.pow(sensorEvent.values[0], 2); 
       y = (int) Math.pow(sensorEvent.values[1], 2); 
       z = (int) Math.pow(sensorEvent.values[2], 2); 

      } 

     // if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { 

      // } 
     } 
    } 

    // I've chosen to not implement this method 
    public void onAccuracyChanged(Sensor arg0, int arg1) 
    { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     // Register this class as a listener for the accelerometer sensor 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_FASTEST); 
     // ...and the orientation sensor 

    } 

    @Override 
    protected void onStop() 
    { 
     // Unregister the listener 
     sensorManager.unregisterListener(this); 
     super.onStop(); 
    } 

    public class CustomDrawableView extends View 
    { 
     static final int width = 150; 
     static final int height = 250; 

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

      mDrawable = new ShapeDrawable(new RectShape()); 
      mDrawable.getPaint().setColor(0xff74AC23); 
      mDrawable.setBounds(x, y, x + width, y + height); 

     } 

     protected void onDraw(Canvas canvas) 
     { 

      RectF rect = new RectF(AccelActivity.x, AccelActivity.y, AccelActivity.x + width, AccelActivity.y 
        + height); // set bounds of rectangle 
      Paint p = new Paint(); // set some paint options 
      p.setColor(Color.BLUE); 
      canvas.drawRect(rect, p); 
      invalidate(); 
     } 
    } 
} 
+0

你能解释一下你为什么要这样做吗?内容视图应该是您的控制器的委托视图。你不能有两个。一个必须在另一个中。 – 2012-02-08 13:28:08

+0

也许你想膨胀主布局内的另一个xml布局? – 2012-02-08 13:30:35

+0

因为我想要通过android main xml打印的加速度计值以及我在java代码中定义的形状,所以我不确定如何同时显示 – user1169775 2012-02-08 13:32:08

回答

0

是的,你可以。

setcontentview(anotherLayout),所以你在你的活动中引发新的布局。

,或者你可以在你的mainLayout定义其他元素和您的功能操作它setVisibily

,如果你需要elementA:

elementA = findViewById(R.id.elementA); 
elementA.setVisibility(true); 

,如果你不需要它:

elementA.setVisibility(false); 
+0

谢谢,但我只是试图打印一件事情从主要的XML和形状是在java代码中创建,所以他们是分开的,所以我试图在模拟器中都打印 – user1169775 2012-02-08 15:04:16

相关问题