用view获取当前layout时,layout中imageview中用Bipmap格式加载的图片无法显示?

J_ANNA 2016-04-28 09:23:52
用view获取当前layout时,layout中imageview中用Bipmap格式加载的图片无法显示?但imageview是获取到了的,因为改变imageview的背景颜色,view中是有显示的,但为什么里面的图片不显示?
View获取layout代码:
View view = LayoutInflater.from(getActivity()).inflate(R.layout.reportlayout, null, false);
Imageview中加载图片代码:
rimgwaveform.setImageBitmap(bitmapwave);
我开始怀疑是Imageview加载图片的格式bitmap不对,但换成drawable还是不显示。主要是在ImageView中加载本地图片都是要用Bitmap格式,这就让我很困惑了。求各位大神帮忙支支招
...全文
284 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
J_ANNA 2016-04-29
  • 打赏
  • 举报
回复
引用 8 楼 qq_29628249 的回复:
有没有试过用bitmapfactory 来拿资源
还没有
qq_29628249 2016-04-29
  • 打赏
  • 举报
回复
有没有试过用bitmapfactory 来拿资源
jklwan 2016-04-28
  • 打赏
  • 举报
回复
inflate的第二个参数改为当前的视图。而且代码多发点儿呢,现在什么都看不出来
J_ANNA 2016-04-28
  • 打赏
  • 举报
回复
引用 1 楼 jklwan 的回复:
inflate的第二个参数改为当前的视图。而且代码多发点儿呢,现在什么都看不出来
大神,我的问题解决了,我没用View view = LayoutInflater.from(getActivity()).inflate(R.layout.reportlayout, null, false); 这个方法了。 改成直接把当前最顶层的linearout设置成一个View,然后将此View转成Bitmap格式后保存到本地即可。 部分代码: private View linearout; linearout=getView().findViewById(R.id.LinearLayout1); public Bitmap convertViewToBitmap(View view){ Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //利用bitmap生成画布 Canvas canvas = new Canvas(bitmap); //把view中的内容绘制在画布上 view.draw(canvas); return bitmap; } private View.OnClickListener btnimageOnClick=new View.OnClickListener() { @Override public void onClick(View v) { demo(); Bitmap bitmapwave=convertViewToBitmap(linearout); // rimgwaveform.setImageBitmap(bitmapwave); String bitName ="保存bitname"; saveMyBitmap(bitName,bitmapwave); } }; public File saveMyBitmap(String bitName,Bitmap mBitmap){ File f = new File("/sdcard/" + bitName + ".webp"); filename="/sdcard/" + bitName + ".webp"; try { f.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block //DebugMessage.put("在保存图片时出错:"+e.toString()); } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } mBitmap.compress(Bitmap.CompressFormat.WEBP, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } return f; } 最后,感谢您的热心帮助!!!
J_ANNA 2016-04-28
  • 打赏
  • 举报
回复
大哥,这是之前的代码,并不是解决问题后的代码。
J_ANNA 2016-04-28
  • 打赏
  • 举报
回复
View priview=LayoutInflater.from(getActivity()).inflate(R.layout.reportlayout,
null,false);
View view= LayoutInflater.from(getActivity()).inflate(R.layout.reportlayout,
(ViewGroup)priview,false);
printer.shotViewAsImage(rbtnprint, view, true);
}


改为当前视图还是不行
kkboy20 2016-04-28
  • 打赏
  • 举报
回复
收到了。 谢谢分享.
J_ANNA 2016-04-28
  • 打赏
  • 举报
