2012年3月25日 星期日

[AndroidLayout] getWidth() and getHeight()

Error
當 view 還未完整被 layout 前,呼叫 getWidth() and getHeight() 所得的值會是 0。


Solution
  • The view's size is assigned when view.onSizeChanged is called with oldw = oldh = 0.
  • Assuming your view is visible, its size has been assigned by the time the activity's window receives focus. That is, when activity.onWindowFocusChanged is called with hasFocus = true.

The sequence is roughly:
  1. view.onFinishInflate
  2. view.onMeasure
  3. view.onSizeChanged(width, height, 0, 0)
  4. view.onLayout
  5. activity.onWindowFocusChanged(true)
// 這樣能確保得到正確的 width and height.
view.post(new Runnable() {
  // TODO
});


* Reference
- Re: When are View.getWidth and getHeight values valid? - joebowbeer - com.googlegroups.android-developers - MarkMail

[Android] Log

The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE.
  • Verbose should never be compiled into an application except during development.
  • Debug logs are compiled in but stripped at runtime.
  • Error, warning and info logs are always kept.

  • When you're building the string to pass into Log.d, the compiler uses a StringBuilder and at least three allocations occur: 
    • the StringBuilder itself
    • the buffer
    • the String object.
    • Realistically, there is also another buffer allocation and copy, and even more pressure on the gc.
  • That means that if your log message is filtered out, you might be doing significant work and incurring significant overhead.



* Reference
- Log | Android Developers

[AndroidAnimation] Animation attributes

  • android:interpolator
    • 指定 Interpolator,Interpolator 指動畫的變化速率。
    • 動畫各 frame 的顯示可以加速,减速,重複顯示。
  • android:shareInterpolator
    • true: 指定 <set>中的各個子動畫定義共享的 interpolator。

  • <set>
    • 為其它 animation 類型的容器。
    • ex: <alpha>,<scale>,<translate>和<rotate>或其它<set>。 
  • <alpha>
    • Fade in, Fade out 動畫 (AlphaAnimation)。
    • 参數有 fromAlpha, toAlpha。
  • <scale>
    • 縮放動畫 (ScaleAnimation)。
    • 參數 fromXScale, toXScale,fromYScale, toYScale, pivotX, pivotY。
      • pivotX, pivotY 定義了缩放時的中心。
  • <translate>
    • 平移動畫 (TranslateAnimation)。
    • 参數由 fromXDelta, toXDelta, fromYDelta, toYDelta。
  • <rotate>
    • 旋轉動畫 (RotateAnimation)。
    • 参數由 fromDegrees, toDegrees, pivotX, pivotY。



* Reference
- Android ApiDemos示例解析(3): App->Activity->Animation | 引路蜂移动软件

[AndroidAnimation] LayoutAnimation attributes

android:layoutAnimation (<LayoutAnimation>)
  • 設定一個 layout 或是 viewgroup 中的子view 的動畫,Layout中的每個元素都會採用同一個動畫效果,但開始時間不同。
  • delay: 定義每個子 View 開始的延遲時間。
  • animationOrder: 子 View 呈現動畫時的順序
    • 可以為 normal (正序), reverse, random。
  • animation: 每個子 View 所採用的動畫效果。
  • interpolator: 定義動畫的變化速率。
    • 可以加速,减速,重複顯示等。(動畫中每一時間的變化方式)


* Reference
- Android ApiDemos示例解析(95):Views->Animation->3D Transition | 引路蜂移动软件

[AndroidGraphic] Glossaries

  • Camera ( android.graphics.Camera )
    • 是一個可以讓你將 2D 物件在 3D 空間中移動,
    • 並將在其移動後的結果,畫在螢幕上的類別。
    • 其實是個 helper class。他提供一些 APIs ,讓你控制要如何在 3D 空間中移動,
    • 最後再產生出合適的 Matrix ,讓你套用到 Canvas 的座標體系上。
  • Bitmap
    • 可以來自 local resource/asset,也可以在程序中建立。
    • 功能相當於圖片的儲存空間。
  • Canvas
    • 和 Bitmap 緊密連繫。
    • 把 Bitmap 比喻為内容的話,Canvas 就是提供了眾多方法操作 Bitamp 的平台 (畫布)。
  • Paint
    • 和 Canvas 緊密連繫。
    • 是"畫板"上的筆刷工具,也用於設置 View 上的樣式。
  • Drawable
    • 如果說 Bitmap, Canvas and Paint 是看不見地在内存中畫圖,那麼 Drawable 就是把他們繪圖结果表現出来的接口。
    • 擁有多個子類,例如:BitmapDrawable, ShapeDrawable, LayerDrawable 等。


