2015年10月23日 星期五

Android / JavaScript color setting

目的:

使用色碼進行設定物件顏色

色碼產生:

http://www.color-hex.com/

添加透明度:

http://stackoverflow.com/a/25170174

Going off the answer from @BlondeFurious, here is some Java code to get each hex value from 100% to 0% alpha:

java code:

1
2
3
4
5
6
7
8
for (double i = 1; i >= 0; i -= 0.01) {
    i = Math.round(i * 100) / 100.0d;
    int alpha = (int) Math.round(i * 255);
    String hex = Integer.toHexString(alpha).toUpperCase();
    if (hex.length() == 1) hex = "0" + hex;
    int percent = (int) (i * 100);
    System.out.println(String.format("%d%% — %s", percent, hex));
}


output:

100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00



2015年10月14日 星期三

ListView滑動更換圖片

目的:

減少ListView上下滑動更換圖片時產生的Lag

操作:

在Main.java使用setOnScrollListener()偵測ListView是否滑動,若處於靜止的狀態時,就開始更換item上的圖片
**只要ListView初始化完成後,使用者開始滑動,一切更換圖片的code皆在setOnScrollListener()中的onScrollStateChange()中進行處理**

Main.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
MyAdapter adapter = null ;

ListView lv = new ListView(this) ;
lv.setAdapter(adapter = new MyAdapter()) ;

lv.setOnScrollListener( new OnScrollListener() {
                                        @Override
                                        public void onScrollStateChanged( AbsListView listview, int scrollState ) {
                                            // TODO Auto-generated method stub
                                            switch ( scrollState ) {
                                                case OnScrollListener.SCROLL_STATE_IDLE:
                                                    MyAdapter.scrolling = false ;

                                                    int first = listview.getFirstVisiblePosition() ;
                                                    int childCnt = listview.getChildCount() ; // listview可用的view總數
                                                    int listDataSize = lvAdapter.getListDataSize() ;
                                                    for ( int i = 0 ; i < childCnt ; ++i ) {
                                                        View convertView = listview.getChildAt( i ) ; // 取出目前需處理的item

                                                        // 取出ViewHolder後可以進行而外處理
                                                        MyAdapter.ViewHolder holder = (MyAdapter.ViewHolder) convertView.getTag() ;

                                                        convertView.setTag( R.image.used, "自訂此圖的記號" ) ;

                                                        //...更新item上的圖片
                                                    }
                                                    break ;
                                                default:
                                                    MyAdapter.scrolling = true ;
                                                    //...可以取消下載的進程
                                                    break ;
                                            }
                                        }

                                        @Override
                                        public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ) {
                                            // TODO Auto-generated method stub
                                        }
                                    } ) ;


在MyAdapter的getView()中處理當前使用item的圖片,是否與前一張圖是否相同,若相同就不處理,若不相同則代表需要更換新圖片在此item中
**畫面上起初所看到的幾張圖片在此處理並顯示,後續上下滑動事件時所更換圖片的code由ListView的setOnScrollListener()處理**

MyAdapter.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static boolean scrolling = false ;

@Override
public View getView( int position, View convertView, ViewGroup parent ) {
    // TODO Auto-generated method stub
    ViewHolder holder = null ;

    if ( convertView == null ) {
        //...
        convertView.setTag( holder ) ;
    } else {
        holder = (ViewHolder) convertView.getTag() ;
    }

    if ( !scrolling ) {
        //...畫面上起初所看到的幾張圖片在此處理並顯示

        // 記錄此item目前被誰所用
        convertView.setTag( R.image.used, "自訂此圖的記號" ) ;
    } else {
        String nowTag = "自訂此圖的記號" ;
        String preTag = String.valueOf( convertView.getTag( R.image.used ) ) ;

        // preTag未產生過則為null,或者有手動清除過
        // item目前使用的圖跟前一張圖是否相同,若相同則代表此圖未滑出螢幕範圍
        if ( preTag != null && !nowTag.equals( preTag ) ) {
            convertView.setTag( R.image.used, null ) ; // 清除此圖的記號
            //...清除此圖,為了下次此item滑到螢幕範圍內後,不會顯示上一張圖
        }
    }

    return convertView ;
}

2015年10月13日 星期二

在Blogger中新增CodeBlock

目的:
在blogger中新增CodeBlock

操作:
使用 http://hilite.me/ 轉換code後,將產生的內容貼到Blogger文章中即可

2015年7月24日 星期五

android 由程式觸發點擊事件(OnClickListener)

目的:

如何藉由程式直接觸發元件的點擊事件?

Button

Button.performClick() ;

TextView



TextView.performClick() ;


直接這樣做會發現到會發生NullPointer Exception


原因是有可能點擊事件雖然有設定了,但TextView為準備完成,所以造成NullPointer Exceptino,因此使用以下方式進行解決,

TextView.post( new Runnable() {
    public void run() {
        TextView.performClick() ;
    }
} ) ;

利用Runnable直接在interface thread中執行

reference:
http://stackoverflow.com/a/11035166

