欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android截屏功能實(shí)現(xiàn)代碼

 更新時(shí)間:2021年07月11日 09:53:15   作者:fmp8  
這篇文章主要為大家詳細(xì)介紹了android截屏功能的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

android開發(fā)中通過View的getDrawingCache方法可以達(dá)到截屏的目的,只是缺少狀態(tài)欄!

原始界面

截屏得到的圖片

代碼實(shí)現(xiàn)

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

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

2. 添加1個(gè)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. 實(shí)現(xiàn)截屏(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 {
     // 獲取內(nèi)置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;
 }

}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在Android系統(tǒng)中使用gzip進(jìn)行數(shù)據(jù)傳遞實(shí)例代碼

    在Android系統(tǒng)中使用gzip進(jìn)行數(shù)據(jù)傳遞實(shí)例代碼

    HTTP協(xié)議上的GZIP編碼是一種用來改進(jìn)WEB應(yīng)用程序性能的技術(shù),4.4MB的文本數(shù)據(jù)經(jīng)過Gzip傳輸?shù)娇蛻舳酥笞優(yōu)?92KB,壓縮效率極高,下面與大家分享下具體的實(shí)現(xiàn)
    2013-06-06
  • Android Scroller的使用方法

    Android Scroller的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android Scroller的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 最新評(píng)論