【问题标题】:Android taking a screenshot imageAndroid 截取图片
【发布时间】:2015-12-17 20:59:43
【问题描述】:

您好,我尝试编写一个截取屏幕截图的函数。 我发现一个代码可以完美地使用按钮 clicklistner,但是当我删除按钮 clicklistner 并尝试在 oncreate 中截取屏幕截图时,我得到的位图是空的。 为什么会这样?

布局:

<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"

    />

活动:

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);
            }
        });

    }


}

【问题讨论】:

  • 您可以尝试将代码写入onPostCreate()

标签: android bitmap screenshot


【解决方案1】:

尝试在 oncreate 中截屏,我得到的位图是空的。为什么会这样?

UI 尚未渲染。鉴于您使用的方法,您需要让框架实际绘制 UI,然后才能捕获屏幕截图。

相反,如果您将根 View draw()Bitmap-backed Canvas 联系起来,那么 可能 已经在 onCreate() 中工作了。这取决于是否已经使用 measure()layout() 调用了根 View,我不确定这是否已经在 onCreate() 或稍后发生。

【讨论】:

    【解决方案2】:

    你可以试试这个。 公共类 MainActivity 扩展 Activity { 线性布局 L1; 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);
     }
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多