2013年1月1日

[Android]製作縮圖

問題:
在使用Android的ImageView元件放入圖檔時,發現圖檔尺寸比ImageView元件的尺寸來的大很多。

解決:
使用Android製作縮圖,



/**
 原始圖檔 bitmap
*/

//取得圖檔寬度
int bmpWidth  = bitmap.getWidth(); 

//取得圖檔高度
int bmpHeight  = bitmap.getHeight(); 

//設定縮圖寬度
float scaleWidth  = (float) sWidth / bmpWidth;     
//按固定大小缩放sWidth,要多大有多大

//設定縮圖高度
float scaleHeight = (float) sHeight / bmpHeight;  

//轉換矩陣
Matrix matrix = new Matrix(); 
matrix.postScale(scaleWidth, scaleHeight); 

//產生縮圖
Bitmap resizeBitmap = 
Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight,matrix, false); 

//記得做資源回收,不然會發生溢位
bitmap.recycle(); 


參考網站:Android中图片缩放方法

2012年9月30日

[CSS]讓版面聽話-Reset.css

問題:在開發Web時,常遇到各家瀏覽器不同的預設,導致設計師日以繼夜的爆肝。

最佳的方法「Reset CSS」

世界知名的 CSS 大師「Eric A. Meyer」整理出一個很棒的解決方法「Reset CSS」,針對 CSS 語法最容易出問題的部份~例如 margin 外間距,各大瀏覽器最常發生不一致的狀況,現在將 margin 全部統一歸 0 ,其他部份,文字大小和行高也全部統成一樣的大小 …. 等,只要掛上這一段「Reset CSS」語法,就可以讓所有的各大瀏覽器乖乖聽話,呈現一樣的結果,CSS 的大同世界就在這裡啊!

/* v1.0 | 20080212 */

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-size: 100%;
    vertical-align: baseline;
    background: transparent;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}

/* remember to define focus styles! */
:focus {
    outline: 0;
}

/* remember to highlight inserts somehow! */
ins {
    text-decoration: none;
}
del {
    text-decoration: line-through;
}

/* tables still need 'cellspacing="0"' in the markup */
table {
    border-collapse: collapse;
    border-spacing: 0;
}

下載網站:http://meyerweb.com/eric/tools/css/reset/reset.css

參考來源:flycan


2012年9月20日

[Android]Android連不到WebAPI

問題:在開發Android時,連不上Web的API,該怎麼辦?

/*
step 1:先檢查是否有加入權限
*/
//AndroidManifest.xml


//*.java
@Override
    public void onCreate(Bundle savedInstanceState) {
  //加入以下兩行
     StrictMode
.setVmPolicy(new StrictMode
.VmPolicy
.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build());
     StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites()
.detectNetwork().penaltyLog().build());
  
  /*
  StrictMode
  This class is not available with API level 8.
  所以要用在2.3以上喔
  */
     
  super.onCreate(savedInstanceState);


參考網站:Android™ 2.1 android.R.drawable Icon Resources

[Android]Android內建的圖示

問題:在開發Android時,沒有美工的頭腦時,網路上下載圖檔又擔心版權問題,所以直接使用Android 內建的圖示

//*.java
ImageView btnImg_Help = (ImageView)findViewById(R.id.Img_Help); 
btnImg_Help.setImageResource(android.R.drawable.ic_menu_help);

//*.xml
android:icon="@android:drawable/ic_menu_save"

參考網站:Android™ 2.1 android.R.drawable Icon Resources

2012年8月16日

[PHP]集合運算


交集: array_intersection()
差集: array_diff()
聯集: 沒有內建,不過可先透過 array_merge 將兩個陣列集合在一起,再利用 array_unique 去除重複的元素
function array_union($a, $b) {
$union = array_merge($a, $b); // duplicates may still exist
$union = array_unique($union);
return $union;
}

2011年12月14日

[PHP]JSON的使用

1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);  
echo $arr2=json_encode($arr); 
$stdin=get_object_vars(json_decode($arr2));
foreach ($stdin as $k => $v) {
echo "$k = \"$v\"\n"; // print json_decode key => value.
}
?>

2011年12月9日

[Android] 開發小問題

1. indexOf判斷中文字串
//indexOf的句子,不知道為什麼第一個字不會去找,怪災。
replaced=" "+result.get(i);

//年
if(replaced.indexOf("年")>0)
{
score[i]=score[i]+1;
}

2.使用正規表示法判斷數字
//判斷數字
     Pattern p=Pattern.compile("[0-9]{1,2}");
     Matcher m=p.matcher(replaced);
     if(m.find()){
      //正確
     }

//{n,m} 表示前一個字元或者前一個RE出現n到m次
//[0-9] 0-9的集合