2

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

4

3 回答 3

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"
/>

我希望这会对你有所帮助。

于 2013-03-27T16:28:09.170 回答
2

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

有没有办法为 Mapfragment 实现圆角?

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

于 2013-03-26T11:42:44.790 回答
-1

将您的地图放在任何布局中,如 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

于 2013-03-26T11:44:48.000 回答