咖啡伴侶

          呆在上海
          posts - 163, comments - 156, trackbacks - 0, articles - 2

          屏幕大小
          1、不同的layout

                Android手機屏幕大小不一,有480x320,640x360,800x480.怎樣才能讓App自動適應不同的屏幕呢? 其實很簡單,只需要在res目錄下創建不同的layout文件夾,比如:layout-640x360,layout-800x480,所有的 layout文件在編譯之后都會寫入R.java里,而系統會根據屏幕的大小自己選擇合適的layout進行使用。

          2、hdpi、mdpi、ldpi

                前的版本中,只有一個drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三個,這三個主要是為了支持多分辨率。

          drawable- hdpi、drawable- mdpi、drawable-ldpi的區別:

          1.drawable-hdpi里面存放高分辨率的圖片,如WVGA (480x800),FWVGA (480x854)
          2.drawable-mdpi里面存放中等分辨率的圖片,如HVGA (320x480)
          3.drawable-ldpi里面存放低分辨率的圖片,如QVGA (240x320)
          系統會根據機器的分辨率來分別到這幾個文件夾里面去找對應的圖片。在開發程序時為了兼容不同平臺不同屏幕,建議各自文件夾根據需求均存放不同版本圖片。

          屏幕方向
          1、橫屏豎屏自動切換

                可以在res目錄下建立layout-port和layout-land兩個目錄,里面分別放置豎屏和橫屏兩種布局文件,這樣在手機屏幕方向變化的時候系統會自動調用相應的布局文件,避免一種布局文件無法滿足兩種屏幕顯示的問題。

          2、禁用自動切換

          只需要在AndroidManifest.xml文件中加入android:screenOrientation屬性限制。

          •Android:screenOrientation="landscape"  //是限制此頁面橫屏顯示
          •Android:screenOrientation="portrait"      //是限制此頁面數豎屏顯示
          字體自適應大小
          方法1:

          首先根據不同分辨率獲取不同字體大小。
          在RES里創建
          values-480x320/strings.xml 里面設置<dimen name="Text_size">30px</dimen>

          values-800x400/strings.xml 里面設置<dimen name="Text_size">30px</dimen>

          分別代表480X320 和 800X400分辨率情況下 字號為30px和40px;

          在java文件中這樣調用

          int sizeOfText = (int) this.getResources().getDimension(R.dimen.Text_size);

          方法2:

          在視圖的 onsizechanged里獲取視圖寬度,一般情況下默認寬度是320,所以計算一個縮放比率rate = (float) w/320   w是實際寬度
          然后在設置字體尺寸時 paint.setTextSize((int)(8*rate));   8是在分辨率寬為320 下需要設置的字體大小實際字體大小 = 默認字體大小 x  rate

          posted @ 2011-09-02 09:28 oathleo 閱讀(1254) | 評論 (0)編輯 收藏

          首先內部存儲路徑為/data/data/youPackageName/,下面講解的各路徑都是基于你自己的應用的內部存儲路徑下。所有內部存儲中保存的文件在用戶卸載應用的時候會被刪除。

          一、 files
          1. Context.getFilesDir(),該方法返回/data/data/youPackageName/files的File對象。
          2. Context.openFileInput()與Context.openFileOutput(),只能讀取和寫入files下的文件,返回的是FileInputStream和FileOutputStream對象。
          3. Context.fileList(),返回files下所有的文件名,返回的是String[]對象。
          4. Context.deleteFile(String),刪除files下指定名稱的文件。

          二、cache
          1. Context.getCacheDir(),該方法返回/data/data/youPackageName/cache的File對象。

          三、custom dir

          getDir(String name, int mode),返回/data/data/youPackageName/下的指定名稱的文件夾File對象,如果該文件夾不存在則用指定名稱創建一個新的文件夾。



          有了數據存儲 API,您可以使用內部存儲器存儲數據。信息可以是私有的,您可以有選擇地讓其他應用程序對之具有讀或寫的訪問權限。本節介紹這個存儲私有數據的 API,它使用 android.content.Context.openFileInput、openFileOutput 和 getCacheDir() 來高速緩存數據,而不是永久地存儲。

          清單 20 中的代碼片段展示了如何從內部私有存儲器讀取數據。使得存儲器為私有的方法是對 openFileOutput() 使用MODE_PRIVATE。


          清單 20. 從本地私有存儲器讀取數據

          /**
           * Writes content to internal storage making the content private to 
           * the application. The method can be easily changed to take the MODE 
           * as argument and let the caller dictate the visibility: 
           * MODE_PRIVATE, MODE_WORLD_WRITEABLE, MODE_WORLD_READABLE, etc.
           * 
           * 
          @param filename - the name of the file to create
           * 
          @param content - the content to write
           
          */
          public void writeInternalStoragePrivate(
                  String filename, 
          byte[] content) {
              
          try {
                  
          //MODE_PRIVATE creates/replaces a file and makes 
                  
          //  it private to your application. Other modes:
                  
          //    MODE_WORLD_WRITEABLE
                  
          //    MODE_WORLD_READABLE
                  
          //    MODE_APPEND
                  FileOutputStream fos = 
                     openFileOutput(filename, Context.MODE_PRIVATE);
                  fos.write(content);
                  fos.close();
              } 
          catch (FileNotFoundException e) {
                  e.printStackTrace();
              } 
          catch (IOException e) {
                  e.printStackTrace();
              }
          }


          清單 21 中的代碼片段展示了如何從內部私有存儲器讀取數據;注意 openFileInput() 的使用。


          清單 21. 從內部私有存儲器讀取數據

          /**
           * Reads a file from internal storage
           * 
          @param filename the file to read from
           * 
          @return the file content
           
          */
          public byte[] readInternalStoragePrivate(String filename) {
              
          int len = 1024;
              
          byte[] buffer = new byte[len];
              
          try {
                  FileInputStream fis 
          = openFileInput(filename);
                  ByteArrayOutputStream baos 
          = new ByteArrayOutputStream();
                  
          int nrb = fis.read(buffer, 0, len); // read up to len bytes
                  while (nrb != -1) {
                      baos.write(buffer, 
          0, nrb);
                      nrb 
          = fis.read(buffer, 0, len);
                  }
                  buffer 
          = baos.toByteArray();
                  fis.close();
              } 
          catch (FileNotFoundException e) {
                  e.printStackTrace();
              } 
          catch (IOException e) {
                  e.printStackTrace();
              }
              
          return buffer;
          }


          清單 22 展示了如何從內部私有存儲器刪除數據。


          清單 22. 從本地私有存儲器刪除數據

              
          /**
           * Delete internal private file
           * @param filename - the filename to delete
           */
          public void deleteInternalStoragePrivate(String filename) {
              File file = getFileStreamPath(filename);
              if (file != null) {
                  file.delete();
              }
          }


          現在可以來看為公共數據使用外部存儲器了。

          回頁首

          為公共數據使用設備的外部存儲器

          有了數據存儲 API,您可以使用外部存儲器存儲數據。信息可以是私有的,您可以有選擇地讓其他應用程序對之具有讀或寫的訪問權限。本節您將對此 API 進行編程,以便使用包括getExternalStorageState()、getExternalFilesDir()、getExternalStorageDirectory() 和getExternalStoragePublicDirectory() 在內的很多 API 來存儲公共數據。您為公共數據使用下面的路徑:/Android/data/<package_name>/files/。

          在使用外部存儲器之前,必須看看它是否可用,是否可寫。下面兩個代碼片段展示了測試這些條件的幫助器方法。清單 23 測試外部存儲器是否可用。


          清單 23. 測試外部存儲器是否可用

              
          /**
           * Helper Method to Test if external Storage is Available
           */
          public boolean isExternalStorageAvailable() {
              boolean state = false;
              String extStorageState = Environment.getExternalStorageState();
              if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
                  state = true;
              }
              return state;
          }


          清單 24 測試外部存儲器是否只可讀。


          清單 24. 測試外部存儲器是否只可讀

              
          /**
           * Helper Method to Test if external Storage is read only
           */
          public boolean isExternalStorageReadOnly() {
              boolean state = false;
              String extStorageState = Environment.getExternalStorageState();
              if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
                  state = true;
              }
              return state;
          }


          清單 25 展示了如何寫到外部存儲器,以存儲公共數據。


          清單 25. 寫到外部內存

              
          /**
           * Write to external public directory
           * @param filename - the filename to write to
           * @param content - the content to write
           */
          public void writeToExternalStoragePublic(String filename, byte[] content) {

              // API Level 7 or lower, use getExternalStorageDirectory()
              //  to open a File that represents the root of the external
              // storage, but writing to root is not recommended, and instead
              // application should write to application-specific directory, as shown below.

              String packageName = this.getPackageName();
              String path = "/Android/data/" + packageName + "/files/";

              if (isExternalStorageAvailable() &&
                 !isExternalStorageReadOnly()) {
                  try {
                      File file = new File(path, filename);
                      file.mkdirs();
                      FileOutputStream fos = new FileOutputStream(file);
                      fos.write(content);
                      fos.close();
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }


          清單 26 展示了如何從外部存儲器讀取數據。


          清單 26. 從外部內存讀取數據

              
          /**
           * Reads a file from internal storage
           * @param filename - the filename to read from
           * @return the file contents
           */
          public byte[] readExternallStoragePublic(String filename) {
              int len = 1024;
              byte[] buffer = new byte[len];
              String packageName = this.getPackageName();
              String path = "/Android/data/" + packageName + "/files/";

              if (!isExternalStorageReadOnly()) {     
                  try {
                      File file = new File(path, filename);            
                      FileInputStream fis = new FileInputStream(file);
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                      int nrb = fis.read(buffer, 0, len); //read up to len bytes
                      while (nrb != -1) {
                          baos.write(buffer, 0, nrb);
                          nrb = fis.read(buffer, 0, len);
                      }
                      buffer = baos.toByteArray();
                      fis.close();
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return buffer;
          }


          清單 27 中的代碼片段展示了如何從外部內存刪除文件。


          清單 27. 從外部內存刪除文件

              
          /**
           * Delete external public file
           * @param filename - the filename to write to
           */
          void deleteExternalStoragePublicFile(String filename) {
              String packageName = this.getPackageName();
              String path = "/Android/data/" + packageName + "/files/"+filename;
              File file = new File(path, filename);
              if (file != null) {
                  file.delete();
              }
          }


          處理外部存儲器需要特殊的權限 WRITE_EXTERNAL_STORAGE,它通過 AndroidManifest.xml 請求得到(參見 清單 28)。


          清單 28. WRITE_EXTERNAL_STORAGE

              
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


          外部存儲 API 通過根據文件類型(比如 Pictures、Ringtones)將文件存儲在預先確定的目錄中,允許您公共地存儲文件。本文沒有介紹這種方法,但是您應該熟悉它。此外,記住外部存儲器中的文件任何時候都可能消失。

          回頁首

          相關的方法

          如果您具有不需要長期永久保存的臨時文件,那么可以將這些文件存儲在高速緩存中。高速緩存是一種特殊的內存,可以用于存儲中小型數據(少于兆字節),但是您一定要知道,取決于有多少內存可用,高速緩存的內容任何時候都可能被清除。

          清單 29 展示了一個幫助器方法,它返回到內部內存中高速緩存的路徑。


          清單 29. 檢索到內部內存高速緩存的路徑

              
          /**
           * Helper method to retrieve the absolute path to the application
           * specific internal cache directory on the file system. These files
           * will be ones that get deleted when the application is uninstalled or when
           * the device runs low on storage. There is no guarantee when these
           * files will be deleted.
           *
           * Note: This uses a Level 8+ API.
           *
           * @return the absolute path to the application specific cache
           * directory
           */
          public String getInternalCacheDirectory() {
              String cacheDirPath = null;
              File cacheDir = getCacheDir();
              if (cacheDir != null) {
                  cacheDirPath = cacheDir.getPath();
              }
              return cacheDirPath;        
          }


          清單 30 展示了一個幫助器方法,它返回到外部內存中高速緩存的路徑。


          清單 30. 檢索到外部內存高速緩存的路徑

              
          /**
           * Helper method to retrieve the absolute path to the application
           * specific external cache directory on the file system. These files
           * will be ones that get deleted when the application is uninstalled or when
           * the device runs low on storage. There is no guarantee when these
           * files will be deleted.
           *
           * Note: This uses a Level 8+ API.
           *
           * @return the absolute path to the application specific cache
           * directory
           */
          public String getExternalCacheDirectory() {
              String extCacheDirPath = null;
              File cacheDir = getExternalCacheDir();
              if (cacheDir != null) {
                  extCacheDirPath = cacheDir.getPath();
              }
              return extCacheDirPath;     
          }

           


          通過使用示例應用程序,您現在應該很好地理解了如何為公共數據使用設備的外部存儲器。

          posted @ 2011-08-31 13:17 oathleo 閱讀(2924) | 評論 (0)編輯 收藏

          WebView對Javascript的支持也很強,google一個Java和Javascript互調的例子  
          整個Eclipse ADT工程例子中都有,這里重點分析一下代碼:
          Java代碼  收藏代碼
          1. public class WebViewDemo extends Activity {  
          2.     private WebView mWebView;  
          3.     private Handler mHandler = new Handler();  
          4.   
          5.     public void onCreate(Bundle icicle) {  
          6.         super.onCreate(icicle);  
          7.         setContentView(R.layout.webviewdemo);  
          8.         mWebView = (WebView) findViewById(R.id.webview);  
          9.         WebSettings webSettings = mWebView.getSettings();  
          10.         webSettings.setJavaScriptEnabled(true);  
          11.         mWebView.addJavascriptInterface(new Object() {  
          12.             public void clickOnAndroid() {  
          13.                 mHandler.post(new Runnable() {  
          14.                     public void run() {  
          15.                         mWebView.loadUrl("javascript:wave()");  
          16.                     }  
          17.                 });  
          18.             }  
          19.         }, "demo");  
          20.         mWebView.loadUrl("file:///android_asset/demo.html");  
          21.     }  
          22. }  

          這 里的重點是addJavascriptInterface(Object obj,String interfaceName)方法,該方法將一個java對象綁定到一個javascript對象中,javascript對象名就是 interfaceName,作用域是Global。這樣初始化webview后,在webview加載的頁面中就可以直接通過 javascript:window.demo訪問到綁定的java對象了。來看看在html中是怎樣調用的:

          Html代碼  收藏代碼
          1. <html>  
          2.         <script language="javascript">  
          3.                 function wave() {  
          4.                     document.getElementById("droid").src="android_waving.png";  
          5.                 }  
          6.         </script>  
          7.         <body>  
          8.             <a onClick="window.demo.clickOnAndroid()">  
          9.                                 <img id="droid" src="android_normal.png"/><br>  
          10.                                 Click me!  
          11.             </a>  
          12.         </body>  
          13. </html>  

           這樣在javascript中就可以調用java對象的clickOnAndroid()方法了,wave()方法是java中調用javascript的例子。

          這里還有幾個知識點:

          1) 為了讓WebView從apk文件中加載assets,Android SDK提供了一個schema,前綴為"file:///android_asset/"。WebView遇到這樣的schema,就去當前包中的 assets目錄中找內容。如上面的"file:///android_asset/demo.html"

          2)addJavascriptInterface方法中要綁定的Java對象及方法要運行另外的線程中,不能運行在構造他的線程中,這也是使用Handler的目的。

          posted @ 2011-08-25 15:36 oathleo 閱讀(1002) | 評論 (0)編輯 收藏

              <property name="proguard-home" value="D:/proguard4.4/lib"/>
              
          <property name="android-jar" value="D:\android3.0\android-sdk_r12-windows\android-sdk-windows\platforms\android-8\android.jar"/>
          //指定 proguard 地址和 android地址


          <java jar="${proguard-home}/proguard.jar" fork="true" failonerror="true">
                      
          <jvmarg value="-Dmaximum.inlined.code.length=32" />
                      
          <arg value="-injars ${target_temp_jar}" />
                      
          <arg value="-outjars ${target_jar}" />
                      
          //第三方包
                      <arg value="-libraryjars ${android-jar}" />
                      
          <arg value="-libraryjars lib/android_script.jar" />

                      
          <!--  <arg value="-libraryjars ${external-libs}/*.jar"/>-->
                      
          <arg value="-dontpreverify" />
                      
          <arg value="-dontoptimize" />
                      
          <arg value="-dontusemixedcaseclassnames" />
                      
          <arg value="-repackageclasses ''" />
                      
          <arg value="-allowaccessmodification" />
                      
                      
          <!--保護public字段-->
                      
          <arg value="-keepclassmembers public class * { public protected *; }" />
                      
          <!--保護泛型-->
                      
          <arg value="-keepattributes Signature" />

                      
          <arg value="-keep public class com.xxxcore.*" />
                      
          <arg value="-keep public class com.xxxviewer.**" />
           
                      
          <arg value="-optimizationpasses 7" />
                      
          <arg value="-verbose" />
                      
          <arg value="-dontskipnonpubliclibraryclasses" />
                      
          <arg value="-dontskipnonpubliclibraryclassmembers" />
                  
          </java>

                  
          <signjar jar="${target_jar}" keystore="${sign.keystore}" alias="${sign.alias}" keypass="${sign.keypass}" storepass="${sign.storepass}"/>
                  
          <delete file="${target_temp_jar}" />
              
          </target>




          參數:

          -include {filename}    從給定的文件中讀取配置參數

          -basedirectory {directoryname}    指定基礎目錄為以后相對的檔案名稱

          -injars {class_path}    指定要處理的應用程序jar,war,ear和目錄

          -outjars {class_path}    指定處理完后要輸出的jar,war,ear和目錄的名稱

          -libraryjars {classpath}    指定要處理的應用程序jar,war,ear和目錄所需要的程序庫文件

          -dontskipnonpubliclibraryclasses    指定不去忽略非公共的庫類。

          -dontskipnonpubliclibraryclassmembers    指定不去忽略包可見的庫類的成員。


          保留選項
          -keep {Modifier} {class_specification}    保護指定的類文件和類的成員

          -keepclassmembers {modifier} {class_specification}    保護指定類的成員,如果此類受到保護他們會保護的更好

          -keepclasseswithmembers {class_specification}    保護指定的類和類的成員,但條件是所有指定的類和類成員是要存在。

          -keepnames {class_specification}    保護指定的類和類的成員的名稱(如果他們不會壓縮步驟中刪除)

          -keepclassmembernames {class_specification}    保護指定的類的成員的名稱(如果他們不會壓縮步驟中刪除)

          -keepclasseswithmembernames {class_specification}    保護指定的類和類的成員的名稱,如果所有指定的類成員出席(在壓縮步驟之后)

          -printseeds {filename}    列出類和類的成員-keep選項的清單,標準輸出到給定的文件

          壓縮
          -dontshrink    不壓縮輸入的類文件

          -printusage {filename}

          -whyareyoukeeping {class_specification}    

          優化
          -dontoptimize    不優化輸入的類文件

          -assumenosideeffects {class_specification}    優化時假設指定的方法,沒有任何副作用

          -allowaccessmodification    優化時允許訪問并修改有修飾符的類和類的成員

          混淆
          -dontobfuscate    不混淆輸入的類文件

          -printmapping {filename}

          -applymapping {filename}    重用映射增加混淆

          -obfuscationdictionary {filename}    使用給定文件中的關鍵字作為要混淆方法的名稱

          -overloadaggressively    混淆時應用侵入式重載

          -useuniqueclassmembernames    確定統一的混淆類的成員名稱來增加混淆

          -flattenpackagehierarchy {package_name}    重新包裝所有重命名的包并放在給定的單一包中

          -repackageclass {package_name}    重新包裝所有重命名的類文件中放在給定的單一包中

          -dontusemixedcaseclassnames    混淆時不會產生形形色色的類名

          -keepattributes {attribute_name,}    保護給定的可選屬性,例如LineNumberTable, LocalVariableTable, SourceFile, Deprecated, Synthetic, Signature, and InnerClasses.

          -renamesourcefileattribute {string}    設置源文件中給定的字符串常量

          posted @ 2011-08-24 14:39 oathleo 閱讀(2501) | 評論 (0)編輯 收藏

          注意
          1.onDown 多指事件仍然響應成單指
          2.onScroll滑動時觸發,e1只能獲得一個點 ,而e2卻能獲得多點。(搞不清楚為什么怎么設計)
          3.想在view上加 GestureListener
          可以使用下面的代碼:
          public class ViewerInnerTouchListener implements OnTouchListener {

              
          private GestureDetector gestureDetector;

              
          public ViewerInnerTouchListener(GAViewer viewer) {
                  gestureDetector 
          = new GestureDetector(new GestureListener(viewer));
              }

              
          public boolean onTouch(View v, MotionEvent event) {
                  gestureDetector.onTouchEvent(event);
                  
          return true;
              }

          }

              setOnTouchListener(
          new ViewerInnerTouchListener(this));

          判斷手勢:
              public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                  
          if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                      Log.i(
          "onFling""onFling");
                      
          return true// Right to left
                  } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                      
          return false// Left to right
                  }
                  
          if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                      
          return false// Bottom to top
                  } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                      
          return false// Top to bottom
                  }
                  
          return false;
              }


          GestureListener 的幾個方法要理解
               //Touch down時觸發,不論是touch (包括long) ,scroll
               public boolean onDown(MotionEvent e) {
               return false;
               }
              
               //Touch了還沒有滑動時觸發
               //(與onDown,onLongPress比較
               //onDown只要Touch down一定立刻觸發。
               public void onShowPress(MotionEvent e) {
               }
              
               //Touch了不移動一直Touch down時觸發
               //Touchdown后過一會沒有滑動先觸發onShowPress再是onLongPress。
               public void onLongPress(MotionEvent e) {
               }
              
               //上面這兩個函數都是在touch down后又沒有滑動(onScroll),又沒有長按(onLongPress),然后Touchup時觸發。
               /**
               * 點擊一下非常快的(不滑動)Touchup: onDown->onSingleTapUp->onSingleTapConfirmed
               *
               點擊一下稍微慢點的(不滑動)Touchup:onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
               * 點擊longpress ,onScroll 時 不觸發 onSingleTapUp
               */
               public boolean onSingleTapUp(MotionEvent e) {
               Log.i("onSingleTapUp", "onSingleTapUp");
               return false;
               }
              
               //Touch了滑動時觸發
               public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
               float distanceY) {
               return true;
               }
              
               //Touch了滑動一點距離后,up時觸發
               public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
               float velocityY) {
               return true;
               }

          posted @ 2011-08-21 11:59 oathleo 閱讀(3244) | 評論 (0)編輯 收藏

          之 前一直沒有搞懂android:padding和android:layout_margin的區別,其實概念很簡單,padding是站在父view的 角度描述問題,它規定它里面的內容必須與這個父view邊界的距離。margin則是站在自己的角度描述問題,規定自己和其他(上下左右)的view之間 的距離,如果同一級只有一個view,那么它的效果基本上就和padding一樣了

          posted @ 2011-08-18 16:33 oathleo 閱讀(313) | 評論 (0)編輯 收藏

          layout在xml里是無法使用findView 找到的

          只能使用:
          View menu = inflater.inflate(R.layout.main_left_layout, null);

          獲得
          LayoutInflater 的方法:
          1.Activity
          LayoutInflater inflater = getLayoutInflater(); 
          2.
          LayoutInflater inflater = LayoutInflater.from(context);



          posted @ 2011-08-16 12:53 oathleo 閱讀(231) | 評論 (0)編輯 收藏

          1、XML資源文件可以被AAPT打包進應用程序包,如果需要被打包,放置在/res/xml目錄下,如果不需要被打包,放置在/res/raw下。

          2、對/res/xml的支持

          以讀取/res/xml/test.xml為例

          Resources res=activity.getResources();

          XmlResourceParser xpp=res.getxml(R.xml.test);

          inet eventType=xpp.getEventType();

          再根據evenType的類型,判斷讀取了什么內容,比如說,讀到了文檔的開頭,XmlPullParser.START_DOCUMENT等

          使用xpp.getName()取得標志名稱,使用xpp.getText()取得文本內容

          最后

          eventType=xpp.next();來讀取下一行內容

          3、對/res/raw的支持

          以讀取/res/raw/test.txt為例

          Resources r=activity.getResources();

          InputStream is=r.openRawResource(R.raw.test);

          4、資產

          /assets不會在R.java中生成資源ID,必須指定文件路徑才能讀取它們。文件路徑是以/assets開頭的相對路徑。

          AssetManger am=activity.getAssets();

          InputStream is=am.open("test.txt");

          posted @ 2011-08-08 11:24 oathleo 閱讀(488) | 評論 (0)編輯 收藏

               摘要: 在Android 2.3狀態欄中添加menu,home和back快捷鍵的方法 1、準備資源,修改XML文準備幾張圖,這里我們準備添加home back和menu圖標,就需要準備6張圖,三張普通狀態,三張按下的高亮狀態圖標:stat_home.pngstat_home_pressed.pngstat_back.pngstat_back_pressed.pngstat_menu.pngstat_men...  閱讀全文

          posted @ 2011-08-05 15:45 oathleo 閱讀(1125) | 評論 (0)編輯 收藏

          和奇怪,調試模式下的SAX和script
          效率巨慢,而運行模式下,好很多,大概快5-10倍。

          另外script包會編譯一個print方法,這個過程耗時很多,嚴重影響效率
          去掉并做些優化后
          500條腳本,執行從1s縮減到200ms

          代碼精簡如下:
          RhinoScriptEngine.java
              Scriptable getRuntimeScope(ScriptContext ctxt) {
                  
          if (ctxt == null) {
                      
          throw new NullPointerException("null script context");
                  }

                  
          // we create a scope for the given ScriptContext
                  Scriptable newScope = new ExternalScriptable(ctxt, indexedProps);

                  
          // Set the prototype of newScope to be 'topLevel' so that
                  
          // JavaScript standard objects are visible from the scope.
                  newScope.setPrototype(topLevel);

                  
          // define "context" variable in the new scope
                  newScope.put("context", newScope, ctxt);

                  
          // define "print", "println" functions in the new scope
                  //去掉下面幾行
          //        Context cx = enterContext();
          //        try {
          //            cx.evaluateString(newScope, printSource, "print", 1, null);
          //        } finally {
          //            cx.exit();
          //        }
                  return newScope;
              }

          posted @ 2011-08-03 14:18 oathleo 閱讀(1507) | 評論 (0)編輯 收藏

          僅列出標題
          共17頁: First 上一頁 4 5 6 7 8 9 10 11 12 下一頁 Last 
          主站蜘蛛池模板: 耿马| 绍兴县| 沁源县| 乌拉特中旗| 普格县| 邵阳市| 静宁县| 武山县| 阿克陶县| 东台市| 高平市| 拉萨市| 济南市| 漠河县| 阿坝| 衡阳市| 丰台区| 平罗县| 吴江市| 高台县| 肃宁县| 巨野县| 石渠县| 勃利县| 怀宁县| 仪陇县| 六枝特区| 平乡县| 孝义市| 龙井市| 济南市| 上饶县| 上高县| 乐平市| 新巴尔虎左旗| 林芝县| 涟源市| 会昌县| 长白| 泸州市| 宜黄县|