2012年1月9日星期一

Android Back Key

大家好,我是奶綠茶
在使用 Android 時,有時會不小心按到 Back 鍵而離開應用程式
理想的操作,應該要有個提示讓使用者知道是否要離開。
只要 Override Activity 的 onKeyDown 事件,
加上提示的程式即可
常用的方法有二個,
1. 使用 Toast 提示,在一定的時間內按二次 Back 鍵
private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
 if(keyCode == KeyEvent.KEYCODE_BACK
   && event.getAction() == KeyEvent.ACTION_DOWN){
     if((System.currentTimeMillis()-exitTime) > 2000){
         Toast.makeText(getContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
         exitTime = System.currentTimeMillis();
  } else {
   finish();
   System.exit(0);
  }
     return true;
    }
 return super.onKeyDown(keyCode, event);
}



2. 使用 AlertDialog ,讓使用者自行決定。
new AlertDialog.Builder( getContext)
.setIcon(R.drawable.icon_info).setTitle("  ")
.setMessage("確定要離開應用程式嗎?")
.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).setNeutralButton("確定", new OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
  finish();
   System.exit(0);
 }
}).create().show();
轉載請註明出處

2012年1月8日星期日

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

大家好,我是奶綠茶
最近在寫 Android 程式時,
一直發生
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
這個問題
後來查了一下,才知道是圖檔太大,超過能使用的記憶體
但透過
BitmapFactory.Options 這個類別就可以決解。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

其中 inSampleSize 的數值,指的是將圖片大小除以 n
重點就要是如何算出正確的數值。
官方文件有提到,該值以 2 的平方為最佳
先準備一張 3000x2000 的圖片,放到 raw 資料夾裡
再沒有使用 inSampleSize 時
直接 decode, 就會發生 OutOfMemory

要算出最佳值,要對圖片 decode 二次
第一次先得到圖片的長、寬,然後再算出 sampleSize ,
第二次再 deocde 得到所需要的圖片
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true; //使用這個屬性, 就只會計算,但不會分配記憶體

Bitmap bitmap = BitmapFactory.decodeResource(res, R.raw.i3000x2000,opt);
trace(opt.outWidth,opt.outHeight);//得到原始圖片長、寬

最佳 sampleSize 算法

轉貼至:http://www.maxhis.info/androiding/bitmap-size-exceed/

 public static int computeSampleSize(BitmapFactory.Options options,int maxNumOfPixels) {
  return computeSampleSize(options,-1 , maxNumOfPixels);
 }
 public static int computeSampleSize(BitmapFactory.Options options,
   int minSideLength, int maxNumOfPixels) {
  int initialSize = computeInitialSampleSize(options, minSideLength,
    maxNumOfPixels);

  int roundedSize;
  if (initialSize <= 8) {
   roundedSize = 1;
   while (roundedSize < initialSize) {
    roundedSize <<= 1;
   }
  } else {
   roundedSize = (initialSize + 7) / 8 * 8;
  }

  return roundedSize;
 }

 private static int computeInitialSampleSize(BitmapFactory.Options options,
   int minSideLength, int maxNumOfPixels) {
  double w = options.outWidth;
  double h = options.outHeight;

  int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
    .sqrt(w * h / maxNumOfPixels));
  int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
    Math.floor(w / minSideLength), Math.floor(h / minSideLength));

  if (upperBound < lowerBound) {
   // return the larger one when there is no overlapping zone.
   return lowerBound;
  }

  if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
   return 1;
  } else if (minSideLength == -1) {
   return lowerBound;
  } else {
   return upperBound;
  }
 }


opt.inSampleSize = computeSampleSize(opt,480*800);
opt.inJustDecodeBounds=false;
trace(opt.inSampleSize);
bitmap = BitmapFactory.decodeResource(res, R.raw.i3000x2000,opt);//第二次 decode
完美。



參考文章:
http://www.maxhis.info/androiding/bitmap-size-exceed/
http://julianshen.blogspot.com/2010/08/android-bitmapoutofmemoryerror.html
http://blog.xuite.net/ffc99a3b/ooxx/44391740
http://bluegray-javalearning.blogspot.com/2011/07/android-out-of-memoryoom.html
http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue

轉載請註明出處

2011年11月30日星期三

批次快速更改副檔名

大家好,我是奶綠茶
想要快速的批次更改副檔名,又不想開其他軟體時
可以使用 Windows 的 批次檔
開啟 notepad , 輸入: ren *.jad *.java
另存成 xxx.bat, 並 copy 到你想更改的資料夾裡
點擊二下啟動即可。
這句的意思就是把該 .bat 所在的資料夾下,
副檔名為.jad 改成.java
完成

2011年11月24日星期四

Flash, Android 呼叫 ASP.NET WebService

大家好,我是奶綠茶
今天來分享如何使用 Flash 及 Android 呼叫 ASP.NET 的 WebService
WebService 的好處, 就是可以把專案所有要用到的方法, 寫在一隻程式裡, 方便日後管理。
傳接的格式, 都使用 JSON
.net 傳 WebService程式
[WebService(Namespace = "http://milkmidi.com/")] //注意這個 Namespace
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
    private static JavaScriptSerializer serializer = new JavaScriptSerializer();
    public WebService () {
    }
   

    [WebMethod]
    public string getArray() {
        return serializer.Serialize(new string[] { "A", "B", "c" });
    }

    [WebMethod]
    public string getObject() {
        var json = new
        {
            RS = "OK"
        };
        return serializer.Serialize(json);
    }

    /// 
    /// Client 端傳 JSON 格式, Server 端解
    ///     
    [WebMethod]
    public string setObject(string json) {
        JsonObject obj = JsonConvert.Import(typeof(JsonObject), json) as JsonObject;
        string name;
        string age;
        try {
            name = obj["name"].ToString();
            age = obj["age"].ToString();
        }
        catch (Exception) {            
            throw;
        }       
        var rs = new
        {
            RS = "OK" ,
            name = name,
            age = age
        };
        return serializer.Serialize(rs);
    }
}
flash端, 使用 FlexSDK 裡的 Webservice 類別
使用方法如下:
 service.要呼叫的方法.send( 參數1, 參數2 );
