2012-12-12 70 views
1

我已经相对布局命名rel动态地将一个imageview的它,像这样:试图采取视图的屏幕截图和它的孩子

 rel.setBackgroundResource(R.drawable.bg_share_one); 

       RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(921, 691); 
       p1.leftMargin = 30; 
       p1.topMargin = 30; 

       ImageView img = new ImageView(this); 
       img.setLayoutParams(p1); 
       myBitmap = BitmapFactory.decodeFile(files[0].getAbsolutePath(), option1); 
       img.setImageBitmap(myBitmap); 

       rel.addView(img); 
       saveCompareImage(); 

然后我把这个方法采取截屏的相对但它添加的imageview没有显示在位图中:

protected void saveCompareImage() { 
    // TODO Auto-generated method stub 

    rel.setDrawingCacheEnabled(true); 
    try { 

     File file = new File("/sdcard/LC/compare.jpg"); 
     file.createNewFile(); 
     Bitmap bm = rel.getDrawingCache(); 
     FileOutputStream ostream = new FileOutputStream(file); 
     bm.compress(Bitmap.CompressFormat.JPEG, 80, ostream); 
     ostream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

回答

1

也许您已启用硬件加速?的setDrawingCacheEnabled()的文件指出,在这种情况下,渲染不同的做法:

当硬件加速被开启,使绘图缓存 对渲染,因为系统使用不同的 机制,加速其忽略了没有效果旗。如果要为视图使用 位图,即使已启用硬件加速,有关如何启用软件和硬件层的信息,请参阅 setLayerType(int,android.graphics.Paint)。

尝试setLayerType(LAYER_TYPE_SOFTWARE, null)什么的。

+0

会是这样的相对布局应该得到这个或ImageView的我加入吧..我都尝试并没有什么区别..仍然没有ImageView的显示up – erik

+0

我不确定,从来没有尝试过,只是看到了API Doc的一部分。我猜你必须在布局和图像视图中都使用它。 – Ridcully

+0

不行不行 – erik

0

问题没有要求的措施:本作品:

protected void saveCompareImage() { 
    // TODO Auto-generated method stub 

    rel.setDrawingCacheEnabled(true); 

    try { 
     File file = new File("/sdcard/LC/compare.jpg"); 
     file.createNewFile(); 
     Bitmap bitmap; 
     rel.setDrawingCacheEnabled(true); 
     rel.measure(MeasureSpec.makeMeasureSpec(rel.getLayoutParams().width, MeasureSpec.EXACTLY), 
       MeasureSpec.makeMeasureSpec(rel.getLayoutParams().height, MeasureSpec.EXACTLY)); 
     rel.layout(0, 0, rel.getMeasuredWidth(), rel.getMeasuredHeight()); 

     bitmap = Bitmap.createBitmap(rel.getDrawingCache()); 


     FileOutputStream ostream = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream); 
     ostream.close(); 
    } catch (Exception e) { 
     Log.v("ERRRO","e:"+e); 
    } 
} 
+0

很高兴你找到了解决办法。 – Ridcully