我正在尝试使用LayerDrawable制作一个简单的地图,其中第一层是地图可绘制而第二层是标记。我想将标记定位在我希望它在每个/大多数设备上的确切位置,我该如何实现这种目标?
我现在设置的方式是使用setLayerInset移动标记,但标记的位置会因疯狂而在设备之间有所不同。
public class MapActivity extends AppCompatActivity {
private int phoneHeight, phoneWidth;
private ZoomageView zoom;
float x, y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setMarkerPos(0.279f, 0.245f);
createMap();
}
public void createMap() {
zoom = (ZoomageView) findViewById(R.id.zoom);
phoneHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
phoneWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
Drawable map = (Drawable) getResources().getDrawable(R.drawable.map);
Drawable marker = (Drawable) getResources().getDrawable(R.drawable.map_marker);
Drawable[] layers = {map, marker};
LayerDrawable layer = new LayerDrawable(layers);
layer.setLayerSize(1, 30, 35);
layer.setLayerInsetLeft(1, getX());
layer.setLayerInsetTop(1, getY());
zoom.setImageDrawable(layer);
zoom.setScaleRange(.5f, 8);
}
public void setMarkerPos(float x, float y) {
this.x = x;
this.y = y;
}
public int getX() {
return (int) (phoneHeight * x);
}
public int getY() {
return (int) (phoneHeight * y);
}
}