2014年1月15日 星期三

Git(七) pull vs fetch

甚麼是git fetch

git fetch就是將遠端的資料update 到local端,簡單的說就是讓local端知道遠端最新的位置是在哪
舉個例子 如下圖
先不管server端的部分,
origin/hello2 為遠端的本地參照位置,hello2為目前實際的位置。

假設 遠端原本只有 aaa bbb ccc三個commit 
然後user A和B 皆使用 git clone , 他們的origin/hello2都會是在 ccc的位置,
hello2也會在ccc的位置

假設user B  commit 並 push 了 ddd,這時user A的 origin/hello2依然是在 ccc
那 user A 該如何更新origin/hello2到 ddd呢?
user A只需要使用 git fetch origin hello2 
這樣 user A的 origin/hello2就會更新到 ddd的位置

fetch 會去更新origin/hello2(本地參照)

解釋完git fetch之後,要解釋git pull就相對簡單許多



甚麼是git pull


git pull 實際作用為 git fetch  +  git merge

舉個例子

假設目前在遠端上面有人 commit 並push了  aaa 和 bbb
而我在本地端commit 了 ccc的功能

假設打入指令 git fetch origin hello2 這時會將origin/hello2 更新到bbb的位置
接著使用git merge origin hello2就會變成 如下圖
然而這個結果跟直接使用 git pull的結果是一樣的

git pull 不只會更新本地參照還會自動進行merge branch。


比較安全的用法

從server上拉資料之前  
總是先使用git fetch更新本地參照
接著先確認是否有其他的分支
若無其他分支,如下圖  則使用git pull  fast forward到 Origin/hello2


若有其他分支,如下圖
則檢查是否有需要merge 若有則使用 git merge。




結論

git fetch是相對較安全的做法,假設想要知道server上最新的版本但是還不想本地端還有一些功能還沒開發完,或是基於某些原因還不想merge的話可以使用git fetch,這樣本地只會取得server上最新版本但是並不會merge到本地端,

假設使用git pull 代表本地端功能已經開發完成可以進行合併,使用git pull就會直接將本地端跟server上做合併的動作。



2014年1月13日 星期一

Android 開發 (十四) sherlock actionBar library 使用

甚麼是sherlock actionBar   附上  範例App

下面試這個library的網站,在這裡我要簡單的介紹這個library的使用方法

http://actionbarsherlock.com/

why use actionbarsherlock

由於內建的actionbar限制很多,有許多特別的UI都無法達成,所以就找到這個library,他使得客製化變得非常方便。


如何使用actionbar

要使用這個library首先必須先將 project reference  引進,這在Android 開發 (十三) ViewPager 使用方法 有介紹如何引用,接著下面是簡單的範例


public class MainActivity extends SherlockActivity implements OnClickListener {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  findViewById(R.id.btn).setOnClickListener(this);
  getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}

 @Override
 public void onClick(View view) {
  if(view.getId()==R.id.btn){
      LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View v = inflator.inflate(R.layout.mylayout, null);
      ((TextView)v.findViewById(R.id.txt)).setText("123");
      LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
      v.setLayoutParams(params);
      v.setBackgroundColor(Color.YELLOW);
      this.getSupportActionBar().setCustomView(v);
  }

 }
}

在onCreate時設定 displayoptions為 DISPLAY_SHOW_CUSTOM ,
接著在button click時將 actionbar設定成任意UI
這樣就完成了

附上  sample code


附註
如果希望能夠在actionbar上面有progressbar的功能
只需要使用

setSupportProgress(Window.PROGRESS_END);
setSupportProgressBarIndeterminateVisibility(true);

要使progressbar消失則使用

setSupportProgressBarIndeterminateVisibility(false);



2014年1月8日 星期三

Android 開發 (十三) ViewPager 使用方法

甚麼是ViewPager

許多使用滑動切換頁面的方式都是使用 ViewPager 完成的

