我正在制作一个简单的指南针视图,它显示在MapView
. 和其他视图的布局MapView
在布局 xml 文件中声明。
我以这篇文章为例。我已经onDraw
为指南针视图编写了方法,但它没有显示任何内容。指南针对象在onSensorChanged
方法中无效。
为什么图片不显示?
XML 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mapviews.MyCompass
android:id="@+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp" />
<com.mapviews.TransparentPanel
android:id="@+id/transparent_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dp"
android:padding="5dp" />
<ImageButton
android:id="@+id/map_zoom_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/cd_zoom_out"
android:src="@drawable/zoom_out" />
<ImageButton
android:id="@+id/map_zoom_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/map_zoom_out"
android:contentDescription="@string/cd_zoom_in"
android:src="@drawable/zoom_in" />
<ImageButton
android:id="@+id/map_center_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/map_zoom_in"
android:contentDescription="@string/cd_center_user"
android:src="@drawable/directions" />
<com.mapviews.MyMap
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="MyAPIKey"
android:clickable="true"
android:state_enabled="true" />
MyCompass 类的一部分
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
canvas.rotate(
-1 * Values.accelValues[0]
, Values.width / 10
, Values.height / 10);
canvas.drawBitmap(
bitmap
, Values.width / 10 - bitmap.getWidth() / 10
, Values.height / 10 - bitmap.getHeight() / 10
, paint);
}
public void setup(Context c) {
paint = new Paint();
bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.compass1);
}
在onCreate
方法
compass = (MyCompass) findViewById(R.id.compass);
compass.setup(this);
compass.bringToFront();