* Reference
- android.graphics.Camera, 3D 的效果 - Java-Jinguo - ITeye技术网站
- Android入门第十四篇之画图 - hellogv的专栏 - 博客频道 - CSDN.NET

[Android] System installer, Unmounted SD Card


如果安裝好 app 則立即點開啟 -> Home -> 點擊 app icon 開啟
  • 會在同個 task create another app (onCreate)。

如果 SD Card 發生不預期時移除
  • 系統則會殺掉正在使用 SD Card 的 process,再重啟(但不一定會重啟成功)。

[SQL][Java] Date


java.sql.date:  沒有時分秒

java date: 到時分秒

2012年3月24日 星期六

[iBatis] ||


'%'||#keyword#||'%'   
  • || 等同於字串所使用的 +  ->> '%keyword%' 
  • 若是 '%'keyword'%' 則會變成 '%''keyword''%'

若是操作兩張 tables 沒設 join or where condition 
  • 則結果會是兩張 tables 相乘

[Android] SyncAdapter (API 5)

AccountManager: 管理手機裡的帳號
  • The user enters credentials (username and password) once per account, granting applications access to online resources with "one-click" approval.
  • Can generate auth tokens for applications, so the application doesn't need to handle passwords directly.
Authenticators: Validate accounts


若有實做 AbstractAccountAuthenticator 則能在 Account & Sync 新增同步帳號;

新增時,會觸發做以下設定的 service:

    





* Reference
- SampleSyncAdapter - Sample Sync Adapter
- class AccountManager
- class AbstractThreadedSyncAdapter
- class AccountAuthenticatorActivity
- class AbstractAccountAuthenticator

[JPA] Path

Path => table field

[AndroidGraphic] ImageView

在 Android 中不允許 ImageView 在產生後,態態修改其長度和寬度,

所以要實現圖片放大缩小的功能,必須將原來的 ImageView 移除,

重新產生一个新的 ImageView,並且指定圖片來源给它,再放入Layout中。


設為 wrap_content 的 ImageView 在 onCreate 的時候就確定了 ImageView 的寬高,

並且给這個 ImageView 分配了内存空間。這個時候我們可以看成是静態設置。

可以理解成已經分配過的就不允许再操作了。


* Reference
- 用Matrix放大了图片,Imageview的大小却没改变

[AndroidLayout] Table Layout


  • The total width of the table is defined by its parent container.
  • The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT
    • However, the layout_height attribute can be defined by a child; default value is WRAP_CONTENT
    • If the child is a TableRow, then the height is always WRAP_CONTENT.
  • android:stretchColumns="1"
    • column 1 will fill the rest spaces
  • android:shrinkColumns
    • 指定不延伸寬度的 column,星號表示全部,或者以逗號區隔從 0 算起的 column index。
  • android:collapseColumns
    • 指定不顯示的 column,星號表示全部,或者以逗號區隔從 0 算起的 column index。
  • android:layout_span
    • 設定單一 cell 可以跨多個 column。
  • android:layout_column="0"
    • cell location from 0。

2012年3月11日 星期日

[AndroidMultimedia] Mediaplayer APIs

當在執行 MediaPlayer 中有錯,則會呼叫 OnErrorListener.onError()。


Prepared state
  • Preparing state is a transient state.
  • A MediaPlayer object must first enter the Prepared state before playback can be started.
  • When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface (setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)).
  • While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.
  • The call to prepare() can take a long time to execute, because it might involve fetching and decoding media data. So, you should never call it from your application's UI thread. 
    • To avoid hanging your UI thread, spawn another thread to prepare the MediaPlayer and notify the main thread when done. 
    • Convenient way to accomplish this task by using the prepareAsync() method. 


Start state
  • After start() returns successfully, the MediaPlayer object is in the Started state. isPlaying() can be called to test whether the MediaPlayer object is in the Started state.
  • While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate()
    • This callback allows applications to keep track of the buffering status while streaming audio/video.
  • Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine.


Stop state
  • Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.


Play position


When the playback reaches the end of stream, the playback completes.
  • The looping mode(setLooping(boolean)):
    • True: the MediaPlayer object shall remain in the Started state.
    • False, the player engine calls a user supplied callback method, OnCompletion.onCompletion(), (setOnCompletionListener(OnCompletionListener)) The invoke of the callback signals that the object is now in the PlaybackCompleted state.
  • While in the PlaybackCompleted state, calling start() can restart the playback from the beginning of the audio/video source.


