當前位置:
首頁 > 最新 > Android不常見問題索引

Android不常見問題索引

照片來自@Bing。

1 android TypedValue.applyDimension()的作用

Android自帶的單位轉化函數。

intsize=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,context.getResources().getDisplayMetrics());

這裡COMPLEX_UNIT_SP是原始單位,20是數值,也就是20sp,輸出px數值。這裡的DisplayMetrics也可以這樣獲得:

Resources.getSystem().getDisplayMetrics()

2 AtomicInteger的用途

privatevolatileintvalue;

AtomicInteger提供原子操作來進行Integer的使用,通過線程安全的方式操作加減,因此十分適合高並發情況下的使用。但是使用volatile將使得VM優化失去作用,導致效率較低,所以要在必要的時候使用。

3 Rect和RectF之間的區別

4 @GuardedBy(lock)

描述哪個狀態變數被哪個鎖保護著,以及哪個鎖保護這些變數的信息

GuardedBy

5 SystemClock.sleep()方法與Thread.sleep()方法的區別

SystemClock.sleep()不需要考慮InterruptedException,而Thread.sleep()中可能會出現InterruptedException,容易導致假死奔潰。

publicstaticvoidsleep(longms)

{

longstart=uptimeMillis();

longduration=ms;

booleaninterrupted=false;

do{

try{

Thread.sleep(duration);

}

catch(InterruptedExceptione){

interrupted=true;

}

duration=start+ms-uptimeMillis();

}while(duration>);

if(interrupted){

// Important: we don"t want to quietly eat an interrupt() event,

// so we make sure to re-interrupt the thread so that the next

// call to Thread.sleep() or Object.wait() will be interrupted.

Thread.currentThread().interrupt();

}

}

查看源碼發現SystemClock.sleep()也是調用的Thread.sleep(),只是將異常捕獲住了。

6 JDK7二進位整數以及下劃線分隔符新特性

JDK7提供了下劃線分隔符,能夠按照自己的習慣進行數字的分割,例如:int b = 1_2312_3131;很容易知道這是1億2312萬3131。

7 SystemClock.uptimeMillis()、SystemClock.elapsedRealtime()和 System.currentTimeMillis()

8 RecycleView中的GridLayoutManager.SpanSizeLookup

參考RecyclerView 中setSpanSizeLookup 解釋

GridLayoutManager manager=newGridLayoutManager(context,2);

這裡定義了一個雙列的表格管理器。

manager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup(){

@Override

publicintgetSpanSize(intposition){

returnBasicCardCreator.useFullSpan(mAdapter.getItemViewType(position))?2:1;

}

});

這裡按位置判斷一行顯示單列還是雙列。

9 BuildConfig.DEBUG

if(BuildConfig.DEBUG){

//判斷是否是調試模式

Log.d(TAG,msg);

}

注意BuildConfig的來源包有幾個的,別導錯了。

10 setWillNotDraw和setFillViewport

setFillViewport(true)

It must be set to ScrollView and has the following efect : 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.

setWillNotDraw(false)

If this view doesn"t do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as ViewGroup. Typically, if you override onDraw(Canvas) you should clear this flag.

比如這個庫

classPagerSlidingTabStripextendsHorizontalScrollView{

publicPagerSlidingTabStrip(Context context,AttributeSet attrs,intdefStyle){

super(context,attrs,defStyle);

setFillViewport(true);

setWillNotDraw(false);

}

}

11 android popupwindow showAsDropDown 為何offsetx無效,offsety有效

showAsDropDown顯示的點是以anchorView左下角點為參照點.改為

showAsDropDown(anchorView,-anchorView.getWidth()-offsetX,-offsetY);

12 Android Support Library 23.2 特性

Vector Drawable以及Animated Vector Drawables

第一步:

android{

defaultConfig{

vectorDrawables.useSupportLibrary=true

}

}

第二步:準備好svg圖片,Android Studio帶有轉換功能。

第三步:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:srcCompat="@drawable/ic_add"/>

13 android:clipChildren和android:clipToPadding

clipToPadding:控制項的繪製區域是否繪製到padding為止,true表示不會繪製到padding中,默認值為true。

clip是修剪的意思。

參考:http://www.jianshu.com/p/2cbc9c12d221

clipChildren:是否限制子View在其範圍內,我們將其值設置為false後那麼當子控制項的高度高於父控制項時也會完全顯示,而不會被壓縮

Defines whether a child is limited to draw inside of its bounds or not. This is useful with animations that scale the size of the children to more than 100% for instance. In such a case, this property should be set to false to allow the children to draw outside of their bounds. The default value of this property is true.

注意:

參考:

14 Android中一些你可能沒注意的小效果實現

scrollview滾動條顏色,edittext游標之類的。

http://www.jianshu.com/p/f361123b0963

15 滾動條

