android 屏幕 view,Android屏幕及view的截图实例详解

Android屏幕及view的截图实例详解

发布时间:2020-09-18 19:26:14

来源:脚本之家

阅读:102

作者:笨鸟-先飞

Android屏幕及view的截图实例详解

屏幕可见区域的截图

整个屏幕截图的话可以用View view = getWindow().getDecorView();

public static Bitmap getNormalViewScreenshot(View view) {

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

return view.getDrawingCache();

}

scrollview的整体截屏

public static Bitmap getWholeScrollViewToBitmap(View view) {

view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

view.buildDrawingCache();

Bitmap bitmap = view.getDrawingCache();

return bitmap;

}

webview的整体截图

public static Bitmap getWholeWebViewToBitmap(WebView webView) {

Picture snapShot = webView.capturePicture();

Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bmp);

snapShot.draw(canvas);

return bmp;

}

listview的整体截图

public static Bitmap getWholeListViewItemsToBitmap(ListView listview) {

ListAdapter adapter = listview.getAdapter();

int itemscount = adapter.getCount();

int allitemsheight = 0;

List bmps = new ArrayList();

for (int i = 0; i < itemscount; i++) {

View childView = adapter.getView(i, null, listview);

childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY),

MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());

childView.setDrawingCacheEnabled(true);

childView.buildDrawingCache();

bmps.add(childView.getDrawingCache());

allitemsheight += childView.getMeasuredHeight();

}

Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);

Canvas bigcanvas = new Canvas(bigbitmap);

Paint paint = new Paint();

int iHeight = 0;

for (int i = 0; i < bmps.size(); i++) {

Bitmap bmp = bmps.get(i);

bigcanvas.drawBitmap(bmp, 0, iHeight, paint);

iHeight += bmp.getHeight();

bmp.recycle();

bmp = null;

}

return bigbitmap;

}

需要多次截图的话,需要用到 view.destroyDrawingCache();

Bitmap normalViewScreenshot = ScreenShotUtils.getNormalViewScreenshot(mFrameContent);

if (normalViewScreenshot != null) {

Bitmap b = Bitmap.createBitmap(normalViewScreenshot);

mImageResult.setImageBitmap(b);

mFrameContent.destroyDrawingCache();

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,Selector是一个用于定义不同状态下View的样式和背景的XML文件。它可以根据View的不同状态(如按下、选中、禁用等)来设置不同的样式和背景。下面是Selector的用法详解实例。 1. 创建selector xml文件 在res/drawable目录下创建一个XML文件,文件名以selector_开头,如selector_button.xml,然后在文件中定义不同状态下View的样式和背景。例如: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <!-- 选中状态 --> <item android:drawable="@drawable/button_selected" android:state_selected="true" /> <!-- 默认状态 --> <item android:drawable="@drawable/button_normal" /> </selector> ``` 2. 在View中应用selector 在布局文件中使用android:background属性来应用selector,如: ```xml <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:background="@drawable/selector_button" /> ``` 这样,当Button的状态改变时,它的背景就会自动变化。 除了android:background属性外,还可以在TextView、EditText等View中使用android:textColor属性来应用selector,如: ```xml <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="@drawable/selector_text_color" /> ``` 其中,selector_text_color.xml文件的内容如下: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:color="@color/colorAccent" android:state_pressed="true" /> <!-- 选中状态 --> <item android:color="@color/colorPrimary" android:state_selected="true" /> <!-- 默认状态 --> <item android:color="@android:color/black" /> </selector> ``` 这样,当TextView的状态改变时,它的文字颜色就会自动变化。 以上就是Selector在Android中的用法详解实例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值