Android 将我的指南针整合到另一项活动中
我在我的应用程序中的一个活动中创建了一个指南针,现在我想将该指南针放入另一个活动中。如果可能的话,它应该表现为另一个视图,但仍然保留它的功能 有没有办法将一个活动嵌入到另一个活动中?还是在应该放置指南针的活动中尝试重新创建指南针更好 我会添加一个屏幕截图,但因为它是一个指南针,所以它不断更新,我认为DDMS有能力截图。所以我会解释:指南针从北到南,从西到东,画在整个活动上。有没有办法限制这种情况并阻止它发生 更新-进展不顺利-根据给出的建议,我根本无法显示指南针。所以我已经包括了我正在使用的代码,看看是否有人有任何想法Android 将我的指南针整合到另一项活动中,android,Android,我在我的应用程序中的一个活动中创建了一个指南针,现在我想将该指南针放入另一个活动中。如果可能的话,它应该表现为另一个视图,但仍然保留它的功能 有没有办法将一个活动嵌入到另一个活动中?还是在应该放置指南针的活动中尝试重新创建指南针更好 我会添加一个屏幕截图,但因为它是一个指南针,所以它不断更新,我认为DDMS有能力截图。所以我会解释:指南针从北到南,从西到东,画在整个活动上。有没有办法限制这种情况并阻止它发生 更新-进展不顺利-根据给出的建议,我根本无法显示指南针。所以我已经包括了我正在使用的代码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.new_compass_layout );
// New instance of our custom drawable view
mCustomDrawableView = new CustomDrawableView( this );
// Register the sensor listeners
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
//********************************************
// Test for adding a new view
RelativeLayout myLayout = (RelativeLayout)findViewById( R.id.reltveLayoutCmpss );
// Add layout parameters
LayoutParams testLayoutParams = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
mCustomDrawableView.setLayoutParams( testLayoutParams );
mCustomDrawableView.onMeasure( 100, 100 );
// Add it to the layout
myLayout.addView( mCustomDrawableView );
//********************************************
// More code in here - doesn't affect painting
}
// Drawable for my compass view
public class CustomDrawableView extends View
{
@Override
protected void onMeasure( int widthMeasure, int heightMeasure )
{
this.setMeasuredDimension(widthMeasure, heightMeasure);
}
Paint paint = new Paint();
public CustomDrawableView(Context context)
{
super(context);
paint.setColor(0xff00ff00);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);;
}
protected void onDraw(Canvas canvas)
{
// Get dimensions of the screen
int width = getWidth();
int height = getHeight();
int centerx = width/2;
int centery = height/2;
canvas.drawLine( centerx, 0, centerx, height, paint );
canvas.drawLine( 0, centery, width, centery, paint);
if( azimuth != null )
{
canvas.rotate( -azimuth * 360/(2*3.14159f), centerx, centery );
paint.setColor( 0xff0000ff );
canvas.drawLine( centerx, -1000, centerx, +1000, paint );
canvas.drawLine( -1000, centery, 1000, centery, paint );
canvas.drawText( "N - ^", centerx+5, centery-10, paint );
canvas.drawText( "S - v", centerx-10, centery+15, paint);
paint.setColor( 0xff00ff00 );
}
}
}
在使用绘画的视图上,是否可以使用onMeasure和setMeasuredDimension?
谢谢活动组/片段。我还没用过,别问怎么用
致以最诚挚的问候。我会以这种方式进行研究,但我确实做到了我-我必须添加所有代码,并使用LayoutParams添加它动态视图是您的指南针视图吗?如果没有,则将其设置为视图,以便您可以在所有布局中使用它,而不考虑活动等。我刚刚将其设置为视图,并动态添加了它-请参阅上面的更新如果在整个活动中绘制线条,请检查视图的边界,正确实现compass views On measure以限制其大小,并确保根据视图的大小而不是屏幕来绘制线条。它没有onMeasure方法,只有measure方法如果将CustomDrawable视图设置为静态类,请将onMeasure更改为调用setMeasuredDimension100,100并将其直接添加到xml文件中的布局reltvelayotcmpss中。您还需要在CustomDrawableView中重写上下文AttributeSet attrs构造函数。