2014年12月16日 星期二

Android-TextView多行本文滾動輕鬆實現

Android中我們為了實現文本的滾動可以在ScrollView中嵌入一個TextView,其實TextView自己也可以實現多行滾動的,畢竟ScrollView必須只能有一個直接的子類佈局。只要在layout中簡單設置幾個屬性就可以輕鬆實現
1
2
3
4
5
6
7
8
9
  <TextView  
    android:id="@+id/tvCWJ"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:scrollbars="vertical"   <!--垂直滾動條 -->
    android:singleLine="false"       <!--實現多行 -->
    android:maxLines="15"            <!--最多不超過15-->
    android:textColor="#FF0000"
    />

當然我們為了讓TextView動起來,還需要用到TextViewsetMovementMethod方法設置一個滾動實例,代碼如下:
1
2
TextView tvAndroid123 = (TextView)findViewById(R.id.tvCWJ);   
tvAndroid123.setMovementMethod(ScrollingMovementMethod.getInstance());

2014年11月12日 星期三

Android學習_建立程式內(application)的全域變數(Global Variable)

Android學習_建立程式內(application)的全域變數(Global Variable)

在Activity間傳遞變數可以利用Bundle的方式來處理,但是如果當Activity太多,又每一個都可能運用到相同的參數,如每一個頁面都要去判斷登入者來決定可使用的功能,這時候可能會將值儲存在所謂的全域變數。

步驟:
1. 建立繼承於Application的類別

package tw.com.ola;
import android.app.Application;
public class GlobalVariable extends Application {
public String UserID = "";
}
說明:
*必須要繼承Application
*可以自行撰寫getter或setter,就跟一般的類別檔一樣
*上述程式碼僅建立一個外部可讀寫的變數UserID

2. 設定AndroidManifest

<application android:name=".GlobalVariable" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity_CheckAndroid" android:label="@string/Activity_CheckAndroid_Title" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
說明:
*一般AndroidManifest中有一個application標籤,原本沒有android:name屬性,將第一步驟建立的類別檔加入,例:android:name=".GlobalVariable"。

3. 將值放入與將值讀出

//放入
GlobalVariable globalVariable = (GlobalVariable)context.getApplicationContext();
globalVariable.UserID = key_UserID;

//讀出
GlobalVariable globalVariable = (GlobalVariable)context.getApplicationContext();
String UserID = globalVariable.UserID;
說明:
*如此就可以將各個Activity都需要的變數,進行儲存與讀取的動作。

2014年9月1日 星期一

Android Intent的几种用法全面总结

Intent应该算是Android中特有的东西。你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料。都指定好后,只要调用startActivity(),Android系统会自动寻找最符合你指定要求的应用程序,并执行该程序。

下面列出几种Intent的用法
显示网页:
  1. Uri uri = Uri.parse("http://www.google.com");
  2. Intent it  = new Intent(Intent.ACTION_VIEW,uri);
  3. startActivity(it);
复制代码
显示地图:
  1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
  2. Intent it = new Intent(Intent.Action_VIEW,uri);
  3. startActivity(it);
复制代码
路径规划:
  1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
  2. Intent it = new Intent(Intent.ACTION_VIEW,URI);
  3. startActivity(it);
复制代码
拨打电话:
调用拨号程序
  1. Uri uri = Uri.parse("tel:xxxxxx");
  2. Intent it = new Intent(Intent.ACTION_DIAL, uri);  
  3. startActivity(it);  
复制代码
  1. Uri uri = Uri.parse("tel.xxxxxx");
  2. Intent it =new Intent(Intent.ACTION_CALL,uri);
  3. 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
复制代码
发送SMS/MMS
调用发送短信的程序
  1. Intent it = new Intent(Intent.ACTION_VIEW);   
  2. it.putExtra("sms_body", "The SMS text");   
  3. it.setType("vnd.android-dir/mms-sms");   
  4. startActivity(it);  
复制代码
发送短信
  1. Uri uri = Uri.parse("smsto:0800000123");   
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  3. it.putExtra("sms_body", "The SMS text");   
  4. startActivity(it);  