底下是source code的下載網址,裡面也附有許多的sample
https://github.com/JakeWharton/Android-ViewPagerIndicator
要使用該sample
首先必須先將lib import 且 copy 到workspace
(步驟是 import -> exist android project -> 選擇路徑 ->  copy project into workspace)

接著必須將該lib加入reference的列表中
(步驟為在project 按右鍵 -> properties -> 選擇Android項目 ->在library那欄選擇 add 將lib 加入reference)

接著如果出現需要import R的錯誤就必須將local端的 android-support-v4.jar 覆蓋library的 android-support-v4.jar
基本上這樣之後使用 Clean Build 就可以build過了  sample code 的 import 也是相同的步驟


如何使用 ViewPager 


viewpager 需要一個PageAdapter,然後在 instantiateItem 可以客製化自己的UI
,記得必須destroyItem 否則會crash,如果需要顯是title的話就必須override 掉getPageTitle,
底下是PageAdapter的範例


class myPagerAdapter extends PagerAdapter {
 private List<String> mTitles;
 public myPagerAdapter(List<String> titles) {
  mTitles = titles;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return mTitles.size();
 }

 @Override
 public Object instantiateItem(View container, int position) {
  // TODO Auto-generated method stub
  LayoutInflater inflater = (LayoutInflater) container.getContext()
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  int id;
  switch (position%3) {
  case 0:
   id = R.layout.firstpage;
   break;
  case 1:
   id = R.layout.secondpage;
   break;
  case 2:
   id = R.layout.thirdpage;
   break;
  default:
   id = R.layout.secondpage;
   break;
  }
  View view = inflater.inflate(id, null);
  ((ViewPager) container).addView(view, 0);
  return view;
 }
 @Override
 public void destroyItem(View container, int position, Object object) {
  // TODO Auto-generated method stub
  ((ViewPager) container).removeView((View)  object);
 }
 @Override
 public CharSequence getPageTitle(int position) {
  // TODO Auto-generated method stub
  return mTitles.get(position);
 }

 @Override
 public boolean isViewFromObject(View arg0, Object arg1) {
  // TODO Auto-generated method stub
  return arg0 == arg1;
 }


然後只需要在MainActivity裡設置 viewpager 和TabPageIndicator 即可

MainActivity.java

  mViewPager.setAdapter(new myPagerAdapter(titles));
  TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);  
        indicator.setViewPager(mViewPager); 


activity_main.xml

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="wrap_content"
         />


這樣viewpager就完成了
可左右滑動,或者按上方的title 變換頁面
附上  sample Code

2014年1月7日 星期二

Android 開發 (十二) ImageView scaletype介紹

ImageView scaletype 的特性顯示圖如下

    圖片size大於imageview                  圖片size小於imageview

















由結果圖可知

假設要顯示整張圖可使用 fitCenter  或 centerInside

fitCenter:  圖片按比例放大/縮小到view的寬度
centerInside:圖片按比例縮小到view的寬度

上半部  圖 : 452x302   view : 50x50
下半部  圖 : 16x15       view : 50x50



fixXY: 把圖片不按比例放大/縮小到view的大小
fitStart: 把圖片按比例放大/縮小到view的寬度,並顯示在view的上部分
fitEnd:  把圖片按比例放大/縮小到view的寬度,並顯示在view的下部分

上半部  圖 : 452x302   view : 50x50
下半部  圖 : 16x15       view : 50x50















matrix : 可用於矩陣繪製

敏捷開發(二) 站立會議

在敏捷開發中,站立會議是每天必開的會議,
對許多人來說"開會"是個非常可怕的事情,隨便一個議程就要一個小時跑不掉,
對許多人而言會議跟浪費時間掛上等。

然而對我而言,以前開會的確是很浪費時間,但是現在身在敏捷開發中,每一次會議都讓我感到非常的珍貴,其中一項就是站立會議。


甚麼是站立會議

