2013-03-26 36 views
2

我正在使用MapFragment在android中編寫應用程序,所以我希望mapview的角部圓角我在xml文件中創建一個形狀並將該形狀設置爲背景的地圖,但我沒有得到我想要的結果。 地圖的角落不是圓形的,但背景是,而且實際上這就是我的代碼所做的,所以任何人都可以給我一個問題,有一個圓角的地圖視圖,任何想法。Mapview在google-api-v2中用圓角在android中

回答

2

看看這個問題,我問的是完全相同的主題:

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

基本上你需要創建一個適當的9補丁圖像,並將其應用在它們映射使用另一種佈局FrameLayout

+0

我會試試這個,讓你知道結果 – Adil 2013-03-26 13:29:26

+0

你可以肯定地看到我的問題,它的工作原理和步驟也向你展示。所以讓我知道你是否有麻煩。 – 2013-03-26 13:52:37

+0

我發佈了一個anser解釋我遵循的步驟來創建一個圓角地圖。我希望這將是有益的,謝謝。 – Adil 2013-03-27 16:30:13

-1

將您的地圖置於任何佈局,如LinearLayout/RelativeLayout或任何佈局,並將該形狀樣式設置爲佈局作爲背景。

我的形狀文件

<?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

+1

從我的經驗這不會工作,你嘗試這種實現?你能提供適合你的形狀樣式代碼和佈局代碼嗎? – 2013-03-26 11:49:01

+0

我試過你的代碼,地圖沒有顯示,而且角落也不是圓形的,而是linearlayout的角落是圓形的,這是我在第一次嘗試時得到的,但是謝謝。 – Adil 2013-03-26 13:28:33

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該片段的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" 
/> 

我希望這會幫助你。

+0

這似乎嚴格比9補丁解決方案更糟糕 - 如果你只是繪製黑色的角落,爲什麼不使用背景9補丁,這似乎是一個像這樣的場景? – secureboot 2014-04-11 18:29:27