2013-01-09 122 views
0

我已经尝试了很多方法。我能够截屏整个屏幕,但我的要求是采取整个Horizo​​ntalScrollView截图并将其保存为图像文件。下面是我使用的示例XML布局:捕获Horizo​​ntalScrollView的屏幕截图,包括离屏元素

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <HorizontalScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/hsv" > 

     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:orientation="horizontal" 
      android:id="@+id/ll"> 

      <Button 
       android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:onClick="click" 
      android:id="@+id/b" 
      android:text="capture" 
      android:layout_margin="20dp"/> 

      <ImageView 
       android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:id="@+id/img1" 
      android:src="@drawable/plug1" 
      android:layout_margin="20dp"/> 

      <ImageView 
       android:layout_height="wrap_content" 
      android:layout_width="200dp" 
      android:id="@+id/img2" 
      android:src="@drawable/plug3" 
      android:layout_margin="20dp"/> 

     </LinearLayout> 

    </HorizontalScrollView> 

</RelativeLayout> 

活动代码:

Button b; 
    ImageView img1, img2; 
    LinearLayout ll; View v; 
    HorizontalScrollView s; 
    int h,w; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     b = (Button)findViewById(R.id.b); 
     img1 = (ImageView)findViewById(R.id.img1); 
     img2 = (ImageView)findViewById(R.id.img2); 

     ll = (LinearLayout)findViewById(R.id.ll); 

     s = (HorizontalScrollView) findViewById(R.id.hsv); 
     s.setDrawingCacheEnabled(true); 

    } 

    //Fired onClick of the button 
     public void click(View v) { 
      shoot(); 
     } 

    // To capture layout, create a bitmap and replace an image with the resulting bitmap. 
    public void shoot() { 

     s.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 

     int totalHeight = s.getMeasuredHeight(); 
     int totalWidth = s.getMeasuredWidth(); 

     s.layout(0, 0, totalHeight, totalWidth);  
     s.buildDrawingCache(true); 

     Bitmap b = Bitmap.createBitmap(s.getDrawingCache());    
     s.setDrawingCacheEnabled(false); 

     img1.setImageBitmap(b);  
    } 

,如果我不使用MeasureSpec我没有得到正确的布局的高度和宽度。

OnClick的按钮我只得到半按钮的图像。我认为这是因为我没有使用ViewTreeObserver。但是当我使用ViewTreeObserver时,我没有得到结果。此外,当我单击按钮两次时,应用程序崩溃 - Bitmap中的NullPointerException。

我已经尝试了几乎所有可用的StackOverFlow。请帮助我找到一种方法来做到这一点。它甚至有可能吗?

回答

1

找到了一个简单的方法来做到这一点。

XML

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:layout_gravity="center_vertical"> 

    <HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 

     <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:orientation="horizontal" 
        android:id="@+id/def" 
        > 

    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="capture" 
     android:onClick="click"/> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/black" 
     android:id="@+id/img"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/a"/> 

    </LinearLayout> 

    </HorizontalScrollView> 

</LinearLayout> 

Acivity代码:

public class CaptureBeyondActivity extends Activity { 
/** Called when the activity is first created. */ 

LinearLayout def; 
ImageView img; 
HorizontalScrollView hsv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

def = (LinearLayout)findViewById(R.id.def); 
img = (ImageView)findViewById(R.id.img); 

} 

public void click(View v) { 
Bitmap bitmap; 
def.setDrawingCacheEnabled(true); 
def.buildDrawingCache(true); 

     bitmap = Bitmap.createBitmap(def.getWidth(), def.getHeight(), 
       Config.ARGB_8888); 
     def.layout(0, 0, def.getLayoutParams().width, 
       def.getLayoutParams().height); 
     Canvas c = new Canvas(bitmap); 
     def.draw(c); 

def.setDrawingCacheEnabled(false); 

if(bitmap!=null){ 
    img.setImageBitmap(bitmap); 
    img.invalidate(); 
} 
}} 

在这里,我已经放在一个Horizo​​ntalScrollView一些元素。点击按钮后会截取截图,第二张图片将被截图所取代。 它也需要截屏的元素的屏幕截图。您可以进一步增强代码并将最终的位图图像保存在SD卡上。

0

这是我第二次看到同样的问题(是的,这里是堆栈溢出),简单的答案是:没有简单的方法来做到这一点!

保存屏幕快照的所有方法都使用了启用绘制到位图缓存并获取此位图的相同原理。 但是如果视图不在屏幕上,它们将不会被绘制。

您可以创建一个非常复杂的系统,它将自动滚动水平屏幕视图,并拍摄多个屏幕截图并将不同的位图拼接在一起。或者可能是一个非常复杂的系统,它会创建无限宽度的画布并要求布局绘制它,但很可能您会在这种情况下耗尽内存。但正如我所说,这些都不是一件简单的事情。

+0

谢谢。你可以建议我一些东西,我可以放大scrollview的全部内容以适应屏幕,然后截取屏幕截图。如果可能的话,任何答案都会有很大的帮助。 –

+0

找到了一个方法。请参阅我的回答。 –