复制代码
发送彩信
  1. Uri uri = Uri.parse("content://media/external/images/media/23");   
  2. Intent it = new Intent(Intent.ACTION_SEND);   
  3. it.putExtra("sms_body", "some text");   
  4. it.putExtra(Intent.EXTRA_STREAM, uri);   
  5. it.setType("image/png");   
  6. startActivity(it);
复制代码
发送Email

  1. Uri uri = Uri.parse("mailto:xxx@abc.com");
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. startActivity(it);
复制代码

  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
  3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
  4. it.setType("text/plain");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));  
复制代码

  1. Intent it=new Intent(Intent.ACTION_SEND);     
  2. String[] tos={"me@abc.com"};     
  3. String[] ccs={"you@abc.com"};     
  4. it.putExtra(Intent.EXTRA_EMAIL, tos);     
  5. it.putExtra(Intent.EXTRA_CC, ccs);     
  6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
  7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
  8. it.setType("message/rfc822");     
  9. startActivity(Intent.createChooser(it, "Choose Email Client"));   
复制代码
添加附件

  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
  3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
  4. sendIntent.setType("audio/mp3");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));
复制代码
播放多媒体
  1.   
  2. Intent it = new Intent(Intent.ACTION_VIEW);
  3. Uri uri = Uri.parse("file:///sdcard/song.mp3");
  4. it.setDataAndType(uri, "audio/mp3");
  5. startActivity(it);
复制代码

  1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
  3. startActivity(it);  
复制代码
Uninstall 程序
  1. Uri uri = Uri.fromParts("package", strPackageName, null);   
  2. Intent it = new Intent(Intent.ACTION_DELETE, uri);   
  3. startActivity(it);
复制代码

顯示地圖與導航intent用法

顯示地圖現成的Intent用法程式碼如下:
Uri uri = Uri.parse("geo:緯度,經度");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
顯示導航現成的Intent用法程式碼如下:
Uri uri = Uri.parse("
http://maps.google.com/maps?f=d&saddr=起點位置&daddr=終點位置&hl=tw");
路徑格式:
f=d&saddr=起點位置&daddr=終點位置
&hl=tw
起點位置格式
:Latitude,Longitude
終點位置格式
:Latitude,Longitude
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
備註:記得要加入internet的使用權限





String vDirectionUrl = "https://maps.google.com/maps?f=d"
                     + "&saddr=" + 來源Latitude + "," + 來源Longitude
                     + "&daddr=" + 目的Latitude + "," + 目的Longitude
                     + "&hl=tw";

// 在 Google 地圖 App 顯示導航
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse(vDirectionUrl) );
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);




Android

