如何以编程方式捕捉手机屏幕的android [英] how to capture the phone screen programmatically in android
本文介绍了如何以编程方式捕捉手机屏幕的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何以编程方式获取手机屏幕。我只是一派,才知道,我必须使用view.getDrawingCache()。但getDrawingCache()始终返回null。
I want to know how to capture phone screen programmatically. I just googled and got to know that I have to use view.getDrawingCache(). But getDrawingCache() always returns null.
我的XML布局
<?xml version="1.0" encoding="utf-8"?>
<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/screenlayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/screen"
/>
</LinearLayout>
Java文件中::
java file::
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View vScreen = findViewById(R.id.screenlayout);
ImageView imageview = (ImageView)findViewById(R.id.screen);
Bitmap bitmap;
View v1 = vScreen.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache()); //getting null here
v1.setDrawingCacheEnabled(false)
imageview.setImageBitmap(bitmap);
}
可有人请纠正我,我要去的地方错了?
Can someone please correct me where I am going wrong?
推荐答案
使用下面的code,以避免空的位图:
Use the following code to avoid a null bitmap:
public class AndroidWebImage extends Activity {
ImageView bmImage;
LinearLayout view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (LinearLayout)findViewById(R.id.screen);
bmImage = (ImageView)findViewById(R.id.image);
view.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
bmImage.setImageBitmap(b);
};
}
好好看看,你必须使用:
Take a good look, you have to use:
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
我用下面的的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
的结果是:
<一href="http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null">Reference
The outcome is:
Reference
这篇关于如何以编程方式捕捉手机屏幕的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文