setPivotX(0)
圖上的位置 非螢幕上位置
getLeft
relative parent 從 parent 上算來的位置 (由 parent's top and left 為初始)
2013年9月2日 星期一
2013年8月25日 星期日
[Android] rating bar size
Error
使用 minHeight and maxHeight 會切掉星星。
Solution
android:scaleX="0.8"
android:scaleY="0.8"
可以縮小 但效果不佳 難以控制位置
最好的方式 直接使用希望呈現的大小圖片
* Reference
- How to set the custom size of the stars in RatingBar
- android ratingbar size
使用 minHeight and maxHeight 會切掉星星。
Solution
android:scaleX="0.8"
android:scaleY="0.8"
可以縮小 但效果不佳 難以控制位置
最好的方式 直接使用希望呈現的大小圖片
* Reference
- How to set the custom size of the stars in RatingBar
- android ratingbar size
[Android] - tag requires a 'drawable' attribute or child tag defining a drawable
Error Message
想在 selector 中使用顏色做為 drawable
<item> tag requires a 'drawable' attribute or child tag defining a drawable
Solution
要放在 /res/color/list_item_selector.xml
想在 selector 中使用顏色做為 drawable
<item> tag requires a 'drawable' attribute or child tag defining a drawable
Solution
要放在 /res/color/list_item_selector.xml
[Android] AdapterView
- after set adapter default select position 0.
- 若被選的位置和原本的相同 那麼不會觸發 onItemSelected.
- setSelection(position)
- position 那個 item 會被移動到 top (not touch mode)
- getChildAt(index)
- index: 畫面的位置。
- 所以如果 index 是在不可視的位置 可能會得到 null。
* Related post:
[ListView] The World of List View
[Android] ListView Tips
2013年2月21日 星期四
[Android] Optimised ImageView
在 4.0 前,使用 ImageView.setImageDrawable(),會執行下列動作:
step 1 and step 3 是必要的
step 2 是定位 drawable 自 ImageView 中的位置,因此只需要在 drawable 大小改變時使用到。
也就是若 AdapterView 中使用的都是大小相同的 drawable,則能省略 step 2,讓滑動的效能獲得改善。
在 4.0 中會先比對大小才呼叫 requestLayout()(run the step 2),4.0 後則能使用 reference 中所提供的 OptimisedImageView。
Relative post
- [Android] Avoid Memory leak
* Reference
- Snippet: ImageView Layout Optimisation | senab
- Set the new Drawable (and keep a reference to it).
- Request a layout pass.
- Invalidate itself to display the new contents.
step 1 and step 3 是必要的
step 2 是定位 drawable 自 ImageView 中的位置,因此只需要在 drawable 大小改變時使用到。
也就是若 AdapterView 中使用的都是大小相同的 drawable,則能省略 step 2,讓滑動的效能獲得改善。
在 4.0 中會先比對大小才呼叫 requestLayout()(run the step 2),4.0 後則能使用 reference 中所提供的 OptimisedImageView。
Relative post
- [Android] Avoid Memory leak
* Reference
- Snippet: ImageView Layout Optimisation | senab
2012年12月2日 星期日
[Android] Touch events trigger order
- ViewGroup#onInterceptorTouchEvent()
- View#onTouchEvent()
- View#onTouch()
目前了解
以上 method 回傳 true 則表示會處理 event,該 event 則不會再被 dispatcher
若是回傳 false,則表示不會處理,因此也不會再接收到後續 event (這邊有點不確定!)
* Reference
- onInterceptTouchEvent和onTouchEvent调用时序 - ddna的专栏 - 博客频道 - CSDN.NET
- ViewGroup | Android Developers
- MotionEvent事件在onInterceptTouchEvent()、onTouchEvent()中的传递顺序 | 第三极 | 移动开发者
- onTouchEvent 、onInterceptTouchEvent的顺序~
- 两分钟彻底让你明白Android中onInterceptTouchEvent与onTouchEvent(图文)! - Android_Tutor的专栏 - 博客频道 - CSDN.NET
2012年12月1日 星期六
[AndroidLayout] getWidth() and getHeight() return 0
getWidth()
取得 view 的實際所佔寬度。
getMeasuredWidth()
view 在 parent 估計會佔的寬度,是最近一次在 measure() 中計算的值。
Solution1
Solution2
[AndroidLayout] getWidth() and getHeight()
* Reference
- getWidth和getMeasuredWidth的正解 - rui313的专栏 - 博客频道 - CSDN.NET
- android - How do you to retrieve dimensions of a view? Getheight() and Getwidth() always return zero - Stack Overflow
- Android初级教程_获取Android控件的宽和高 **
取得 view 的實際所佔寬度。
getMeasuredWidth()
view 在 parent 估計會佔的寬度,是最近一次在 measure() 中計算的值。
Solution1
final TextView tv = (TextView)findViewById(R.id.image_test); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { LayerDrawable ld = (LayerDrawable)tv.getBackground(); ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });
Solution2
[AndroidLayout] getWidth() and getHeight()
* Reference
- getWidth和getMeasuredWidth的正解 - rui313的专栏 - 博客频道 - CSDN.NET
- android - How do you to retrieve dimensions of a view? Getheight() and Getwidth() always return zero - Stack Overflow
- Android初级教程_获取Android控件的宽和高 **
[AndroidLayout] status selector
<item android:state_selected="true" android:state_pressed="true"></item>
selector status 的判斷 是在行為發生時生效
若想維持該狀態下的設定
可藉 APIs
button.setEnabled(true);
button.setSelected(false);
設定 widgets 的狀態 以固定 selector 中的對應結果
selector status 的判斷 是在行為發生時生效
若想維持該狀態下的設定
可藉 APIs
button.setEnabled(true);
button.setSelected(false);
設定 widgets 的狀態 以固定 selector 中的對應結果
2012年10月20日 星期六
[Android] Dialog error - Unable to add window token null is not for an application
Error Message
Unable to add window token null is not for an application
Occurred when
使用 getApplication() 去建立 dialog,必須使用 Activity (extends Context) 才行。
* Reference
- AlertDialog显示错误 Unable to add window token null is not for an application - Trinea @Java @Android 体验 技术 营销 - ITeye技术网站
Unable to add window token null is not for an application
Occurred when
使用 getApplication() 去建立 dialog,必須使用 Activity (extends Context) 才行。
* Reference
- AlertDialog显示错误 Unable to add window token null is not for an application - Trinea @Java @Android 体验 技术 营销 - ITeye技术网站
2012年10月10日 星期三
[Android] ListView Tips
In order to avoid blending and make the rendering faster while scrolling/flinging, the ListView widget uses a ’scrolling cache’ mechanism.
CharArrayBuffer
Cursor#getString() 每次呼叫都會建立 String objects,而且這些 objects 當 user scroll ListView 時就會被回收,所以為避免這樣的建立與回收物件,CharArrayBuffer 儲存 cursor 中的 data,然後直接從 CharArrayBuffer 中存取,reuse。
Using a color as part of your list selector may entirely fill your ListView.
This problem comes from the fact a ColorDrawable does not respect its bounds: it entirely fills the clip rect of the Canvas in which it is drawn into.
Solution
Use a Drawable that respect its bounds such as a GradientDrawable or a NinePatchDrawable.
- AndroidDevBlog » ListView Tips & Tricks #2: Sectioning your ListView
- AndroidDevBlog » ListView Tips & Tricks #3: Create fancy ListViews
- Why is my list black? An Android optimization | Android Developers Blog
- ListView 背景預設為透明色,所以會看到 window 預設的背景色,深灰色 (#FF191919)
- 為了滑動效能,the Android framework reuses the cache color hint.
- 若 scrolling cache is true,那麼當 color hint is set,the framework 複製 ListView 中的每個 child 為 bimap 並且設該 hint value.
- the framework copies each child of the list in a Bitmap filled with the hint value. ListView then blits these bitmaps directly on screen and because these bitmaps are known to be opaque, no blending is required. (?)
- This mechanism consists on taking an opaque screenshot of all visible child Views (in other words, it draws the content of each itemview in a Bitmap) and directly draws those screenshots when necessary.
- 因為預設的 cache color hint is #191919,所以在滑動時會看到 item 的黑色背景。
- The color used to make those screenshots opaque is the one given by the cache color hint.
- If your ListView turns opaque (black for instance) when scrolling that because you need to set the cache color hint to the actual color of your ListView background.
CharArrayBuffer
Cursor#getString() 每次呼叫都會建立 String objects,而且這些 objects 當 user scroll ListView 時就會被回收,所以為避免這樣的建立與回收物件,CharArrayBuffer 儲存 cursor 中的 data,然後直接從 CharArrayBuffer 中存取,reuse。
Using a color as part of your list selector may entirely fill your ListView.
This problem comes from the fact a ColorDrawable does not respect its bounds: it entirely fills the clip rect of the Canvas in which it is drawn into.
Solution
Use a Drawable that respect its bounds such as a GradientDrawable or a NinePatchDrawable.
* Reference
- AndroidDevBlog » ListView Tips & Tricks #2: Sectioning your ListView
- AndroidDevBlog » ListView Tips & Tricks #3: Create fancy ListViews
- Why is my list black? An Android optimization | Android Developers Blog
[Android] background of android.R.style.Theme_Holo_Light_Dialog
Situation
在 Activity 中設定 android.R.style.Theme_Holo_Light_Dialog
並將此 theme 的背景設為半透明
(Using custom style extends android.R.style.Theme_Holo_Light_Dialog)
開啟畫面時 預設會 focus 在 editText 而跳出 soft keyboard
dialog background 會由半透明變成全黑
But! 相同方式 若是在 AndroidManifest.xml 中設定 theme
即使跳出 soft keyboard 背景也不會變黑?!
Record
soft keyboard is a dialog on window top.
而預設的 soft keyboard dialog style 的背景是黑色的
是否是因為此背景使得 Activity 也轉為黑色背景?
* Reference
- How is the soft keyboard in Android drawn? - Stack Overflow
在 Activity 中設定 android.R.style.Theme_Holo_Light_Dialog
並將此 theme 的背景設為半透明
(Using custom style extends android.R.style.Theme_Holo_Light_Dialog)
開啟畫面時 預設會 focus 在 editText 而跳出 soft keyboard
dialog background 會由半透明變成全黑
But! 相同方式 若是在 AndroidManifest.xml 中設定 theme
即使跳出 soft keyboard 背景也不會變黑?!
Record
soft keyboard is a dialog on window top.
而預設的 soft keyboard dialog style 的背景是黑色的
是否是因為此背景使得 Activity 也轉為黑色背景?
* Reference
- How is the soft keyboard in Android drawn? - Stack Overflow
[Android] Dialog style attributes
- @null
- true
- false
- true
- @color/transparent
- false
* Reference
- android 自定义Dialog背景透明及显示位置设置 - J健 - 博客园
[Android] setContentView of Dialog
Error Message
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
Solution
一定要在 dialog.setContentView() 呼叫 dialog.show()。
* Reference
- android alertdialog布局 dialog布局 - - ITeye技术网站
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
Solution
一定要在 dialog.setContentView() 呼叫 dialog.show()。
* Reference
- android alertdialog布局 dialog布局 - - ITeye技术网站
[AndroidView] Clickable widget in ListView item
Occurred when
如果是 button 則會接收走 click item event,使得 ListView 沒有接收到 click item event; 但若是 TextView 則不會有此現象。
Solution
在 button 設定以下屬性
android:focusable="false"
* Reference
- android - ListView and Buttons inside ListView - Stack Overflow
- [转]ListView事件的研究 - 雪夜&流星 - 博客园
如果是 button 則會接收走 click item event,使得 ListView 沒有接收到 click item event; 但若是 TextView 則不會有此現象。
Solution
在 button 設定以下屬性
android:focusable="false"
* Reference
- android - ListView and Buttons inside ListView - Stack Overflow
- [转]ListView事件的研究 - 雪夜&流星 - 博客园
2012年6月15日 星期五
[AndroidLayout] Inflate layout
Error Message
06-11 16:31:59.046: E/ListView(28360): at java.lang.reflect.Method.invoke(Method.java:507)
06-11 16:31:59.046: E/ListView(28360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-11 16:31:59.046: E/ListView(28360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-11 16:31:59.046: E/ListView(28360): at dalvik.system.NativeStart.main(Native Method)
Solution
inflate layout 傳入參數為
inflate(R.layout.items_row, parent, false);
or
inflate(R.layout.items_row, null);
* Reference
- Android: Custom ListAdapter extending BaseAdapter crashes on application launch
06-11 16:31:59.046: E/ListView(28360): at java.lang.reflect.Method.invoke(Method.java:507)
06-11 16:31:59.046: E/ListView(28360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-11 16:31:59.046: E/ListView(28360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-11 16:31:59.046: E/ListView(28360): at dalvik.system.NativeStart.main(Native Method)
Solution
inflate layout 傳入參數為
inflate(R.layout.items_row, parent, false);
or
inflate(R.layout.items_row, null);
* Reference
- Android: Custom ListAdapter extending BaseAdapter crashes on application launch
2012年6月9日 星期六
2012年5月20日 星期日
[AndroidLayout] Input Method Editors
若沒有做任何設定與調整,在輸入時所跳出的鍵盤(IMEs, Input Method Editors)很有可能會擋住正在輸入文字的元件,可以用以下方式處理:
android:windowSoftInputMode ="adjustPan"
android:windowSoftInputMode ="adjustResize"
* Reference
- android:windowSoftInputMode
- Android Development Tips: Resizing layout while on-screen keyboard is displayed
- Issue 5497: adjustResize windowSoftInputMode breaks when activity is fullscreen
- Android windowSoftInputMode – Resize the application for the soft-keyboard
- Android: Soft Keyboard resizes background image
- android软键盘弹出引起的各种不适终极解决方案
- Android软键盘的隐藏显示研究 ***
- 关于android的输入法弹出来 覆盖输入框的问题
- Issue 14596: adjustPan not working correctly in landscape mode
- 畫面中使用 scrollView,讓使用者可以自己適當的調整畫面位置。
- 在該 activity 設 android:windowSoftInputMode ="adjustPan|adjustResize",讓系統幫忙處理。
android:windowSoftInputMode ="adjustPan"
- Can prevent your background from resizing.
- 不會呼叫 View.onSizeChanged()。
- 不會調整 layout 所以不能保證能看到整個 layout 但會保持必能看到輸入框(平移)。
- 設為 fullscreen 有時會使此參數失效,所以若是用 webview fullscreen 得注意。
android:windowSoftInputMode ="adjustResize"
- 把原本的 layout(include background!) 往上頂,移出空間給 IME,但僅發生在 application 有可調整的空間。
- 沒加此的話,不會一開啟就 focus on editText(?);加的話,一開啟就會 focus on 且有 soft input(?。
- 會呼叫 View.onSizeChanged()。
- 設為 fullscreen 有時會使此參數失效,所以若是用 webview fullscreen 得注意。
* Reference
- android:windowSoftInputMode
- Android Development Tips: Resizing layout while on-screen keyboard is displayed
- Issue 5497: adjustResize windowSoftInputMode breaks when activity is fullscreen
- Android windowSoftInputMode – Resize the application for the soft-keyboard
- Android: Soft Keyboard resizes background image
- android软键盘弹出引起的各种不适终极解决方案
- Android软键盘的隐藏显示研究 ***
- 关于android的输入法弹出来 覆盖输入框的问题
- Issue 14596: adjustPan not working correctly in landscape mode
2012年5月5日 星期六
[AndroidLayout] declare-styleable and TypedArray
自定 View,且定訂屬性可以使用在 layout xml 中並取得其值。
Define attributes in <declare-styleable>。
extends View,畫出自己的圖,也可取得 xml 中的設定值。
可在 layout xml 中引用自訂的 View。
* Reference
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用 - jincf2011的专栏 - 博客频道 - CSDN.NET
- Declaring a custom android UI element using XML
- public void recycle ()
- Define attributes in <declare-styleable>。
- extends View,畫出自己的圖,也可取得 xml 中的設定值。
- 可在 layout xml 中引用自訂的 View。
Define attributes in <declare-styleable>。
extends View,畫出自己的圖,也可取得 xml 中的設定值。
public class CustomView extends View { private Paint paint; public CustomView(Context context,AttributeSet attrs) { super(context,attrs); paint = new Paint(); // 取出自定的 attributes from MyView. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); // 個別取出 attribute,要給預設值,以在 xml 中沒有定義時使用。 int textColor = a.getColor(R.styleable.MyView_textColor, Color.WHITE); float textSize = a.getDimension(R.styleable.MyView_textSize, 36); paint.setTextSize(textSize); paint.setColor(textColor); // Give back a previously retrieved StyledAttributes, for later re-use. a.recycle(); } }
可在 layout xml 中引用自訂的 View。
* Reference
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用 - jincf2011的专栏 - 博客频道 - CSDN.NET
- Declaring a custom android UI element using XML
- public void recycle ()
2012年4月1日 星期日
[AndroidDev] merge
- The <merge /> tag was created for the purpose of optimizing Android layouts by reducing the number of levels in view trees.
- 如果 root layout 的大小設為和 parent 一樣 (ex: filll_parent)並且沒其他參數調整需求,那麼其實此 root layout 在實質上是沒有作用的,這時候便可以改以 merge 做為你的 root layout,這樣一來 children 便會直接被加在 merge's parent 上而能少拉一層 view。
- Since our FrameLayout has the same dimension as its parent, by the virtue of using the fill_parent constraints, and does not define any background, extra padding or a gravity, it is totally useless.When the LayoutInflater encounters this tag, it skips it and adds the <merge /> children to the <merge /> parent. (The children will be added directly to the top-level FrameLayout.)
- 但 merge 是不能用來取代 LinearLayout 的!
- You could not apply this trick if your layout was using a LinearLayout as its root tag for instance. The <merge /> can be useful in other situations though.
- The <merge /> tag is extremely useful and can do wonders in your code. However, it suffers from a couple of limitations:
- <merge /> can only be used as the root tag of an XML layout
- When inflating a layout starting with a <merge />, you must specify a parent ViewGroup and you must set attachToRoot to true (see the documentation for inflate(int, android.view.ViewGroup, boolean) method)
* Reference
- Layout Tricks: Merging Layouts | Android Developers
- Android里merge和include标签的使用_云开雾散_百度空间
[AndroidDev] Window Backgrounds & UI Speed
In this article, you will discover how to speed up the drawing and the perceived startup time of your activities. Both these techniques rely on a single feature, the window's background drawable.
* Reference
- Android Developers Blog: Window Backgrounds & UI Speed
- When you setup your user interface by calling setContentView() on an Activity.
- 該 activity's window 會被加上你所設的 view 以及 Android 為你所建立的 views。(其中最重要的是 DecorView.)
- Android adds your views to the Activity's window. The window however does not contain only your views, but a few others created for you. (The most important one is, in the current implementation is the DecorView.)
- The DecorView is the view that actually holds the window's background drawable.
- 藉由呼叫 getWindow().setBackgroundDrawable() 便可以改變 activity's window background 也就是 DecorView's background drawable.
- Calling getWindow().setBackgroundDrawable() from your Activity changes the background of the window by changing the DecorView's background drawable.
- If you are using the standard Android themes, a default background drawable is set on your activities. The standard theme currently used on the T-Mobile G1 uses for instance a ColorDrawable.
- 但此預設背景其實是會影響效能的!
- An easy way to make such an application draw faster is to remove the background drawable. Since the user interface is entirely opaque, drawing the background is simply wasteful. Removing the background improves the performance quite nicely.
- Removing the window's background can be achieved very easily by using a custom theme. To do so, first create a file calledres/values/theme.xml containing the following:
- This trick comes in very handy for any app that uses a MapView, a WebView or any other full screen opaque view.
- Using a theme to change the window's background is also a fantastic way to improve the perceived startup performance of some of your activities. This particular trick can only be applied to activities that use a custom background, like a texture or a logo.
- 如果是在 XML layout 或是 onCreate() 中設定背景,那麼 user 開啟 activity 時會先看到 default theme and its dark background. 等到 content view and first layout/drawing 被 inflate 才會顯示你所設定的背景。
- If this application simply set the wooden background in the XML layout or in onCreate() the user would see the application startup with the default theme and its dark background. The wooden texture would only appear after the inflation of the content view and the first layout/drawing pass.
- 改善方式:
- 將背景設定在 activity theme 中,這樣系統在 application 開始時會已最快的速度載入該 theme,所以 user 再也不會先看到 default background。
- Instead, the application defines the wooden background in a theme, picked up by the system as soon as the application starts. The user never sees the default theme and gets the impression that the application is up and running right away.
- ex: The same exact trick is used in the Google Maps application that ships with the T-Mobile G1. When the application is launched, the user immediately sees the loading tiles of MapView. This is only a trick, the theme is simply using a tiled background that looks exactly like the loading tiles of MapView.
* Reference
- Android Developers Blog: Window Backgrounds & UI Speed
訂閱:
文章 (Atom)