發表文章

目前顯示的是 3月, 2019的文章

Screen Size 螢幕尺寸 與 文字大小

        DisplayMetrics dm=new DisplayMetrics();         getWindowManager().getDefaultDisplay().getMetrics(dm);         displayWidth=dm.widthPixels;         displayHeight=dm.heightPixels; 用這個方法我們可以得到螢幕的尺寸。有了螢幕的尺寸,我們就可以設定文字的大小,讓文字大小跟著螢幕尺寸改變。 int contentWordSize=11; //一列11個字。 TextView tv=findViewById(R.id.xxxx); tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, displayWidth/contentWordSize); 這樣是不是很方便!

改變 drawable 的顏色

public static Drawable tintDrawable(Drawable drawable, int color) {         Drawable wd = DrawableCompat.wrap(drawable);         DrawableCompat.setTint(wd, color);         return wd;     } 只要將使用中的drawable,用上面的方法處理,再拿來用,drawable的顏色就會改變。 這個方法可以用來處理ImageButton點擊後,ImageButton變色。方便許多。 ImageButton ibtn = findViewById( R.id.xxxx ); iBtn.setBackground(getResources().getDrawable(R .drawable.xxxx )); ibtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ibtn.setBackground(tintDrawable(getResources().getDrawable( R.drawable.xxxx ) .mutate() , Color.Green )); new Handler().postDelayed(new Runnable() { @Override public void run() { iBtn.setBackground(getResources().getDrawable(R .drawable.xxxx )); } }, 1000); } }); 現在ImageButton點擊後,會將原來的圖變成Color.GREEN的

PendingIntent 速記

什麼是PendingIntent,字面上來看就是 即將發生的intent。 我們知道如果要開始一個Intent,可以如下 Intent x1 = new Intent( this, xxx.class); startActivity(x1); 通常我們是要開啟一個新的activity會用到Intent。我們更應該把 Intent理解為Activity間的一個訊息的傳遞。也就是由Intent這個信使,帶著訊息,告知Activity要啟動了。這樣理解,我們可以推論,Intent可以帶著訊息通知其它class,讓他們啟動。 如 Service Intent x1 = new Intent(this, xxxService.class); startService(x1); 但有時候,我們有一個Intent,我們不想用 startActivity, 或是 startService去啟動,而是希望它等待至某種要求出現才啟動。這就需要用到 PendingIntent。 所以簡單的說,PendingIntent就是把一個已知的 Intent 包起來。然後pending(即將發生)。 我們看一下PendingIntent有幾個靜態方法 包了一個Activity的Intent public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) 包了一個Broadcast的Intent public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) 包了一個Service的Intent public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags) 包了多個Activity的Intent public static PendingIntent getActivities(Context context, int requestCode,

Android 自動產生 VIEW ID

動態產生 Layout 時一定會遇到 ID配置的問題。 在API > 17後有提供一個  View.generateViewId(),只要在新增的LAYOUT物件設定即可 Button btn = new Button(this); btn.setId(View.generateViewId()); 如果需要API<17要如何是好?我們深入View.generateViewId()看看。 private static final AtomicInteger sNextGeneratedId = new AtomicInteger( 1 ); public static int generateViewId () { for (;;) { final int result = sNextGeneratedId.get(); // aapt-generated IDs have the high byte nonzero; clamp to the range under that. int newValue = result + 1 ; if (newValue > 0x00FFFFFF ) newValue = 1 ; // Roll over to 1, not 0. if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } } 看樣子,是用AtomicInteger來增加ID。利用compareAndSet(CAS)來查newValue是否可以置入result的記憶體位址中。另外我們也看到,newValue最高到0x00FFFFFF,0x00FFFFFF以上就會從1再開始。也代表0x00FFFFFF以上是保留給系統工具使用。 所以,g