回复
public class PrintUtil { private static final String TAG = "PrintUtil"; public String SCREEN_SHOTS_LOCATION = Environment .getExternalStorageDirectory()+ "/AAA/"; //保存到SD卡指定文件夹“AAA”中,若文件夹不存在则自动生成以此命名的文件夹 /** * 对当前view进行截屏,并保存到SCREEN_SHOTS_LOCATION指定的目录下 * @param view * @param name * @return * @throws Exception */ public void takeScreenShot(View view, String name,TakeScreenShotCallBack callback) throws Exception{ takeScreenShot(view,name,SCREEN_SHOTS_LOCATION,callback); } TakeScreenShotCallBack mCallback = new TakeScreenShotCallBack(){ @Override public void excute(File file) { // TODO Auto-generated method stub } }; /** * 把传入的view保存为图片 * @param parent 事件触发视图,可以是button等 * @param view 保存为图片的视图 * @param isShow 是否显示展示视图 * @return */ public void shotViewAsImage(View parent,View view, boolean isShow){ PopupWindow popupWindow = getPopuptWindow(view); if(isShow) popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0); File newFile = null; try { takeScreenShot(popupWindow.getContentView(),null,mCallback); } catch (Exception e) { Log.e(TAG, e.getMessage()); } } /** * 创建popupWindow * @param popupWindow_view * @return */ protected PopupWindow getPopuptWindow(View popupWindow_view) { PopupWindow popupWindow = null; popupWindow = new PopupWindow(popupWindow_view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);// 创建PopupWindow实例 //popupWindow = new PopupWindow(popupWindow_view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);// 创建PopupWindow实例 popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(false); return popupWindow; } public interface TakeScreenShotCallBack{ public void excute(File file); } /** * 对当前view进行截屏,并保存到path指定的目录下 * @param view * @param name * @param path * @return * @throws Exception */ public void takeScreenShot(final View view, final String name,final String path,final TakeScreenShotCallBack callback) throws Exception { final Bitmap bitmap = convertViewToBitmap(view); Runnable runnable = new Runnable() { @Override public void run() { Canvas canvas = new Canvas(bitmap); int w = bitmap.getWidth(); int h = bitmap.getHeight(); Paint paint = new Paint(); paint.setColor(Color.YELLOW); String n = null; if(null == name || "".equals(name)){ SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddhhmmss"); n = simple.format(new Date()); }else{ n = name; } canvas.drawText("", w - w / 2, h - h / 10, paint); canvas.save(); canvas.restore(); FileOutputStream fos = null; File file = null; try { File sddir = new File(path); if (!sddir.exists()) { sddir.mkdirs(); } //生成以时间为名称的文件 file = new File(path + File.separator + n + ".png"); fos = new FileOutputStream(file); if (fos != null) { bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close(); } } catch (Exception e) { Log.e(TAG, e.getMessage(),e); } callback.excute(file); } }; Thread thread = new Thread(runnable); thread.start(); } /** * 转换view为bitmap * @param view * @return */ public Bitmap convertViewToBitmap(View view){ //view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY)); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; } private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
J_ANNA 2016-04-28
  • 打赏
  • 举报
回复
public Bitmap convertViewToBitmap(View view){ Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //利用bitmap生成画布 Canvas canvas = new Canvas(bitmap); //把view中的内容绘制在画布上 view.draw(canvas); return bitmap; } /**生成bitmap并显示在imageview中**/ private View.OnClickListener btnimageOnClick=new View.OnClickListener() { @Override public void onClick(View v) { demo(); Bitmap bitmapwave=convertViewToBitmap(simpleWaveform); rimgwaveform.setImageBitmap(bitmapwave); } }; public void printutil(String data){ View view= LayoutInflater.from(getActivity()).inflate(R.layout.reportlayout, null,false); printer.shotViewAsImage(rbtnprint, view, true); } //保存图片到本地 private View.OnClickListener btnprintOnClick=new View.OnClickListener() { @Override public void onClick(View v) { String data="第一个保存文件"; // TODO 自动生成的方法存根 printutil(data); } };
内容概要:本文围绕分布式电源接入对配电网的影响展开研究,利用Matlab进行建模仿真与代码实现,系统分析了分布式电源(如光伏、风电等)接入后对配电网在电能质量、潮流分布、电压稳定性、保护配置等方面的影响。研究涵盖了多种分布式电源类型与不同渗透率场景,通过构建典型的配电网模型,仿真其在正常运行及故障条件下的动态响应特性,重点探讨了分布式电源引起的电压越限、反向潮流、短路电流水平变化等问题,并提出了相应的优化调控策略与解决方案。同,结合主动配电网的有功无功协调优化、鲁棒调度等高级应用,展示了如何借助现代优化算法提升系统接纳能力与运行经济性。; 适合人群:具备电力系统基础知识,熟悉Matlab/Simulink仿真环境,从事新能源接入、配电网规划与运行等相关领域的科研人员、工程师及高校研究生。; 使用场景及目标:①掌握分布式电源接入对配电网关键指标的影响机制;②学习基于Matlab的配电网建模与仿真方法;③理解并实现主动配电网的协调优化调度算法;④为实际工程分布式电源并网方案设计与问题诊断提供理论支持和技术参考。; 阅读建议:建议读者结合文提供的Matlab代码,逐步复现仿真案例,深入理解模型构建与算法实现细节,并尝试在不同参数设置或网络结构下进行拓展实验,以增强对系统动态行为的认知与分析能力。

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