2015年2月21日 星期六

[Android] ImageView adjustViewBounds 圖 自動調整大小


如果imageview設定
android:layout_width="fill_parent"
android:layout_height="wrap_content"
當圖的寬大於imageview的寬,會自動縮成如下,其中還有ScaleType的多種選項可用,但之前一直遇到的問題是,不論怎麼設定ScaleType,下方黃色空間還是會被imageview佔住,想放textview還是button都會被遮住,
If I set imageview like these,
android:layout_width="fill_parent"
android:layout_height="wrap_content"
When the width of picture is larger than the imageview, the picture will be auto scaled to fit the imageview. There are many ScaleType option that can be set. The problem is, no matter how I set the ScaleType, the yellow area is occupied by the imageview

後來找了一陣子發現只要加上這行
android:adjustViewBounds="true"
imageview就會自動縮成picture的大小,還你黃色的空間。

After studying from internet, I got this
android:adjustViewBounds="true"
The imageview will auto scale to the size of picture, and then you can use the yellow area.



[Android] supportv4 fragment camera 問題

第一次使用supportv4 TabManager TabHost的功能來製作fragment分頁,在呼叫內建相機返回之後,有機率遇到java.lang.IllegalStateException: Fragment already added:的錯誤,網路上有很多相關討論,但感覺和此並沒有很相關。試了一陣子,發現原來是內建相機預設是橫向模式(landscape),照出來的照片會自動轉+-90度,在返回fragment的時候,這照片有機會自己再轉-+90度,(即使手機的畫面旋轉關閉),導致(個人猜測)supportv4誤判而再把fragment全部加回去,造成錯誤。

解決方法:因為此app只打算直式使用,所以manifest加入
<activity android:name="xxx.xxx.YourFragmentActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|screenSize"/>

It's my first time to use the function of supportv4 TabManager TabHost to make fragments. When calling the camera and back to the fragment, the error "java.lang.IllegalStateException: Fragment already added:" may occurs. There are many discussion about this error, but few about this case. After trying for hours, I suppose the reason is: The default android camera is landscape. After taking a picture, the picture may automatically rotates +-90 degree(even the auto rotation is off). When back to a fragment, the picture rotates -+90 degree again. supportv4 may make a wrong decision to add all the fragment back and error occurs.

Solution:Since this app is design for portrait mode, add these in manifest
<activity android:name="xxx.xxx.YourFragmentActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|screenSize"/>