Social Icons

2013年5月29日 星期三

今天我學到 - 在RelativeLayout中,如果使用include的時候,如何對元件佈局

今天在該程式界面的佈局,其中發生一個問題,我在RelativeLayout中用了2個include,原本想要讓他們上下排排站,卻發現跌在一起。原本的佈局如下:
  <RelativeLayout 
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:orientation="vertical"
        >
        <include android:id="@+id/distance_panel"
            android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />
        <include android:id="@+id/time_panel" 
            android:layout_below="@id/distance_panel"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />

後來查了一下,發現要把layout_width和layout_height也補上才會對,又學了一招。
   <RelativeLayout 
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:orientation="vertical"
        >
        <include android:id="@+id/distance_panel"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />
        <include android:id="@+id/time_panel" 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
            android:layout_below="@id/distance_panel"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />

2013年5月22日 星期三

今天我學到 - 不同Android版本的Fragment裡的onCreateOptionsMenu和onResume呼叫順序會不一樣


今天在查一個程式崩潰的問題,查到後來是因為當系統呼叫onResume時,一個menu item還未被建立,然後在onResume裡直接對這個null物件操作,所以造成程式崩潰。不過我在我的三台手機和模擬器都沒有問題啊,奇怪!

解決的方法就是多加個判斷就可以了。
  super.onResume();
  if (_new_record_menu!=null)
   _new_record_menu.setVisible(_show_new_record_menu);

今天我學到 - Service onStart程序內不要做loading太重的事

在我的計劃裡面,主程式起來前會呼叫一個service來完成一些上傳的動作,但是常常會讓我的主程式跑出UI freeze的對話框。用Google查了一下,Service的程序是會影響主程序的,如果要操作比較耗時的工作,還是要再另外起一個Thread會比較好啊。

原本的程式:
 @Override
 public void 
 onStart(Intent intent, int startId) {
  super.onCreate();
  showNotification();
  
  trainingId = -1;
  while (isConnect()==true &&          // still connect with internet 
    _error_occurred == false &&        // no error occurred.
    (trainingId=providerUtils.getLastTrainingId())>0) {  // can get any training record.
   
   if (_bg_h!=null) {
    _is_upload = true;
    _start_upload_time = System.currentTimeMillis();
    _bg_h.post(do_uploadTraingRecord);
    while (_is_upload==true && 
     _error_occurred==false) {
     if ((System.currentTimeMillis()-_start_upload_time) > max_waiting_time) {
      _is_upload = false;
      _error_occurred = true;
     }
     else {
      try {
       Thread.sleep(1000);
      } 
      catch (InterruptedException e) {
       e.printStackTrace();
       _error_occurred = true;
      }      
     }
    }
   }
   else {
    // no background handler.
    break;
   }
  }
  stopService();
 }
會發現有一個while迴圈,這就是造成UI凍結的兇手,只要把它搬到thread去就好了。

更改過後:
 @Override
 public void 
 onStart(Intent intent, int startId) {
  super.onCreate();
  showNotification();
  
  if (isConnect()==false || providerUtils.getLastTrainingId() <=0 || _bg_h==null) {
   stopService();
  }
  else {
   _bg_h.post(run_uploadAll);
  }
 }

2013年5月15日 星期三

今天我學到 - 如何讓Google map view (v2),在滾動頁面時不會產生殘影

使用Google map api v2時,發生了一個大問題,程式中的map view不是佔據整個頁面,只是其中的一部分。這樣問題就來了,當滾動(Scrolling)時原本map view的位置就會產生殘影,整個畫面就被破壞掉了。(後來發現如果將Google Map View放在ViewPager,也會有類似的問題)  

2013年5月7日 星期二

今天我學到 - Hardware acceleration和WebView

程式裡因為改用Google Map api V2時,就忘了將Hardware acceleration設成off,就在測試時發現WebView會不定時的crash,google了一下找到這篇,說WebView使用硬體加速會有很多問題,建議取消。但是,因為程式中的Google Map api v2用到openGL,所以如果Hardware acceleration設成on的話是會讓整個速度提升不少的。所以就花了一點時間查詢如何在外部和程式中設定是否使用硬體加速的功能。

在AndroidManifest.xml中設定

a.整個application設定
<application android:hardwareAccelerated="true" ...>
b.對activity
<activity android:hardwareAccelerated="false" />
您可以在application中設定整體是否要加速,然後在activity中對分別activity來設定。但是,如果您想要在程式中設定,或者只針對某一個View來設定呢的話,可以參考下面程式碼:
// 在window中只能設定硬體加速,不能取消哦。
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

// 在view中可以取消硬體加速
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

2013年5月6日 星期一

2013年5月2日 星期四

今天我學到 - Google Map API v2如何Zoom到指定位置和指定區塊

將程式裡的Google Map從原本的v1,移植到v2的版本上面來,發現還不是非常容易哩。原本v1裡面的一些程式碼都不能用了,全部都的改成v2的思維才行。


2013年5月1日 星期三

今天我學到 - 申請Android Google Map API v2

一直以來都使用初始的Google map api,不過這個版本在遇到新的Framework就有些適應不良了。也就是說如果在有Map的時候,要使用MapActivity,就不能和一般的Activity放在一起了,會增加程式的複雜度。另外,如果將Google Map用在Fragment中就必須使用MapFragment才行。

一旦,要將程式相容於Android 2.x時,一切都會變得很複雜了。。。

新的計劃裡要用HoloEverywhere + Google map api V2來完成,所以需要申請新的key,查了一下,摩刻部落已經將申請api key的程序寫的很完整了,放上連結,以便往後查詢。