1. 程式人生 > >Arcgis For Android 不支援中文解決方案

Arcgis For Android 不支援中文解決方案

之前專案用的Arcgis for Android版本為10.2.1,TextSymbol還支援中文,現在升級到了10.2.4竟然不支援中文了,所有的中文都被空格替換掉了,鬱悶了半天,現在找到了一個比較好的解決方案,繞過TextSymbol。

之前做IOS的同事也遇到過這種情況,不過他將PictureMarkerSymbol和TextSymbol新增到CompositeSymbol中,然後再將CompositeSymbol加入到Graphic中,解決了該問題,但是我這次試這種方案,發現不能用了。

現在說一下我的解決思路吧,我使用的是PictureMarkerSymbol,看一下他的一個構造方法:

public 
PictureMarkerSymbol (Drawable drawable)

引數是一個Drawable物件,如果我們把所有的要在彈出氣泡中顯示的資訊放在一個View中,然後把View轉為Drawable物件,問題不就解決了嗎? 下面看一下效果圖最後的效果圖吧:
在這裡我貼出兩段比較關鍵的程式碼: 1.載入View檢視
	private View getGraphicView(Point point)
	{
		RelativeLayout view=(RelativeLayout)getLayoutInflater().inflate(R.layout.pop_graphic_view,null);
		DisplayMetrics metrics=getResources().getDisplayMetrics();
		int density=(int)metrics.density;
		//之所以在這兒設定LayoutParams引數,是因為在如果不這樣設定,在之後呼叫view.measure時在某些Android版本上會報空指標異常
		//在4.1.2回報空指標,4.4.2不會報,具體是裝置原因還是Android版本原因沒有詳細測試,條件有限
		RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,80*density);
		layoutParams.setMargins(0, 0, 0, 0);
		view.setLayoutParams(layoutParams);
		//這裡要說明一點,textView的singleLine屬性值不能設定為true,
		//否則textView上所有的文字會疊加在一起,成為一個密密麻麻的點
		//不信你可以試試
		TextView textView=(TextView) view.findViewById(R.id.tv_content);
		textView.setText("您點選的座標詳細資訊:\nX:"+point.getX()+"\nY:"+point.getY());
		return view;
	}
pop_graphic_view佈局檔案內容為:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/pop_bg"
    android:padding="0dp"
    android:layout_width="160dp"
    android:layout_height="60dp" >
    <TextView 
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="16sp"
        android:padding="5dp"
        android:layout_marginBottom="10dp"
        />
</RelativeLayout>

2.View轉為Bitmap:
	/**
	 * 將View轉換為BitMap
	 * @param view
	 * @return
	 */
	public static Bitmap convertViewToBitmap(View view){
		view.destroyDrawingCache();
		view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
				View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.setDrawingCacheEnabled(true);
		return view.getDrawingCache(true);
	}
然後生成Drawable物件:
Drawable drawable=new BitmapDrawable(null, bitmap);
生成PictureMarkerSymbol物件:
PictureMarkerSymbol symbol=new PictureMarkerSymbol(drawable);
以上即為核心程式碼片段,程式碼都比較簡單,不多做解釋了,主要是解決問題的思路。 原始碼下載地址: