Android開發之專案經驗分享

yungfan發表於2016-05-19

在Android開發中,除了基本的理論知識,還需要將所學知識運用到真實的專案中,在專案中鍛鍊自己的分析問題、解決問題的能力,本文將總結一下本人專案中遇到的一些問題,總結成章,與大家共勉~~~

1、如何拉伸一個圖片為一條線

專案需求:需要在佈局中設定一條分割線,該分割線需要自定義,美工也給了一張圖片,那麼如何實現?

在drawable目錄下建立一個repeat.xml:

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
                 android:src="@drawable/bg" 
                 android:tileMode="repeat" /> 

然後在佈局的xml檔案中可以這樣引用:

<LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="1px" 
            android:background="@drawable/repeat" > 
</LinearLayout> 
2、圖片在SQLite中的存取

(1)儲存Drawable物件到資料庫

//第一步,將Drawable物件轉化為Bitmap物件

Bitmap bmp = (((BitmapDrawable)tmp.image).getBitmap());

//第二步,宣告並建立一個輸出位元組流物件

ByteArrayOutputStream os = new ByteArrayOutputStream();

//第三步,呼叫compress將Bitmap物件壓縮為PNG格式,第二個引數為PNG圖片質量,第三個引數為接收容器,即輸出位元組流os

bmp.compress(Bitmap.CompressFormat.PNG, 100, os);

//第四步,將輸出位元組流轉換為位元組陣列,並直接進行儲存資料庫操作,注意,所對應的列的資料型別應該是BLOB型別

ContentValues values = new ContentValues();

values.put("image", os.toByteArray());

db.insert("apps", null, values);

db.close();
過程總結

Drawable→Bitmap→ByteArrayOutputStream→SQLite

(2)從資料庫讀取圖片

//第一步,從資料庫中讀取出相應資料,並儲存在位元組陣列中
byte[] blob = cursor.getBlob(cursor.getColumnIndex("image"));

//第二步,呼叫BitmapFactory的解碼方法decodeByteArray把位元組陣列轉換為Bitmap物件

Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);

//第三步,呼叫BitmapDrawable建構函式生成一個BitmapDrawable物件,該物件繼承Drawable物件,所以在需要處直接使用該物件即可

BitmapDrawable bd = new BitmapDrawable(bmp);
總結思路為

SQLite→byte[]→Bitmap→BitmapDrawable

3、修改 EditText.setError(“Info”); 的字型顏色

在 res/values/styles.xml檔案中,在自定義主題里加入一個item:

<resources xmlns:android="http://schemas.android.com/apk/res/android">  
    <style name="AppBaseTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:textColorPrimaryInverse">@android:color/primary_text_light</item>
    </style>
</resources>

然後到AndroidMenifest.xml中修改Application的主題為上述主題,即 android:theme=”@style/AppBaseTheme”

4、notifyDataSetChanged 無效的問題

問題描述:呼叫notifyDataSetChanged 介面並沒有重新整理
一般情況下,介面卡的對應的list資料來源如果發生了改變,呼叫該方法能達到重新整理列表的效果,但是有時候發現 當list的資料變化時,採用notifyDataSetChanged()無效。 仔細研究後發現,其實adapter是對list的地址的繫結,而當list重新賦值後,會導致了list指向了新的list的地址。 於是乎,為了解決這個問題,先採用 list.clear(); list.addAll(newlist); 然後採用 adapter.notifyDataSetChanged(),就搞定了。

5、如何獲取activity上所有的控制元件,並獲取自己想要的控制元件進行操作
public List<View> getAllChildViews()
   {
     //decorView是window中的最頂層view,可以從window中獲取到decorView
      View view = this.getWindow().getDecorView();
      return getAllChildViews(view);
   }
 
private List<View> getAllChildViews(View view)
   {
      List<View> allchildren = new ArrayList<View>(); 
      if (view instanceof ViewGroup)
      {
        ViewGroup vp = (ViewGroup) view;
        for (int i = 0; i < vp.getChildCount(); i++)
        {
           View viewchild = vp.getChildAt(i);
           allchildren.add(viewchild);
           allchildren.addAll(getAllChildViews(viewchild));
        }
      }
      return allchildren;
   }

public void check(List<View> list)
   {
      for (int i = 0; i < list.size(); i++)
      {
        View v = list.get(i);
        //判斷是不是Button
        if (v instanceof Button)
        {
           ((Button) v).setText("改變");
        }
      }
   }

6、去除GridView的預設點選背景顏色
GridView.setSelector(new ColorDrawable(Color.TRANSPARENT));


相關文章