国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android截屏功能實現代碼

2019-12-12 04:45:29
字體:
來源:轉載
供稿:網友

android開發中通過View的getDrawingCache方法可以達到截屏的目的,只是缺少狀態欄!

原始界面

截屏得到的圖片

代碼實現

1. 添加權限(AndroidManifest.xml文件里)

復制代碼 代碼如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2. 添加1個Button(activity_main.xml文件)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context=".MainActivity" >  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerHorizontal="true"    android:layout_centerVertical="true"    android:text="@string/hello_world" />    <Button     android:id="@+id/btn_save"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Screenshot"    /></RelativeLayout>

3. 實現截屏(MainActivity.java文件)

package com.example.androidtest;import java.io.File;import java.io.FileOutputStream;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.graphics.Bitmap;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);  Button btn = (Button) this.findViewById(R.id.btn_save); btn.setOnClickListener(new View.OnClickListener() {    @Override  public void onClick(View v) {  screenshot();  } }); }  private void screenshot() { // 獲取屏幕 View dView = getWindow().getDecorView();  dView.setDrawingCacheEnabled(true);   dView.buildDrawingCache();      Bitmap bmp = dView.getDrawingCache();    if (bmp != null)    {     try {     // 獲取內置SD卡路徑     String sdCardPath = Environment.getExternalStorageDirectory().getPath();     // 圖片文件路徑     String filePath = sdCardPath + File.separator + "screenshot.png";          File file = new File(filePath);     FileOutputStream os = new FileOutputStream(file);     bmp.compress(Bitmap.CompressFormat.PNG, 100, os);     os.flush();     os.close();  } catch (Exception e) {  }    } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荔浦县| 莒南县| 昌都县| 渝中区| 蓬溪县| 安远县| 衡南县| 张家港市| 宣武区| 大兴区| 天等县| 依兰县| 佛坪县| 句容市| 云浮市| 宁陕县| 满城县| 淳安县| 界首市| 龙岩市| 郓城县| 尼木县| 乌鲁木齐市| 石楼县| 黄山市| 无锡市| 梓潼县| 喜德县| 舟山市| 太谷县| 双流县| 昌图县| 高邑县| 汤阴县| 大竹县| 万全县| 临沧市| 宁夏| 始兴县| 凤翔县| 定边县|