`
ming_fanglin
  • 浏览: 221189 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
1,返回图片名称列表
context.getResources().getAssets().list("image")


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


3,判断是否已存在文件
context.getFileStreamPath(file).isFile()


4,写文件
# 保存的模式:   
#          *  使用context中的文件输出流它有四种模式: 
#          *  文件读写的操作模式: 
#          *      Context.MODE_PRIVATE=0:只能是当前的应用才能操作文件 如果创建的文件已经存在 新内容覆盖原内容 
#          *      Context.MODE_APPEND=32768:新内容追加到原内容后 这个模式也是私有的 这个文件只能被创建文件的应用所访问 
#          *      Context.MODE_WORLD_READABLE=1:允许其他应用程序读取本应用创建的文件 
#          *      Context.MODE_WORLD_WRITEABLE=2:允许其他应用程序写入本应用程序创建的文件,会覆盖原数据。              
HttpURLConnection conn;
			try {
				conn = (HttpURLConnection) myFileUrl.openConnection();
				conn.setDoInput(true);
				conn.connect();
				InputStream is = conn.getInputStream();

				FileOutputStream output = context.openFileOutput(
						"filename",
						Context.MODE_PRIVATE);
				int ch = 0;
				int max = 1;
				byte[] buffer = new byte[max];
				while ((ch = is.read(buffer, 0, max)) != -1) {
					output.write(buffer, 0, ch);
				}
				output.close();
				is.close();} catch (IOException e) {
				e.printStackTrace();
				Toast.makeText(context, "错误!", Toast.LENGTH_SHORT).show();
			}


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

6,handle 发送消息
mHandler.sendMessage(mHandler.obtainMessage());


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

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

9,图片写文字
public static Bitmap drawTextAtBitmap(Bitmap bitmap, String text) {
		int x = bitmap.getWidth();

		int y = bitmap.getHeight();

		// 创建一个和原图同样大小的位图
		Bitmap newbit = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);

		Canvas canvas = new Canvas(newbit);

		Paint paint = new Paint();

		// 在原始位置0,0插入原图
		canvas.drawBitmap(bitmap, 0, 0, paint);

		paint.setColor(Color.RED);

		paint.setTextSize(10);

		// 在原图指定位置写上字
		canvas.drawText(text, 2, 20, paint);

		canvas.save(Canvas.ALL_SAVE_FLAG);

		// 存储
		canvas.restore();

		return newbit;

	}


10,函数反射调用(通过函数名调用)
try {
			Class cla = Class.forName("com.android.util.AllAndroidMethod");
			Object cObject = cla.newInstance();
			Method[] methodlist = cla.getDeclaredMethods();
			for (Method method : methodlist) {
				if (method.getName().equalsIgnoreCase(methodNameString)) {
					Class pts[] = method.getParameterTypes();
					Method method2 = cla.getDeclaredMethod(method.getName(),
							pts);

						method2.invoke(cObject, arg0, list);
					
						method2.invoke(cObject, arg0, arg1, list);
					
						method2.invoke(cObject, arg0, arg1);
					
						method2.invoke(cObject, arg0);

				}
			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


11,基站定位的一点
 public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   

		int cid = -1;
		int lac = -1;
		int nbid = -1;
		int nbrssi = -1;

		TelephonyManager tm  = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		GsmCellLocation gcl = (GsmCellLocation)tm.getCellLocation();
		if(gcl != null)
		{
			cid = gcl.getCid();
			lac = gcl.getLac();
		}

		NeighboringCellInfo nbinfo = new NeighboringCellInfo();
		nbid = nbinfo.getCid();    //获取邻居小区号
		nbrssi = nbinfo.getRssi(); //获取邻居小区信号强度

		TextView tv = new TextView(this);
		String str = "CellID = " + cid + ", Lac = " + lac + ", nbid = " + nbid + ", nbrssi = " + nbrssi;
		tv.setText(str);
		setContentView(tv);
    }
3
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics