Android开发:用getDrawingCache方法获取ImageView中的图像需要注意的问题(二)

2014-11-24 11:36:41 来源: 作者: 浏览: 6
btn_photo.setOnTouchListener(this);


}



public boolean onTouch(View v, MotionEventevent)


{


switch(v.getId())


{


case R.id.iv_photo:


if(event.getAction() ==MotionEvent.ACTION_DOWN)


{


// ImageView对象(iv_photo)必须做如下设置后,才能获取其中的图像


iv_photo.setDrawingCacheEnabled(true);


// 在ImageView对象(iv_photo)被touch down的时候,获取ImageView中的图像


obmp = Bitmap.createBitmap(iv_photo.getDrawingCache());


//然后在OK按钮(btn_photo)被touch down的时候,比较ImaageView对象(iv_photo)中的图像和


//obmp是否一致,以便做进一步的处理,比如,如果不一致就保存,否则就不保存到数据库中。



//从ImaggeView对象中获取图像后,要记得调用setDrawingCacheEnabled(false)清空画图缓


//冲区,否则,下一次用getDrawingCache()方法回去图像时,还是原来的图像


iv_photo.setDrawingCacheEnabled(false);



// 将得到obmp写入文件


FileOutputStream m_fileOutPutStream = null;


String filepath = Environment.getExternalStorageDirectory() +File.separator + "tempPhoto.png";


try


{


m_fileOutPutStream= new FileOutputStream(filepath);


}


catch (FileNotFoundException e)


{


e.printStackTrace();


}



obmp.compress(CompressFormat.PNG, 100, m_fileOutPutStream);



try


{


m_fileOutPutStream.flush();


m_fileOutPutStream.close();


}


catch (IOException e)


{


e.printStackTrace();


}



Intent intent = new Intent(this, TakePicture.class);


startActivityForResult(intent, 1);


}


break;


case R.id.btn_photo:


if(event.getAction() ==MotionEvent.ACTION_DOWN)


{


// ImageView对象(iv_photo)必须做如下设置后,才能获取其中的图像


iv_photo.setDrawingCacheEnabled(true);


// 获取ImageView中的图像


Bitmap sbmp =Bitmap.createBitmap(iv_photo.getDrawingCache());


// 从ImaggeView对象(iv_photo)中获取图像后,要记得调用setDrawingCacheEnabled(false)


// 清空画图缓冲区


iv_photo.setDrawingCacheEnabled(false);

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: