2022年4月26日 星期二

App inventor:aix程式碼練習2

此為取得快照的aix原始檔,原始連結。

https://community.appinventor.mit.edu/t/f-os-simple-screenshot-extension/54125

以下僅加上註解 ,便於了解 :

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import android.view.View;
import android.graphics.Bitmap;
import java.io.FileOutputStream;
import android.graphics.Bitmap.CompressFormat;
import android.content.pm.PackageManager;
import android.os.Environment;
import java.io.File;
import java.lang.Enum;
@DesignerComponent(
version = 2,
description = "A non-visible extension to take screenshots.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16")
//簡單物件
@SimpleObject(external = true)
//Libraries
//使用者庫
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
//Ta要你建立的package英文相同
public class Ta extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
//此class的主要建構式,在此將外在ui的元件和這個function的變數綁定,Ta要與類別名相同
public Ta(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
//取得快照的主要程式碼
@SimpleFunction(description = "Takes a screenshot of the current app, with the given quality, compressFormat and saves the screenshot as the given path.")
public void TakeScreenshot(int quality, String compressFormat, String path) {
try {
//設定mPath為你設定的路徑
String mPath = path;
//預設壓縮成jpg
CompressFormat cp = CompressFormat.JPEG;
//假如compressFormat的值是png,就壓成png格式
if (compressFormat == "PNG") {
//假如compressFormat的值是png,就壓成WEBP格式
cp = CompressFormat.PNG;
} else if (compressFormat == "WEBP") {
cp = CompressFormat.WEBP;
}
//設定vi為activity.getWindow().getDecorView().getRootView(),反正就是取得視圖
View v1 = activity.getWindow().getDecorView().getRootView();
//在使用getDrawingCache前要先setDrawingCacheEnabled(true)
v1.setDrawingCacheEnabled(true);
//透過Bitmap.createBitmap這個方法將圖片顯示出來
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());

//用完getDrawingCache,將setDrawingCacheEnabled設為false
v1.setDrawingCacheEnabled(false);
//設定檔案要儲存的路徑
File imageFile = new File(mPath);
//設定要儲存的路徑
FileOutputStream outputStream = new FileOutputStream(imageFile);
//將bitmap開始壓縮到某個位置存起來,參數1是壓縮的形式,參數2是品質,第三個是儲存的路徑
bitmap.compress(cp, quality, new FileOutputStream(path));
//執行SimpleEvent裡的EventDispatcher.dispatchEvent(this, "SavedScreenshot", path);
SavedScreenshot(imageFile.toString());
//釋放資源
outputStream.flush();
outputStream.close();
}
catch(Throwable e) {
//失敗了會去執行SimpleEvent裡的EventDispatcher.dispatchEvent(this, "Failed", error);
Failed(e.getMessage());
}
}
//設定屬性,回傳JPEG
@SimpleProperty(description = "A compress format block.")
public String Jpeg() {
return "JPEG";
}
//設定屬性,綠色積木,回傳PNG
@SimpleProperty(description = "A compress format block.")
public String Png() {
return "PNG";
}
//設定屬性,回傳Webp,WebP(發音:weppy)是一種同時提供了有損壓縮與無失真壓縮(可逆壓縮)的圖片檔案格式。
@SimpleProperty(description = "A compress format block. This block will only work for devices smaller than Android 10.")
public String Webp() {
return "WEBP";
}

//失敗事件反應。Failed
@SimpleEvent(description = "This event is fired when the extension has failed to take the screenshot.")
public void Failed (String error) {
EventDispatcher.dispatchEvent(this, "Failed", error);
}
//成功事件反應
@SimpleEvent(description = "This event is fired when the extension has taken the screenshot in the given path.")
public void SavedScreenshot (String path) {
EventDispatcher.dispatchEvent(this, "SavedScreenshot", path);
}
//Function反應
@SimpleFunction(description = "Returns whether the application has the write permission. This is mandatory if you use this extension.")
public boolean IsWritePermissionGranted() {
//android 9.0之後,要寫入資料夾,要先獲得寫入的權限才能寫入
String permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
int res = context.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
//此Function目的為取得檔案要儲存的資料夾
@SimpleFunction(description = "Returns an absolute path of the application specific directory of this app.")
public String ApplicationSpecificDirectory() {
String pkgName = context.getPackageName();
//回傳你要儲存檔案的路徑
return "/storage/emulated/0/Android/data/" + pkgName + "/files/";
}
}

沒有留言:

張貼留言