简体   繁体   English

Android拍摄截图

[英]Android taking a screenshot image

Hi I tried to write a function that takes a screenshot. 嗨,我试着编写一个截图的功能。 I found a code that does it perfectly with a button clicklistner,but when I remove the button clicklistner and just try to take a screenshot in the oncreate the bitmap I get is empty. 我找到了一个代码,通过按钮clicklistner完美地完成它,但当我删除按钮clicklistner并尝试在oncreate中截取我获得的位图是空的。 Why does it happens? 为什么会这样?

layout: 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
>
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="munch"
    android:id="@+id/munchscreen"
    />
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="500dp"
    android:id="@+id/screenshots"

    />

Activity: 活动:

public class MainActivity extends Activity {
    LinearLayout L1;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);


        L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
        Button but = (Button) findViewById(R.id.munchscreen);
        but.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                View v1 = L1.getRootView();
                v1.setDrawingCacheEnabled(true);
                Bitmap bm = v1.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                image = (ImageView) findViewById(R.id.screenshots);
                image.setBackgroundDrawable(bitmapDrawable);
            }
        });

    }


}

just try to take a screenshot in the oncreate the bitmap I get is empty. 只是尝试在oncreate中截取我得到的位图是空的。 Why does it happens? 为什么会这样?

The UI will not have been rendered yet. 用户界面尚未呈现。 Given the approach that you are using, you need for the framework to have actually drawn the UI before you can capture a screenshot. 鉴于您正在使用的方法,您需要框架在捕获屏幕截图之前实际绘制UI。

If, instead, you have the root View draw() to a Bitmap -backed Canvas , that might work already in onCreate() . 相反,如果你有根View draw()Bitmap -backed Canvas ,那可能onCreate()已经有效。 It depends on whether the root View has been called with measure() and layout() yet, and I'm not sure whether that happens already in onCreate() or at a later point. 这取决于是否已使用measure()layout()调用了根View ,并且我不确定它是否已经在onCreate()或稍后发生。

you can try this. 你可以试试这个。 public class MainActivity extends Activity { LinearLayout L1; 公共类MainActivity扩展Activity {LinearLayout L1; ImageView image; ImageView图像;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);


    L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
    Button but = (Button) findViewById(R.id.munchscreen);
    /*but.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            View v1 = L1.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bm = v1.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
            image = (ImageView) findViewById(R.id.screenshots);
            image.setBackgroundDrawable(bitmapDrawable);
        }
    });*/
}
  @Override
 public void onPostCreate()
 {
  View v1 = L1.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bm = v1.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
            image = (ImageView) findViewById(R.id.screenshots);
            image.setBackgroundDrawable(bitmapDrawable);
 }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM