Android code

本文详细介绍了如何在Android应用中实现Drawable、Bitmap与字节数组之间的转换,并提供了判断EditText输入为数字、字母或汉字的方法,以及如何根据设备的屏幕方向设置布局。此外,还分享了解决处理大图片时可能出现的内存溢出问题的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值