站立會議顧名思義,就是站著開會,其實第一次開會的時候覺得很有趣,
感覺像是一堆人在那邊罰站,然後就從第一位開始一個接著一個報告,
內容基本上都是

昨天我做了甚麼,有沒有遇到甚麼問題,今天我打算做甚麼。
如果是小問題就拿出來大家稍為提供一些意見,沒有問題的話就換下一位



為何要站立會議

站立會議的目的就是讓其他人瞭解每個人目前的進度(尤其是PM),
然後有哪些人目前是比較有空閒,以便於做其他的安排,例如假設小明昨天完成了一項功能,今天沒其他的事情就可以派去幫忙解bug或者refactor,簡單來說這樣可以讓PM對於人力做較適當的安排。



站立會議的時程

我們參加會議的人數大約十個,但是會議時間通常不會超過十五分鐘,
通常都是兩三句話就帶過,如果有其他的問題通常會找幾個相關人員私下討論。



敏捷開發 = RD會變輕鬆?

站立會議讓大家互相了解目前的進度,其實對RD而言也是一種壓力,
假設兩個人負責相同的功能只是負責的平台不同,例如android 版 vs ios 版
假設一個人接近完成,另一個人還有一半功能尚未完成,這時候壓力就大了,
雖然說RD可自行訂定任務完成的時間,但是天有不測風雲,總是會有無法預測的事情會發生,個人認為敏捷開發需要有非常高的自主性,必須對做的事情負責,知道自己的不足就必須自己補足,進而追上平均的速率,然後關於寫story,最近認為寫story也是一門學問,不過這就是另一回事了~~ XD

2014年1月6日 星期一

Android 開發 (十一) include

include是幹嘛用的

include 讓我們可以重複使用相同的UI元件,如果需要使用同樣的UI不需要重覆寫在不同的XML檔,只需要寫一遍,然後使用include就可以重覆使用,這樣的好處是方便管理,而且UI有更改時可以一次改所有的頁面。

如何使用include

include_layout.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

  



    <TextView 

        android:id="@+id/txt"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

    <ImageView

        android:id="@+id/img"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/ic_launcher"

        />

</LinearLayout>



首先先寫一份之後會共用的UI,
接著使用 include 在layout這個property中將 include_layout放入

    <include

        android:id="@+id/include"

        layout="@layout/include_layout"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />



這樣UI就會自行顯示了。


如何取得include內的UI

要取得include內的UI只需要先取得 include 再使用該view去取得想取得的View
如下
View include_layout = (View)findViewById(R.id.include);
TextView txtView = ((TextView)include_layout.findViewById(R.id.txt));
txtView.setText("Include Text");


附上簡單的 sampleCode

Android 開發 (十) ViewStub

為何要使用viewstub

在寫android app時畫layout時必須要十分小心,否則將會導致顯示畫面不順(頓)或者out of memory。
在畫layout時即使設定View.visible(Gone),該View依然會消耗資源,解決的方法就是使用viewstub,ViewStub 是個非常輕量的view, 只有在Viewstub被inflat或visible之後才會佔空間
這也代表著,我們可以決定他何時要被inflat,我們可以在任何我們需要使用時才inflat他,例如button click之後 inflat 我們需要的UI

舉例來說

viewstub 可以用來實現 facebook的  ....更多
當按下時,就可以讀取新的view 進來
使用viewstub可以增加Page在初始化時的效率,避免畫了其他暫時不會被使用到的view,進而增加效率。

如何使用viewStub

<ViewStub
        android:id="@+id/stub"
        android:layout="@layout/mylayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
其中 layout 為 當我們inflat時產生的layout   例如

mylayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  
    <TextView
        android:id="@+id/Mytext"
        android:text="Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

接著在任意想要inflat 的時候使用

View view = viewStub.inflate();
TextView txtTextView = (TextView)view.findViewById(R.id.Mytext);
txtTextView.setText("What is this");


附上簡單的 sample code