android如何使用代码截屏,android实现截屏功能代码
搜索熱詞
下面是編程之家 jb51.cc 通過網絡收集整理的代碼片段。
編程之家小編現在分享給大家,也給大家做個參考。
2. 添加1個Button(activity_main.xml文件)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screenshot"
/>
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;
}
}
以上是編程之家(jb51.cc)為你收集整理的全部代碼內容,希望文章能夠幫你解決所遇到的程序開發問題。
如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。
總結
以上是編程之家為你收集整理的android實現截屏功能代碼全部內容,希望文章能夠幫你解決android實現截屏功能代碼所遇到的程序開發問題。
如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。
本圖文內容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
總結
以上是生活随笔為你收集整理的android如何使用代码截屏,android实现截屏功能代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单实用的铁道部12306.cn网站自动
- 下一篇: Hamcrest断言