Android code

Drawable、Bitmap、byte[]之間的轉換
1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {   
        Bitmap bitmap = Bitmap   
                        .createBitmap(   
                                        drawable.getIntrinsicWidth(),   
                                        drawable.getIntrinsicHeight(),   
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888   
                                                        : Bitmap.Config.RGB_565);   
        Canvas canvas = new Canvas(bitmap);   
        //canvas.setBitmap(bitmap);   
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());   
        drawable.draw(canvas);   
        return bitmap;   
}  
2、從資源中獲取Bitmap
Resources res=getResources();   
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);  

3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){   
    ByteArrayOutputStream baos = new ByteArrayOutputStream();     
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);     
    return baos.toByteArray();   
}   
4、 byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){   
            if(b.length!=0){   
                return BitmapFactory.decodeByteArray(b, 0, b.length);   
            }   
            else {   
                return null;   
            }   
5、Bitmap → Uri
 Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
android判斷EditText輸入的數字、中文還是字母方法
2010-10-23 16:46

String txt = edInput.getText().toString();

   Pattern p = Pattern.compile("[0-9]*");
     Matcher m = p.matcher(txt);
     if(m.matches() ){
      Toast.makeText(Main.this,"輸入的是數字", Toast.LENGTH_SHORT).show();
      }
     p=Pattern.compile("[a-zA-Z]");
     m=p.matcher(txt);
     if(m.matches()){
      Toast.makeText(Main.this,"輸入的是字母", Toast.LENGTH_SHORT).show();
     }
     p=Pattern.compile("[/一-/龥]");
     m=p.matcher(txt);
     if(m.matches()){
      Toast.makeText(Main.this,"輸入的是漢字", Toast.LENGTH_SHORT).show();
     }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
  // 豎屏
}
else if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//橫屏
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Android的Btimap處理大圖片解決方法

原文: http://www.android123.com.cn/kaifafaq/594.html

        我們都知道Android的Dalvik VM爲一個應用提供了大約16MB的內存,一般我們處理超過8MB的圖片將會出現OutOfMemoryError異常,我們解碼一個圖片爲了防止內存不 足的異常我們可以使用BitmapFactory.Options 的udeinTempStorage屬性解決,代碼如下:

BitmapFactory.Options cwj = new BitmapFactory.Options();
cwj.inTempStorage = new byte[1024*1024*5]; //5MB的臨時存儲空間
Bitmap bm = BitmapFactory.decodeFile(inputStream,cwj); //這裏cwj爲Options屬性

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