您的位置:首页 > 移动开发 > Android开发

手机截屏和图片分享

2017-05-23 09:48 246 查看
1.手机截屏的方法很多,这里我分享一种可以不仅仅截屏当前显示的,还可以把未显示的截取出来!

a.布局文件<?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">

<ScrollView
android:id="@+id/mScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
android:id="@+id/title_layout"
layout="@layout/title_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/selected_color"
android:paddingBottom="10dp"
>

<ImageView
android:id="@+id/evaluation_time_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:src="@drawable/evaluation_time"/>

<TextView
android:id="@+id/evaluation_time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/evaluation_time_icon"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/evaluation_time_icon"
android:text="用时 00:35:20"
android:textColor="@color/whitesmoke"/>

<com.ustcsoft.yzyx.zxjy.view.CircleProgressView
android:id="@+id/evaluation_score_percent"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/evaluation_time_tv"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"/>

</RelativeLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@color/lineColor"/>

<TextView
android:layout_width="match_parent"
android:layout_height="34dp"
android:layout_marginStart="15dp"
android:gravity="center_vertical"
android:text="基础分析"
android:textColor="@color/text_light_black"
android:textSize="@dimen/txt_size_24px"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lineColor"/>

<com.ustcsoft.yzyx.zxjy.view.HorizontalChartView
android:id="@+id/score_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lineColor"/>

<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="15dp"
android:gravity="center_vertical"
android:text="综合评价"/>

<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lineColor"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:text="此处写专家点评此处写专家点评此处写专此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评此处写专家点评家点评此处写专家点评"
android:textColor="@color/text_light_black"
android:textSize="@dimen/txt_size_24px"/>
</LinearLayout>
</ScrollView>

</LinearLayout>

2.代码截取

public static Bitmap shotScrollView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}

原理大家看下,就是画布覆盖。

2.分享图片

public void shareImage(Bitmap bitmap) {
try {
Uri uriToImage = Uri.parse(MediaStore.Images.Media.insertImage(
mContext.getContentResolver(), bitmap, null, null));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/*");
// 遍历所有支持发送图片的应用。找到需要的应用
startActivity(Intent.createChooser(shareIntent, "分享测评报告"));
} catch (Exception e) {
//            ContextUtil.getInstance().showToastMsg("分享图片到**失败");
}
}

就到这里,亲测可以,前辈的分享,在这里分享给大家。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息