Permission
  • <uses-permission android:name="android.permission.INTERNET" />
  • <uses-permission android:name="android.permission.WAKE_LOCK" />


States
  • Create successful -> Idle -> setDataResource() -> Initialized -> prepare() or prepareAsync() -> Prepared -> start()/pause()/seekTo() -> Started/Paused/PlaybackCompleted
  • Error -> reset() -> Idle
  • Calling start()/pause()/stop() has not effect on a MediaPlayer object that is already in the Started/Paused/Stopped state.



* Reference
- MediaPlayer API
MediaPlayer State Diagram **
- MediaPlayer Guide
- media-formats
-  利用Android的MediaPlayer播放影片並動態調整影片畫面大小

2012年3月3日 星期六

[AndroidLayout] ScrollView handy trick by Romain Guy

Set the android:layout_weight of the TextView to 1.0. By doing so I am forcing the text to use the available empty space when it is shorter than the ScrollView. This can only work when android:fillViewport="true" is set on the scroll view.

To understand this result, you must remember that android:layout_height="fill_parent" means "set the height to the height of the parent." This is obviously not what you want when using a ScrollView.

After all, the ScrollView would become useless if its content was always as tall as itself. To work around this, you need to use the ScrollView attribute called android:fillViewport. When set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect.

fill_parent is meaningless inside a ScrollView. What you want is to use wrap_content and add android:fillViewport="true" on the ScrollView.


* Reference
- ScrollView’s handy trick – Romain Guy

[AndroidLayout] android:weightSum and android:layout_weight

android:weightSum
  • If unspecified, the sum is computed by adding the layout_weight of all of the children.
  • Must be a floating point value, such as "1.2".

android:layout_weight
  • Linear layout提供此屬性,讓其所有的子顯示物件可以透過設定該屬性來表明自己的重要性。
  • 當所有有定義明確長寬的物件都好之後,剩下的空間分配給有宣告layout_weight屬性的物件,按照優先權使所有剩下空間被填滿。
  • 分配方式:
    • 若是指定在 child,值越分配到越空間。
    • ex: 假設剩下空間有三個View(A、B、C)要分配,其權值大小分別為3、1、1,因此它們所分配到的空間大小將會是3:1:1。
    • 若是指定在 parent,值越則分配到越空間。

利用這兩個屬性可以分配 LinearLayout 中 widgets 的比例:
This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

# Update on 2012/06/18
假設是兩個 widgets 要依比例分配寬,那麼此兩 widgets 的寬則應設為 0 dp,讓系統可調整其比例。


* Reference
- LinearLayout | Android Developers
- Chickenrice's Workshop: [Android] layout_weight的妙用-讓View的大小以百分比率顯示(proportionate size)
- milkmidi Blog: Android LinearLayout **

[AndroidManifest] uses-sdk

android:minSdkVersion
  • An integer designating the minimum API Level required for the application to run.
  • The Android system will prevent the user from installing the application if the system's API Level is lower than the value specified in this attribute.
  • Default value of "1", which indicates that your application is compatible with all versions of Android.
  • The application will crash during runtime when attempting to access the unavailable APIs.

android:targetSdkVersion
  • An integer designating the API Level that the application targets.
  • Default value equals that given to minSdkVersion.
  • This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version.
  • The application is still able to run on older versions (down to minSdkVersion).
  • The system may enable compatibility behaviors to ensure when system API level > target.
  • For example, 
    • setting this value to "11" or higher allows the system to apply a new default theme (Holo) to your app when running on Android 3.0 or higher
    • disables screen compatibility mode when running on larger screens (because support for API level 11 implicitly supports larger screens).
  • There are many compatibility behaviors that the system may enable based on the value you set for this attribute.
  • 也就是說,在執行時起作用,如果版本相同時,效率可能會高點。

android:maxSdkVersion
  • An integer designating the maximum API Level on which the application is designed to run.
  • In Android 1.5, 1.6, 2.0, and 2.0.1, the system checks the value of this attribute when installing an application and when re-validating the application after a system update.
  • In the case of re-validation after system update, this effectively removes your application from the device.
  • Declaring this attribute is not recommended.
    • By design, new versions of the platform are fully backward-compatible.
    • Declaring the attribute can result in your application being removed from users' devices after a system update to a higher API Level.
  • Future versions of Android (beyond Android 2.0.1) will no longer check or enforce the maxSdkVersion attribute during installation or re-validation. 
    • Google Play will continue to use the attribute as a filter, however, when presenting users with applications available for download.

target API level
指定要 compiler 的 API 版本。


* Reference
- uses-sdk
- minSdkVersion、targetSdkVersion、targetApiLevel的区别