0

我在拍摄屏幕快照时遇到问题。位图绘制除视图底部之外的所有内容。我附上了一些布局代码以及来自快照调用的代码。

该活动还有一个带有TabListeners的操作栏。所以它在操作栏中有 3 个选项卡和几个按钮(它们显示在快照上。如果我可以在快照中删除它们会很好,因为我根本不需要它们^^U 但它不是重要的事情)。

快照:

    private void snapShot() {
        Log.d("On snapShot","At beggining");
        Bitmap bitmap;
        getWindow().getDecorView().setDrawingCacheEnabled(true);    
        try{
            bitmap = Bitmap.createBitmap(getWindow().getDecorView().getDrawingCache());
        }catch (IllegalStateException e){
            bitmap = null;
        }
        try{    
            Log.d("On snapShot", "inside Try");
            File root = Environment.getExternalStorageDirectory();              
            if (root.canWrite()){
                pic = new File(root,"pic.png");
                FileOutputStream out = new FileOutputStream(pic); 
                bitmap.compress(CompressFormat.PNG, 100, out); 
                out.flush(); 
                out.close();
            }
        }catch(IOException e) { 
            Log.e("BROKEN", "Could not write file " + e.getMessage());
        }
        getWindow().getDecorView().setDrawingCacheEnabled(false);       
     }

布局:

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

    <LinearLayout 
        android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal" 
  android:layout_weight="20"
  >
  <com.example.daemon.views.CaptionView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id = "@+id/legendView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
  </com.example.daemon.views.CaptionView>

        <View 
            android:id="@+id/lineSeparation"
            android:background="#F000"
            android:layout_marginTop="1dp"
            android:layout_height="1dp"
            android:layout_width="match_parent"
         /> 
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <com.example.daemon.views.ChartView
      xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/viewChart"
          android:layout_width="fill_parent"
      android:layout_height="fill_parent">
  </com.example.daemon.views.ChartView>
    </LinearLayout>
</LinearLayout>

添加更多代码,在其中创建操作栏和选项卡。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    // Set the application works only on Landscape orientation.
    actionBar = getActionBar();                                     // Creates the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);                
    Log.i(TAG, "On create");
    String label1 = "one day";              
    Tab tab = actionBar.newTab();           // Creates a tab
    tab.setText(label1);                

    // Links the tab created with an existing Fragment 
    TabListener<Tab1Fragment> tl = new TabListener<Tab1Fragment>(this, label1, Tab1Fragment.class);     
    tab.setTabListener(tl);                 // Makes the tab  respond the touch events.
    actionBar.addTab(tab);                  // Add the tab to the action bar.

当我推送快照时的PNG: 在此处输入图像描述

它应该如何出现,来自我的相机的图片: 在此处输入图像描述

问题(我认为)是当创建位图时,它的最大高度为 732(与视图相同)但它开始从顶部捕获视图(包括不在视图上的操作栏)所以它只能绘制前 732 行。关键是,如果我说在操作栏之后开始,则会出现错误,因为“y + height cannot be > bitmap.height”所以......我该怎么办?

十分感谢!!!

4

2 回答 2

1

我找到了解决方案。很难找到它,但它很容易。我只是在第一个 LinearLayout (包含所有其他内容的那个)中添加一个 id。然后我可以创建通过 id 查找视图的位图。

在我添加的布局中:android:id="@+id/mainLayout" 然后在活动中:

int id = R.id.mainLayout;
View view = findViewById(id);                   
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(getWindow().findViewById(id).getDrawingCache());    

感谢您的支持,我希望这对其他人有所帮助。

于 2012-11-06T10:31:47.800 回答
0

也许设置 BitMap 的大小可能会解决您的问题。

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
bitmap = Bitmap.createBitmap(getWindow().getDecorView().getDrawingCache(), 0, 0, width, height);
于 2012-10-31T14:04:03.960 回答