Android截圖、儲存、分享
阿新 • • 發佈:2019-01-26
原理:將擷取到的Bitmap賦給Dialog上的ImageView,並對Dialog加了彈出和收起的動畫,實現截圖效果。
首先建立一個layout名為show_cut_screen_layout用於彈出截圖對話方塊,上面是一個image,下面是橫向線性佈局的兩個button。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/btn_layout" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/share_img" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/viewfinder_mask" android:text="分享" /> <Button android:id="@+id/share_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/colorAccent" android:text="取消" /> </LinearLayout> <ImageView android:id="@+id/show_cut_screen_img" android:layout_above="@id/btn_layout" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
然後設定對話方塊彈出的style:
進入values——style.xml裡面新增如下程式碼:
<!--彈出對話方塊動畫--> <style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1"> <item name="android:windowEnterAnimation">@anim/popview_in_amin</item> <item name="android:windowExitAnimation">@anim/popview_out_amin</item> </style>
接下來在res資料夾下新建一個anim資料夾用於儲存對話方塊彈出和收起動畫:
在裡面新建popview_in_amin.xml :
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="200"/> </set>
和popview_out_amin.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="0%"
android:pivotY="100%"
android:fillAfter="false"
android:duration="200"/>
</set>
接下來是java程式碼:
//截圖功能
private void popShotSrceenDialog(){
final AlertDialog cutDialog = new AlertDialog.Builder(this).create();
View dialogView = View.inflate(this, R.layout.show_cut_screen_layout, null);
ImageView showImg = (ImageView) dialogView.findViewById(R.id.show_cut_screen_img);
dialogView.findViewById(R.id.share_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cutDialog.dismiss();
}
});
dialogView.findViewById(R.id.share_img).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//分享
Uri pa=Uri.fromFile(new File(filePath));//根據路徑轉化為uri
Intent imageIntent = new Intent(Intent.ACTION_SEND);//呼叫系統的ACTION_SEND
imageIntent.setType("image/png");
imageIntent.putExtra(Intent.EXTRA_STREAM, pa);//EXTRA_STREAM對應轉化為uri的path
startActivity(Intent.createChooser(imageIntent, "分享"));
}
});
//獲取當前螢幕的大小
int width = getWindow().getDecorView().getRootView().getWidth();
int height = getWindow().getDecorView().getRootView().getHeight();
//生成相同大小的圖片
Bitmap temBitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
//找到當前頁面的跟佈局
View view = getWindow().getDecorView().getRootView();
//設定快取
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
//從快取中獲取當前螢幕的圖片
temBitmap = view.getDrawingCache();
//儲存圖片
if (temBitmap != null)
{
try {
// 獲取內建SD卡路徑
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 圖片檔案路徑,獲取系統時間
long time=System.currentTimeMillis();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
java.util.Date date=new java.util.Date(time);
String str=sdf.format(date);
filePath = sdCardPath + File.separator +str+"screenshot.png";
File file = new File(filePath);
FileOutputStream os = new FileOutputStream(file);
temBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Toast.makeText(be_qrcode.this,"儲存失敗,請檢查許可權或清理記憶體",Toast.LENGTH_SHORT).show();
}
}
showImg.setImageBitmap(temBitmap);
cutDialog.setView(dialogView);
Window window = cutDialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager m = window.getWindowManager();
Display d = m.getDefaultDisplay(); // 獲取螢幕寬、高用
WindowManager.LayoutParams p = window.getAttributes(); // 獲取對話方塊當前的引數值
p.height = (int) (d.getHeight() * 0.8); // 高度設定為螢幕的0.6
p.gravity = Gravity.CENTER;//設定彈出框位置
window.setAttributes(p);
window.setWindowAnimations(R.style.dialogWindowAnim);
cutDialog.show();
}