中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

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

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

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

原始界面

圖1


截屏得到的圖片

圖1


代碼實現(xiàn)

1. 添加權(quán)限(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. 實現(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;
	}

}

標(biāo)簽: 代碼 權(quán)限

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:JS實現(xiàn)右下角的彈窗

下一篇: VC++中獲取DC的四種方法