今天做分享,需求是截圖分享,做了也是一個運動類的産品,那好,我們就直接開始做,考慮了一下,因為是全屏的分享,所有很自然而然的想到了View的getDrawingCache()方法來擷取Bitmap,看到網上有人說截取不了WebView上的圖檔,倒是沒有去嘗試,因為我們的應用不需要,不過有時間還是要去試試,占占坑,這篇部落格隻是記錄一下知識點,沒什麼技術含量
我們寫個小Sample就好了
activity_main.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
android:id="@+id/ivPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@+id/btnAllWindow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="截取全屏"/>
點選俺就截圖,然後傳回一個Bitmap,設定在這個imageview的控件上,拿我們寫一個截圖的方法
/** * 截取全屏 * * @return */ public Bitmap captureScreenWindow() { getWindow().getDecorView().setDrawingCacheEnabled(true); Bitmap bmp = getWindow().getDecorView().getDrawingCache(); return bmp; }
OK,那我們的點選事件
/** * 點選事件 * * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnAllWindow: Bitmap bitmap = captureScreenWindow(); ivPlay.setImageBitmap(bitmap); break; } }
我們實際來看下效果
Ok,但是這樣并沒有儲存在記憶體卡上,我們需要儲存一下,做圖檔上傳的功能對吧,所有,我們再來寫一個儲存圖檔的方法
/** * 儲存到記憶體卡 * * @param bitName * @param mBitmap */ public void saveBitmapForSdCard(String bitName, Bitmap mBitmap) { //建立file對象 File f = new File("/sdcard/" + bitName + ".png"); try { //建立 f.createNewFile(); } catch (IOException e) { } FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } //原封不動的儲存在記憶體卡上 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } }
把我們的bitmap通過流儲存,同時擷取本地的時間命名,我們的點選事件就是這樣:
/** * 點選事件 * * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnAllWindow: Bitmap bitmap = captureScreenWindow(); ivPlay.setImageBitmap(bitmap); long time = System.currentTimeMillis(); saveBitmapForSdCard("img" + time, bitmap); break; } }
最後的結果
OK,這隻是一個很簡單的截圖功能,當然,還有很多其他的方法,大家自己可以去研究一下,全部代碼
MainActivity
package com.liuguilin.screenshotssample;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//截取全屏
private Button btnAllWindow;
private ImageView ivPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btnAllWindow = (Button) findViewById(R.id.btnAllWindow);
btnAllWindow.setOnClickListener(this);
ivPlay = (ImageView) findViewById(R.id.ivPlay);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAllWindow:
Bitmap bitmap = captureScreenWindow();
ivPlay.setImageBitmap(bitmap);
long time = System.currentTimeMillis();
saveBitmapForSdCard("img" + time, bitmap);
break;
}
}
public Bitmap captureScreenWindow() {
getWindow().getDecorView().setDrawingCacheEnabled(true);
Bitmap bmp = getWindow().getDecorView().getDrawingCache();
return bmp;
}
public void saveBitmapForSdCard(String bitName, Bitmap mBitmap) {
//建立file對象
File f = new File("/sdcard/" + bitName + ".png");
try {
//建立
f.createNewFile();
} catch (IOException e) {
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//原封不動的儲存在記憶體卡上
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
要注意添加一下權限哦
貌似代碼就這麼點,Demo也可以不用上傳了,想一起學習Android的可以加群:555974449
什麼?小夥伴你需要?那我上傳好了:http://download.csdn.net/detail/qq_26787115/9637229