2015年7月21日 星期二

android 實線虛線、實現、圓角矩形、兩角圓角矩形


虛線在4.0以上無法正常顯示,所以對顯示虛線的元件關閉硬體加速,讓其可以正常顯示
可以針對單一元件關閉硬體加速:

.java

TextView.setLayerType( View.LAYER_TYPE_SOFTWARE, null )  ;


.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >

    <!-- dashWith:破折限寬 -->
    <!-- dashGap:線與線的空隙寬,當dashGap為0則為實線 -->
    <stroke
        android:dashGap="5dp"
        android:dashWidth="5dp"
        android:width="1px"
        android:color="@color/blue" />


</shape>


reference:
http://www.cnblogs.com/ansionchen/archive/2013/04/08/3019086.html

2015年5月25日 星期一

ListView的item中設定元件layout_weight

目的:

在listview的item中設定元件layout_weight

操作:

  1. 在item的xml中,每個元件設定layout_weight
  2. listview的設定,layout_width="match_parent"

Code:

main.xml

    <ListView
        android:id="@+id/totalReview_detailLV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>




item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2" >
    </TextView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </TextView>

</LinearLayout>

reference:
http://stackoverflow.com/a/28078042

2015年5月18日 星期一

SQLite using rawQuery

目的:

在android中使用SQLiteDatabase.rawQuery( query, whereArgs ) 進行query。

操作:

            String query = "SELECT r.id FROM %s c INNER JOIN %s r ON c.name = r.name WHERE c.id = ?" ;
            query = String.format( query, src_table_name, change_table_name, 0 ) ;
            String[] whereArgs = new String[] { String.valueOf( id ) } ;
            Cursor c = db.rawQuery( query, whereArgs ) ;

Q1. 「?」使用在table name位置時,android 會出現SQLException,無法執行。

A1. 使用String.format,搭配「%s」方式導入table name,但其他條件仍使用whereArgs方式,在query時帶入。

2015年5月17日 星期日

SQLite 時間

目的:

在SQLite中insert時,default值填入current time。

操作:

CREATE TABLE IF NOT EXISTS `test` (
  `curr_time` TIMESTAMP DEFAULT (datetime('now','localtime')) -- 時間
)

  1. `curr_time` TIMESTAMP DEFAULT (datetime('now','localtime')) :目前手機時間
  2. `curr_time` TIMESTAMP DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')) :目前手機時間
  3. `curr_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP :時間有可能非手機目前的時間

insert時間:

in Android:

            ContentValues contentValues = new ContentValues() ;
            contentValues.put( "curr_time", new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( new Date() ) ) ;
            success = ( db.update( "test", contentValues, null, null ) > 0 ) ;

2015年5月10日 星期日

DrawerLayout搭配ActionBar,設定HomeAsUp

目的:

第一頁使用側拉抽屜,HomeAsUp根據抽屜是否開啟而自動變換icon。
第二頁使用側拉抽屜,HomeAsUp直接固定使用返回icon,抽屜拉開不會改變icon。

元件:

  1. android.support.v4.widget.DrawerLayout
  2. android.support.v7.widget.ActionBarDrawerToggle

操作:

  1. 設定DrawerLayout 和 ActionBarDrawerToggle
  2. 設定啟用HomeAsUp:
    getSupportActionBar().setDisplayHomeAsUpEnabled( true ) ;
    getSupportActionBar().setHomeButtonEnabled( true ) ;
  3. 第一頁:
    mDrawerToggle.setDrawerIndicatorEnabled( truue ) ; // 預設
  4. 第N頁:
    mDrawerToggle.setDrawerIndicatorEnabled( false ) ; 

reference:
http://stackoverflow.com/q/17258020

2015年5月8日 星期五

如何使用全高的側拉抽屜?(DrawerLayout)

目的:


側拉抽屜出來的效果要高度填滿整個螢幕高(同play商店)。

PS.側拉抽屜預設效果會在ActionBar底下滑出


元件:

  1. android.support.v4.widget.DrawerLayout
  2. android.support.v7.widget.Toolbar


操作:

  1. 針對Activity的style 設定<item name="windowActionBar">false</item>
  2. 針對Activity的style 設定<item name="windowActionModeOverlay">true</item>
  3. class中設定setSupportActionBar( toolbar ) ;
**但是這樣getSupportActionBar()就會回傳null**

Q1:

只有一個頁面使用側拉抽屜,但其它頁面不需使用,但需仍需要使用ActionBar。
但是Activity的style中不設定<item name="windowActionBar">false</item>又不行。
若不設定的話,在使用setSupportActionBar( toolbar ) 將會發生Exception。

Error message:
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

A1:

  1. 另外設定一個style
  2. 在使用class頁面中的onCreate()的第一行、super.onCreate( savedInstanceState )前,設定額外的style
    setTheme( R.style.另外的style ) ;
  3. 其餘照舊

style code:

    <style name="HomeTheme" parent="AppBaseTheme">
        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>
    </style>



reference:
http://stackoverflow.com/a/12724687