【问题标题】:Mapview in google-api-v2 with rounded corners in androidgoogle-api-v2中的Mapview在android中带有圆角
【发布时间】:2013-03-26 11:36:00
【问题描述】:

我正在使用 MapFragment 在 android 中编写一个应用程序,所以我希望 mapview 的角变圆我在 xml 文件中创建一个形状并将该形状设置为地图的背景,但我没有得到结果想。 地图的角不是圆角的,但背景是圆角的,实际上这就是我的代码所做的,所以任何人都可以给我一个圆角地图视图的问题,任何想法。

【问题讨论】:

    标签: android rounded-corners mapfragment


    【解决方案1】:

    查看我在完全相同的主题上提出的这个问题:

    Is there a way to implement rounded corners to a Mapfragment?

    基本上,您需要创建一个适当的 9-patch 图像,并使用带有FrameLayout 的另一个布局将其应用到它们的地图上。

    【讨论】:

    • 我会试试这个,让你知道结果
    • 您当然可以从我的问题中看到它有效并且步骤也呈现给您。因此,如果您有任何问题,请告诉我。
    • 我发布了一个分析器,解释了我创建圆角地图所遵循的步骤。我希望这会有所帮助,谢谢。
    • 很棒的@Adil,这看起来很有趣。
    • 感谢@Emil Adz,如果您看到我们可以将其应用于我们想要的任何视图,例如图像或其他东西,我们所需要做的就是传递我们希望其圆角作为参数的视图到构造函数 RoundedCornerMap。我现在想使用宽度和高度来在屏幕上留出一些空间,以便我可以添加其他内容,我正在更改 xml 文件中的高度,但没有任何反应。
    【解决方案2】:

    我做了一些研究,发现了一个问题。好吧,我创建了一个扩展的视图 LinearLayout(或framelayout)这个视图是这样的

    public class RoundedCornerMap extends LinearLayout{
    
    Bitmap windowFrame;
    //this constructer is needed by a tool in explipse to render the layout, you can not define it
    public RoundedCornerMap(Context context, AttributeSet attr){
        super(context, attr);
    }
    //this 
    public RoundedCornerMap(Context context, AttributeSet attr, View view) {
        super(context, attr);
        init(view);
    }
    
    private void init(View view) {
    
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        view.setClickable(true);
        addView(view);
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
    
        if(windowFrame==null)
            createWindowFrame();// Lazy creation of the window frame, this is needed as we don't know the width & height of the screen until draw time
    
        canvas.drawBitmap(windowFrame, 0, 0, null);
    }
    
    private void createWindowFrame() {
    
        windowFrame= Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);// Create a new image we will draw over the map
        Canvas osCanvas = new Canvas(windowFrame);// Create a    canvas to draw onto the new image
    
        RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
        RectF innerRectangle = new RectF(10, 10, getWidth()-10, getHeight()-10);
    
        float radiusCorner = getWidth()/18f;// The angle of your corners
    
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);// Anti alias allows for smooth corners
        paint.setColor(getResources().getColor(Color.BLACK));// This is the color of your activity background
        osCanvas.drawRect(outerRectangle, paint);
    
        paint.setColor(Color.RED);// An obvious color to help debugging
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));// A out B http://en.wikipedia.org/wiki/File:Alpha_compositing.svg
        osCanvas.drawRoundRect(innerRectangle, radiusCorner, radiusCorner, paint);
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    
        super.onLayout(changed, l, t, r, b);
    
        windowFrame=null;// If the layout changes null our frame so it can be recreated with the new width and height
    }
    
    }
    

    扩展 Mapfragment 的 Fragment 的 onCreate 方法是这样的:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        XmlPullParser parser = getResources().getXml(R.layout.rounded_corner_map);
        AttributeSet attr = Xml.asAttributeSet(parser);
    
    
        View view=new RoundedCornerMap(myActivity, attr, super.onCreateView(inflater, container, null));
        //ignore about this line below i just want to set a shape to the view
        //view.setBackgroundResource(R.drawable.map_shape);
        return view;
    }
    

    我们需要像这样定义 xml 文件 rounded_corner_map:

    <?xml version="1.0" encoding="utf-8"?>
    <com.map.RoundedCornerMap xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
    

    希望对你有所帮助。

    【讨论】:

    • 这似乎比 9 补丁解决方案更糟糕 - 如果你只是在画黑角,为什么不使用背景 9 补丁,这似乎是为像这样的场景而设计的?
    【解决方案3】:

    将您的地图放在任何布局中,例如 LinearLayout/RelativeLayout 或任何布局,并将该形状样式设置为 Layout 作为背景。

    我的形状文件

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="10dp"/>
        <stroke android:color="#aa6632"/>
        <padding android:left="5dp" android:top="5dp"
            android:right="5dp" android:bottom="5dp"/>
    </shape>
    

    我的布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="400dp" 
            android:layout_centerInParent="true"
            android:background="@drawable/test">
        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/mapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment" />
        </LinearLayout>
    
    </RelativeLayout>
    

    您也可以使用FrameLayout 代替LinearLayout

    【讨论】:

    • 根据我的经验,这行不通,你试过这个实现吗?你能提供适合你的形状样式代码和布局代码吗?
    • 我试过你的代码,地图没有显示,角也没有圆角,而是线性布局的角是圆角的,这是我第一次尝试得到的,但是谢谢。
    猜你喜欢
    • 2015-09-11
    • 2015-02-19
    • 2013-02-14
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多