• <bdo id='cs9Sm'></bdo><ul id='cs9Sm'></ul>

  1. <tfoot id='cs9Sm'></tfoot>
  2. <i id='cs9Sm'><tr id='cs9Sm'><dt id='cs9Sm'><q id='cs9Sm'><span id='cs9Sm'><b id='cs9Sm'><form id='cs9Sm'><ins id='cs9Sm'></ins><ul id='cs9Sm'></ul><sub id='cs9Sm'></sub></form><legend id='cs9Sm'></legend><bdo id='cs9Sm'><pre id='cs9Sm'><center id='cs9Sm'></center></pre></bdo></b><th id='cs9Sm'></th></span></q></dt></tr></i><div id='cs9Sm'><tfoot id='cs9Sm'></tfoot><dl id='cs9Sm'><fieldset id='cs9Sm'></fieldset></dl></div>

  3. <legend id='cs9Sm'><style id='cs9Sm'><dir id='cs9Sm'><q id='cs9Sm'></q></dir></style></legend>

    <small id='cs9Sm'></small><noframes id='cs9Sm'>

      Android/Java - GoogleMaps FragmentActivity 上的自定义视图

      时间:2023-05-20
        <tbody id='aMRyE'></tbody>

        <small id='aMRyE'></small><noframes id='aMRyE'>

        <tfoot id='aMRyE'></tfoot>

            • <bdo id='aMRyE'></bdo><ul id='aMRyE'></ul>

            • <legend id='aMRyE'><style id='aMRyE'><dir id='aMRyE'><q id='aMRyE'></q></dir></style></legend>
              <i id='aMRyE'><tr id='aMRyE'><dt id='aMRyE'><q id='aMRyE'><span id='aMRyE'><b id='aMRyE'><form id='aMRyE'><ins id='aMRyE'></ins><ul id='aMRyE'></ul><sub id='aMRyE'></sub></form><legend id='aMRyE'></legend><bdo id='aMRyE'><pre id='aMRyE'><center id='aMRyE'></center></pre></bdo></b><th id='aMRyE'></th></span></q></dt></tr></i><div id='aMRyE'><tfoot id='aMRyE'></tfoot><dl id='aMRyE'><fieldset id='aMRyE'></fieldset></dl></div>
                本文介绍了Android/Java - GoogleMaps FragmentActivity 上的自定义视图未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在尝试在自定义视图中绘制一些用于测试的东西,该视图已添加到 GoogleMaps FragmentActivity.但不知何故,当我在模拟器中运行它时,似乎我的 CustomView 不是绘图.我尝试使用新的 RelativeLayout 和 MapLayout.有任何想法吗 ?我做错了什么?

                Im trying to draw some Stuff for testing in an Custom View, which is added to the GoogleMaps FragmentActivity. But somehow, when i run it in the emulator, it seems like my CustomView isnt drawing.I tried it with a new RelativeLayout and an MapLayout. Any ideas ? What did i do wrong ?

                这是我的主要 GoogleMaps FragmentActivity 代码,从 Android 模板自动生成:

                Heres my Main GoogleMaps FragmentActivity code, auto generated from Android templates :

                public class GoogleMaps extends FragmentActivity implements OnMapReadyCallback {
                
                private GoogleMap mMap;
                
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.google_maps);
                    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(this);
                
                    //RelativeLayout relativeLayout = new RelativeLayout(this);
                    //relativeLayout.addView(new SpriteLayer(this));
                
                    MapView map = new MapView(this);
                    map.addView(new SpriteLayer(this));
                
                }
                
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    mMap = googleMap;
                
                    // Add a marker in Sydney and move the camera
                    LatLng sydney = new LatLng(-34, 151);
                    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                }
                }
                

                这是我的自定义视图代码:

                Thats my CustomView Code :

                public class SpriteLayer extends View {
                
                Paint paint = new Paint();
                
                public SpriteLayer(Context context) {
                    super(context);
                
                    setWillNotDraw(false);
                
                }
                
                @Override
                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                
                    int desiredWidth = 100;
                    int desiredHeight = 100;
                
                    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
                    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
                    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
                    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
                
                    int width;
                    int height;
                
                    //Measure Width
                    if (widthMode == MeasureSpec.EXACTLY) {
                        //Must be this size
                        width = widthSize;
                    } else if (widthMode == MeasureSpec.AT_MOST) {
                        //Can't be bigger than...
                        width = Math.min(desiredWidth, widthSize);
                    } else {
                        //Be whatever you want
                        width = desiredWidth;
                    }
                
                    //Measure Height
                    if (heightMode == MeasureSpec.EXACTLY) {
                        //Must be this size
                        height = heightSize;
                    } else if (heightMode == MeasureSpec.AT_MOST) {
                        //Can't be bigger than...
                        height = Math.min(desiredHeight, heightSize);
                    } else {
                        //Be whatever you want
                        height = desiredHeight;
                    }
                
                    //MUST CALL THIS
                    setMeasuredDimension(width, height);
                
                }
                
                @Override
                public void onDraw(Canvas canvas) {
                
                    paint.setColor(Color.GREEN);
                    Rect rect = new Rect(20, 56, 200, 112);
                    canvas.drawRect(rect, paint );
                
                    Log.println(Log.ERROR,"Test ","One");
                }
                }
                

                目前我只看到带有标记的标准地图,仍然没有矩形:(

                Currently i only see the standard map with an marker on it, still no Rectangle :(

                推荐答案

                您需要测量所需的视图大小.你只是传递你收到的东西,可能是 0.

                You need to measure the desired size of your view. You are just passing what you are receiving, which probably is 0.

                另一种选择是使用 MATCH_PARENT 设置 LayoutParams,但我不确定这是否可行.

                Another option is to set a LayoutParams with MATCH_PARENT, but I'm not sure if this will work.

                您尝试做的是高级,您应该在尝试之前阅读一些教程.

                What you are trying to do is advanced, you should read some tutorials before trying it.

                这里有更多信息:https://developer.android.com/training/custom-views/custom-drawing.html

                这篇关于Android/Java - GoogleMaps FragmentActivity 上的自定义视图未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:LibGdx 2 半屏“按钮" 下一篇:如何创建漩涡/漩涡效果?

                相关文章

                最新文章

                <tfoot id='MNx6a'></tfoot>
                <legend id='MNx6a'><style id='MNx6a'><dir id='MNx6a'><q id='MNx6a'></q></dir></style></legend>
                  <bdo id='MNx6a'></bdo><ul id='MNx6a'></ul>
                1. <small id='MNx6a'></small><noframes id='MNx6a'>

                    <i id='MNx6a'><tr id='MNx6a'><dt id='MNx6a'><q id='MNx6a'><span id='MNx6a'><b id='MNx6a'><form id='MNx6a'><ins id='MNx6a'></ins><ul id='MNx6a'></ul><sub id='MNx6a'></sub></form><legend id='MNx6a'></legend><bdo id='MNx6a'><pre id='MNx6a'><center id='MNx6a'></center></pre></bdo></b><th id='MNx6a'></th></span></q></dt></tr></i><div id='MNx6a'><tfoot id='MNx6a'></tfoot><dl id='MNx6a'><fieldset id='MNx6a'></fieldset></dl></div>