設置滑動到頂部和底部的背景或顏色:

android:overScrollFooter="@android:color/transparent"

android:overScrollHeader="@android:color/transparent"

設置滑動到邊緣時無效果模式:

android:overScrollMode="never"

設置滾動條不顯示:

android:scrollbars="none"

整體設置:

android:id="@+id/rv_search_one"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:overScrollMode="never"

android:scrollbars="none"/>

滾動條的樣式和位置

android:scrollbarStyle

insideOverlay:默認值,表示在padding區域內並且覆蓋在view上

insideInset:表示在padding區域內並且插入在view後面

outsideOverlay:表示在padding區域外並且覆蓋在view上,推薦這個

outsideInset:表示在padding區域外並且插入在view後面

16 隱藏導航欄和狀態欄

隱藏導航欄

View decorView=getWindow().getDecorView();

intuiOptions=decorView.getSystemUiVisibility();

intnewUiOptions=uiOptions;

//隱藏導航欄

newUiOptions=View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

decorView.setSystemUiVisibility(newUiOptions);

隱藏狀態欄

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

或者

true

隱藏標題欄

requestWindowFeature(Window.FEATURE_NO_TITLE);

或者

false

true

17 獲取標題欄、狀態欄、導航欄高度

標題欄

狀態欄

publicstaticintgetStatusBarHeight(Context context){

int result=;

int resId=context.getResources().getIdentifier("status_bar_height","dimen","android");

if(resId>){

result=context.getResources().getDimensionPixelSize(resId);

}

returnresult;

}

導航欄

@TargetApi(14)

publicstaticintgetNavigationBarHeight(Context context){

// below version 4.4

if(Build.VERSION.SDK_INT

return;

}

// has navigation bar

if(getNavigationBarVisibility(context)){

int resourceId=Resources.getSystem().getIdentifier("navigation_bar_height","dimen",

"android");

if(resourceId>){

returnResources.getSystem().getDimensionPixelSize(resourceId);

}

}

return;

}

18 TextView去除默認的上下padding

includeFontPadding="false"

19 順暢地取消動畫效果

ValueAnimator.reverse();

20 Chales在Android7.0上不能抓包的問題

安卓上要裝兩個證書:

21 android:angle="45"

22 命令行打包

./gradlew iBiliPlayer-v4:assembleRelease--stacktrace

23 新建開源庫注意

以BiliFeed為例,application id為com.bilibilii.bilifeed。在新建項目的時候,Application Name用BiliFeed,Company domain用bilibili.com,這個沒問題。但是在新建庫的時候,如果想讓庫的包名以com.bilibili.bilifeed開頭的話,那麼就需要設置庫的名字為bilifeed,module的名字用別的(例如library)。然後,這個時候構建apk會報Multiple dex files define錯誤,需要在app項目的menifest文件中修改包名(例如com.bilibili.bilifeed.sample),然後標籤就需要通過包名的全稱導入了。然後需要非常注意的是,app項目的R文件,是包名開頭的,如果之前app有引用R,此時需要修改成com.bilibili.bilifeed.sample.R

24 一些屬性常量的定義方法

舉個栗子,我要在對象裡面保存一個flag變數,這個變數的表示該對象是否支持某一種或多種屬性,來看下如何定義這些屬性,如何給flag變數添加某種屬性,如何判斷flag變數是否支持某種屬性。

publicstaticfinalintCLICKABLE=0x01;

publicstaticfinalintLONG_CLICKABLE=0x02;

privateintmFlags;

publicvoidsetFlag(intflag){

mFlags=flag;

}

/**

*

* @param flag {@link #CLICKABLE#LONG_CLICKABLE}

*/

publicbooleanisSupport(intflag){

returnmFlags&flag;

}

通過或操作來設置屬性,通過與操作來獲取屬性。

byte轉int為什麼要& 0xff?

byte有8位,int有32位,如果要轉換的byte是個負數,比如-2,二進位補碼錶示為1111 1110。直接轉換為int後,高24位進行補1(窄的整型轉換成較寬的整型時符號擴展規則:如果最初的數值類型是有符號的,那麼就執行符號擴展(即如果符號位 為1,則擴展為1,如果為零,則擴展為0)),為11111111 11111111 111111111111 1110。此時我們不管二進位轉化為十進位後的值是正確的(還是-2),但是二進位數據已經錯了,因為它比原始的二進位數據多了24個1。

執行& 0xff操作以後,高24位全部會變成0。這時候二進位數據是跟原來一樣的,轉化成十進位是錯的(不是-2了)。

所以個人理解,執行& 0xff操作是為了在非數值計算的場景下保持byte數據轉化前後的二進位值是一致的。


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 全球大搜羅 的精彩文章:

湖人的比賽還用看?醒來又打贏強隊了!

TAG:全球大搜羅 |