/**
 * @author milkmidi
 */
package  {  
 import com.adobe.serialization.json.JSON;
 import flash.display.Sprite;
 import mx.rpc.events.FaultEvent;
 import mx.rpc.events.ResultEvent;
 import mx.rpc.soap.LoadEvent;
 import mx.rpc.soap.WebService;
 import mx.utils.ObjectUtil;
 public class MXWebService extends Sprite {  
  public var service:WebService = new WebService();
  public function MXWebService()  {   
   service.wsdl = "http://localhost:61490/webService/WebService.asmx?wsdl";
   service.addEventListener(LoadEvent.LOAD, serviceLoadHandler);
   service.loadWSDL();
   
   //getArray();
   //getObject();   
   setObject(  JSON.encode( { name:"milkmidi", age:"30" } ) );
   
   // 使用 
   // service.要呼叫的方法.send( 參數1, 參數2 );
  }  
  
  private function getArray():void {
   service.getArray.addEventListener(ResultEvent.RESULT, resultHandler);
   service.getArray.addEventListener(FaultEvent.FAULT, faultHandler);
   service.getArray.send();
  }
  private function getObject():void {
   service.getObject.addEventListener(ResultEvent.RESULT, resultHandler);
   service.getObject.addEventListener(FaultEvent.FAULT, faultHandler);
   service.getObject.send();
  }
  private function setObject(json:String):void {
   service.setObject.addEventListener(ResultEvent.RESULT, resultHandler);
   service.setObject.addEventListener(FaultEvent.FAULT, faultHandler);
   service.setObject.send(json);
  }
  
  private function faultHandler(e:FaultEvent):void {
   trace(e);
  }
  
  private function resultHandler(e:ResultEvent):void {
   trace(e);
   trace(ObjectUtil.toString(e.result));
  }
  
  private function serviceLoadHandler(e:LoadEvent):void {
   trace(e);
  }  
  
 }
}
Android 端
參考了這二篇的教學
http://composedcrap.blogspot.com/2009/08/connecting-to-net-web-service-from.html
http://blog.yam.com/wewa85/article/32877801
public class SampleCallWebService extends Activity {
 
 // ASP.NET 端, 宣告的 Namespace
 private static final String NAMESPACE = "http://milkmidi.com/";
 
 // asmx 路徑
 private static final String URL = "http://localhost:61490/webService/WebService.asmx";

 //要呼叫的方法
 private static final String MemberLogin_SOAP_ACTION = NAMESPACE+"setObject";
 private static final String METHOD_NAME2 = "setObject";

 private Button mBtn;
 private TextView mMsgTxt;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mBtn = (Button) findViewById(R.id.btn);  
  mMsgTxt = (TextView) findViewById(R.id.btn);
 
  mBtn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    try {
     mMsgTxt.setText( doSetObject() );
    } catch (JSONException e) {
     e.printStackTrace();
    }
   }

  });
 }
 public String doSetObject() throws JSONException {
  Log.i("[WebService]", "doSetObject");
  String rs = "";
  
  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
  JSONObject json = new JSONObject();
  json.put("name", "milkmidi");
  json.put("age", "30");
  request.addProperty("json", json.toString());  

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);
  envelope.dotNet = true;
  envelope.setOutputSoapObject(request);
  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);  
  try {
   androidHttpTransport.call(MemberLogin_SOAP_ACTION, envelope);  
   SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
   rs = result.toString();
  } catch (Exception e) {
   mMsgTxt.setText(e.toString());
  }
  
  JSONObject rsJson = new JSONObject(rs);  
  return rsJson.getString("name")+","+rsJson.getString("age");

 }
}

轉載請註明出處

2011年11月23日星期三

html5 easel 3D Ball

大家好,我是奶綠茶
今天來分享關於 html5 的技術
試寫一個簡單的 3D Ball 效果
使用的 html5 Framework, 是 gskinner 大神所開發的 easeljs
為什麼要選這套呢,因為是 gskinner 開發, 就一定是好東西
還有一點是他長的很像 AS3 的寫法, 真的超像的
對 Flash Developer 來說,真的不難,

轉載請註明出處

2011年11月22日星期二

Java String split

大家好,我是奶綠茶
在開發程式時,經常會需要用到把一整個字串依某字位拆成一個 Array
在 Java  裡 可以使用 String 裡的 split 方法
和 FlashActionScript 用法是一樣的
但有一點點不同之處,
如果你想要 split 的字元是 "." ,就要寫成 "\\."
不然永遠得到的 Array 長度都是 0

public class StringSplit {
  public static void main(String args[]) throws Exception{
    String testString = "Real.How.To";
    // bad
    System.out.println(java.util.Arrays.toString(
        testString.split(".")
    ));
    // output : []

    // good
    System.out.println(java.util.Arrays.toString(
      testString.split("\\.")
    ));
    // output : [Real, How, To]
  }
}
參考文章:http://www.rgagnon.com/javadetails/java-0438.html