Android 学习笔记(二) 各种技巧小知识

http://ming-fanglin.iteye.com/blog/793532


1,返回图片名称列表 

Java代码   收藏代码
  1. context.getResources().getAssets().list("image")  


2,确认图片名字,拿到bitmap 
Java代码   收藏代码
  1. InputStream inputStream = context.getResources()  
  2.                                 .getAssets().open("image/" + url);  
  3.                         bitmap = BitmapFactory.decodeStream(inputStream);  


3,判断是否已存在文件 
Java代码   收藏代码
  1. context.getFileStreamPath(file).isFile()  


4,写文件 
Java代码   收藏代码
  1. # 保存的模式:     
  2. #          *  使用context中的文件输出流它有四种模式:   
  3. #          *  文件读写的操作模式:   
  4. #          *      Context.MODE_PRIVATE=0:只能是当前的应用才能操作文件 如果创建的文件已经存在 新内容覆盖原内容   
  5. #          *      Context.MODE_APPEND=32768:新内容追加到原内容后 这个模式也是私有的 这个文件只能被创建文件的应用所访问   
  6. #          *      Context.MODE_WORLD_READABLE=1:允许其他应用程序读取本应用创建的文件   
  7. #          *      Context.MODE_WORLD_WRITEABLE=2:允许其他应用程序写入本应用程序创建的文件,会覆盖原数据。                
  8. HttpURLConnection conn;  
  9.             try {  
  10.                 conn = (HttpURLConnection) myFileUrl.openConnection();  
  11.                 conn.setDoInput(true);  
  12.                 conn.connect();  
  13.                 InputStream is = conn.getInputStream();  
  14.   
  15.                 FileOutputStream output = context.openFileOutput(  
  16.                         "filename",  
  17.                         Context.MODE_PRIVATE);  
  18.                 int ch = 0;  
  19.                 int max = 1;  
  20.                 byte[] buffer = new byte[max];  
  21.                 while ((ch = is.read(buffer, 0, max)) != -1) {  
  22.                     output.write(buffer, 0, ch);  
  23.                 }  
  24.                 output.close();  
  25.                 is.close();} catch (IOException e) {  
  26.                 e.printStackTrace();  
  27.                 Toast.makeText(context, "错误!", Toast.LENGTH_SHORT).show();  
  28.             }  


5,image.mutate() 
mutate()方法是让图片资源mutable,内部工作原理应该就是克隆了一份自己。 

6,handle 发送消息 
Java代码   收藏代码
  1. mHandler.sendMessage(mHandler.obtainMessage());  


7,图片渐变效果实现思路 
利用消息机制,递归设置图片的透明度,用一个变量标识是否已经完全显示。 

8,handle 消息机制,非UI线程调用 
这些handler只要用了default的Looper,他们之间就能通信。 

9,图片写文字 
Java代码   收藏代码
  1. public static Bitmap drawTextAtBitmap(Bitmap bitmap, String text) {  
  2.         int x = bitmap.getWidth();  
  3.   
  4.         int y = bitmap.getHeight();  
  5.   
  6.         // 创建一个和原图同样大小的位图  
  7.         Bitmap newbit = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);  
  8.   
  9.         Canvas canvas = new Canvas(newbit);  
  10.   
  11.         Paint paint = new Paint();  
  12.   
  13.         // 在原始位置0,0插入原图  
  14.         canvas.drawBitmap(bitmap, 00, paint);  
  15.   
  16.         paint.setColor(Color.RED);  
  17.   
  18.         paint.setTextSize(10);  
  19.   
  20.         // 在原图指定位置写上字  
  21.         canvas.drawText(text, 220, paint);  
  22.   
  23.         canvas.save(Canvas.ALL_SAVE_FLAG);  
  24.   
  25.         // 存储  
  26.         canvas.restore();  
  27.   
  28.         return newbit;  
  29.   
  30.     }  


10,函数反射调用(通过函数名调用) 
Java代码   收藏代码
  1. try {  
  2.             Class cla = Class.forName("com.android.util.AllAndroidMethod");  
  3.             Object cObject = cla.newInstance();  
  4.             Method[] methodlist = cla.getDeclaredMethods();  
  5.             for (Method method : methodlist) {  
  6.                 if (method.getName().equalsIgnoreCase(methodNameString)) {  
  7.                     Class pts[] = method.getParameterTypes();  
  8.                     Method method2 = cla.getDeclaredMethod(method.getName(),  
  9.                             pts);  
  10.   
  11.                         method2.invoke(cObject, arg0, list);  
  12.                       
  13.                         method2.invoke(cObject, arg0, arg1, list);  
  14.                       
  15.                         method2.invoke(cObject, arg0, arg1);  
  16.                       
  17.                         method2.invoke(cObject, arg0);  
  18.   
  19.                 }  
  20.             }  
  21.   
  22.         } catch (ClassNotFoundException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         } catch (IllegalAccessException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         } catch (InstantiationException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         } catch (SecurityException e) {  
  32.             // TODO Auto-generated catch block  
  33.             e.printStackTrace();  
  34.         } catch (IllegalArgumentException e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         } catch (NoSuchMethodException e) {  
  38.             // TODO Auto-generated catch block  
  39.             e.printStackTrace();  
  40.         } catch (InvocationTargetException e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  


11,基站定位的一点 
Java代码   收藏代码
  1. public void onCreate(Bundle savedInstanceState) {     
  2.        super.onCreate(savedInstanceState);     
  3.   
  4.     int cid = -1;  
  5.     int lac = -1;  
  6.     int nbid = -1;  
  7.     int nbrssi = -1;  
  8.   
  9.     TelephonyManager tm  = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  10.     GsmCellLocation gcl = (GsmCellLocation)tm.getCellLocation();  
  11.     if(gcl != null)  
  12.     {  
  13.         cid = gcl.getCid();  
  14.         lac = gcl.getLac();  
  15.     }  
  16.   
  17.     NeighboringCellInfo nbinfo = new NeighboringCellInfo();  
  18.     nbid = nbinfo.getCid();    //获取邻居小区号  
  19.     nbrssi = nbinfo.getRssi(); //获取邻居小区信号强度  
  20.   
  21.     TextView tv = new TextView(this);  
  22.     String str = "CellID = " + cid + ", Lac = " + lac + ", nbid = " + nbid + ", nbrssi = " + nbrssi;  
  23.     tv.setText(str);  
  24.     setContentView(tv);  
  25.    }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值