String strURL = "http://maps.google.com/maps?f=d&saddr=&daddr=&hl=;
/* 
   f=d:使用Google Map 導航功能
   saddr:傳入 開始位置(預設為目前位置)
   daddr:傳入 目的位置
   hl:指定語系 (Locale.getDefault().getCountry())
*/
Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW); 
intent.setData 
( 
    Uri.parse(strURL )
); 
startActivity(intent); 
iOS

    MKUserLocation *userLoc = self.myMapView.userLocation;
    NSString *destlatlong = [NSString stringWithFormat:@"%f,%f",
                             userLoc.location.coordinate.latitude,
                             userLoc.location.coordinate.longitude];
         
    NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?f=d&saddr=%@&daddr=%@&z=12",
                     destlatlong,[strLoc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

2014年6月2日 星期一

[PHP] 簡易的圖片相似度比較

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
 * 圖片相似度比較
 *
 * @version     $Id: ImageHash.php 4429 2012-04-17 13:20:31Z jax $
 * @author      jax.hu
 *
 * <code>
 *  //Sample_1
 *  $aHash = ImageHash::hashImageFile('wsz.11.jpg');
 *  $bHash = ImageHash::hashImageFile('wsz.12.jpg');
 *  var_dump(ImageHash::isHashSimilar($aHash, $bHash));
 *
 *  //Sample_2
 *  var_dump(ImageHash::isImageFileSimilar('wsz.11.jpg', 'wsz.12.jpg'));
 * </code>
 */
 
class ImageHash {
 
    /**取樣倍率 1~10
     * @access public
     * @staticvar int
     * */
    public static $rate = 2;
 
    /**相似度允許值 0~64
     * @access public
     * @staticvar int
     * */
    public static $similarity = 80;
 
    /**圖片類型對應的開啟函數
     * @access private
     * @staticvar string
     * */
    private static $_createFunc = array(
        IMAGETYPE_GIF   =>'imageCreateFromGIF',
        IMAGETYPE_JPEG  =>'imageCreateFromJPEG',
        IMAGETYPE_PNG   =>'imageCreateFromPNG',
        IMAGETYPE_BMP   =>'imageCreateFromBMP',
        IMAGETYPE_WBMP  =>'imageCreateFromWBMP',
        IMAGETYPE_XBM   =>'imageCreateFromXBM',
    );
 
 
    /**從檔案建立圖片
     * @param string $filePath 檔案位址路徑
     * @return resource 當成功開啟圖片則回傳圖片 resource ID,失敗則是 false
     * */
    public static function createImage($filePath){
        if(!file_exists($filePath)){ return false; }
 
        /*判斷檔案類型是否可以開啟*/
        $type = exif_imagetype($filePath);
        if(!array_key_exists($type,self::$_createFunc)){ return false; }
 
        $func = self::$_createFunc[$type];
        if(!function_exists($func)){ return false; }
 
        return $func($filePath);
    }
 
 
    /**hash 圖片
     * @param resource $src 圖片 resource ID
     * @return string 圖片 hash 值,失敗則是 false
     * */
    public static function hashImage($src){
        if(!$src){ return false; }
 
        /*缩小圖片尺寸*/
        $delta = 8 * self::$rate;
        $img = imageCreateTrueColor($delta,$delta);
        imageCopyResized($img,$src, 0,0,0,0, $delta,$delta,imagesX($src),imagesY($src));
 
        /*計算圖片灰階值*/
        $grayArray = array();
        for ($y=0; $y<$delta; $y++){
            for ($x=0; $x<$delta; $x++){
                $rgb = imagecolorat($img,$x,$y);
                $col = imagecolorsforindex($img, $rgb);
                $gray = intval(($col['red']+$col['green']+$col['blue'])/3)& 0xFF;
 
                $grayArray[] = $gray;
            }
        }
        imagedestroy($img);
 
        /*計算所有像素的灰階平均值*/
        $average = array_sum($grayArray)/count($grayArray);
 
        /*計算 hash 值*/
        $hashStr = '';
        foreach ($grayArray as $gray){
            $hashStr .= ($gray>=$average) ? '1' : '0';
        }
 
        return $hashStr;
    }
 
 
    /**hash 圖片檔案
     * @param string $filePath 檔案位址路徑
     * @return string 圖片 hash 值,失敗則是 false
     * */
    public static function hashImageFile($filePath){
        $src = self::createImage($filePath);
        $hashStr = self::hashImage($src);
        imagedestroy($src);
 
        return $hashStr;
    }
 
 
    /**比較兩個 hash 值,是不是相似
     * @param string $aHash A圖片的 hash 值
     * @param string $bHash B圖片的 hash 值
     * @return bool 當圖片相似則回傳 true,否則是 false
     * */
    public static function isHashSimilar($aHash, $bHash){
        $aL = strlen($aHash); $bL = strlen($bHash);
        if ($aL !== $bL){ return false; }
 
        /*計算容許落差的數量*/
        $allowGap = $aL*(100-self::$similarity)/100;
 
        /*計算兩個 hash 值的漢明距離*/
        $distance = 0;
        for($i=0; $i<$aL; $i++){
            if ($aHash{$i} !== $bHash{$i}){ $distance++; }
        }
 
        return ($distance<=$allowGap) ? true : false;
    }
 
 
    /**比較兩個圖片檔案,是不是相似
     * @param string $aHash A圖片的路徑
     * @param string $bHash B圖片的路徑
     * @return bool 當圖片相似則回傳 true,否則是 false
     * */
    public static function isImageFileSimilar($aPath, $bPath){
        $aHash = ImageHash::hashImageFile($aPath);
        $bHash = ImageHash::hashImageFile($bPath);
        return ImageHash::isHashSimilar($aHash, $bHash);
    }
 
}