android 基礎知識六

Android ViewGroup提高繪製性能
Android ViewGroup如果下面有很多子View,繪製的時候,需要開啓其子View的繪製緩存功能,從而提高繪製效率。具體的代碼如下:


  1. public void setChildrenDrawingCacheEnabled(boolean enabled) {
  2.     final int count = getChildCount();
  3.     for (int i = 0; i < count; i++) {
  4.         final View view = getChildAt(i);
  5.         view.setDrawingCacheEnabled(true);
  6.          
  7.         // Update the drawing caches
  8.         view.buildDrawingCache(true);
  9.     }
  10. }
複製代碼
另一方面也可以通過setDrawingCacheQuality(low)將緩存質量降低,減少內存。
最後結束的時候,需要通過以下代碼來清空繪製緩存。


  1. void clearChildrenCache() {
  2.     final int count = getChildCount();
  3.     for (int i = 0; i < count; i++) {
  4.         final CellLayout layout = (CellLayout) getChildAt(i);
  5.         layout.setChildrenDrawnWithCacheEnabled(false);
  6.     }
  7. }
複製代碼
---------------------------------------------------------------------------
Android按指定大小讀取圖片
Android開發中,我們經常遇到Android讀取圖片大小超過屏幕顯示的圖(一般只要顯示一定規格的預覽圖即可),在圖片特別多或者圖片顯示很頻繁的時候要特別注意這個問題,下面介紹個按指定大小讀取圖像的方法。
實現原理:首先獲取圖片文件的圖像高和寬,如果小於指定比例,則直接讀取;如果超過比例則按指定比例壓縮讀取。
捕獲OutOfMemoryError時注意點:後面返回的是null,不要馬上從別的地方再讀圖片,包括R文件中的,不然依然會拋出這個異常,一般在初始化的時候緩存默認圖片,然後顯示緩存中的圖片。


  1. /** 獲取圖像的寬高**/
  2. public static int[] getImageWH(String path) {
  3.     int[] wh = {-1, -1};
  4.     if (path == null) {
  5.         return wh;
  6.     }
  7.     File file = new File(path);
  8.     if (file.exists() && !file.isDirectory()) {
  9.         try {
  10.             BitmapFactory.Options options = new BitmapFactory.Options();
  11.             options.inJustDecodeBounds = true;
  12.             InputStream is = new FileInputStream(path);
  13.             BitmapFactory.decodeStream(is, null, options);
  14.             wh[0] = options.outWidth;
  15.             wh[1] = options.outHeight;
  16.         }
  17.         catch (Exception e) {
  18.             Log.w(TAG, "getImageWH Exception.", e);
  19.         }
  20.     }
  21.     return wh;
  22. }
  23.    
  24. public static Bitmap createBitmapByScale(String path, int scale) {
  25.     Bitmap bm = null;
  26.     try {
  27.         //獲取寬高
  28.         int[] wh = getImageWH(path);
  29.         if (wh[0] == -1 || wh[1] == -1) {
  30.             return null;
  31.         }
  32.   
  33.         //讀取圖片
  34.         BitmapFactory.Options options = new BitmapFactory.Options();
  35.         options.inSampleSize = Math.max(wh[0]/scale, wh[1]/scale);
  36.         InputStream is = new FileInputStream(path);
  37.             bm = BitmapFactory.decodeStream(is, null, options);
  38.     }
  39.     catch (Exception e) {
  40.         Log.w(TAG, "createBitmapByScale Exception.", e);
  41.     }
  42.     catch (OutOfMemoryError e) {
  43.         Log.w(TAG, "createBitmapByScale OutOfMemoryError.", e);
  44.         //TODO: out of memory deal..
  45.     }
  46.     return bm;
  47. }

複製代碼
--------------------------------------------------------------------------
Android按鈕顏色設置1.工程目錄
a.在res目錄-新建drawble文件夾放入自定義圖片

20110926225648.jpg (12.25 KB, 下載次數: 0)

下載附件  保存到相冊

2012-10-16 14:01 上傳


2.main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     android:text="@string/hello"
  11.     />
  12.     <ImageButton
  13.    android:id="@+id/button"
  14.     android:layout_width="wrap_content"
  15.     android:layout_height="wrap_content"
  16.    android:background="#000000"
  17.    android:src="@drawable/sy"
  18.    />
  19. </LinearLayout>
複製代碼
3.類代碼

  1. package com.YANSE;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.ImageButton;

  5. public class YANSE extends Activity {

  6.     private ImageButton Image =null;

  7.    @Override

  8.    public void onCreate(Bundle savedInstanceState) {

  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.main);

  11.         Image=(ImageButton)findViewById(R.id.button);

  12.     }

  13. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章