手機截圖和圖片分享
阿新 • • 發佈:2019-01-04
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("分享圖片到**失敗"); } }