[App Inventor] 如何匯入 .aix 擴充元件到 App Inventor 測試伺服器

本文將介紹如何匯入 .aix 擴充元件到 App Inventor 測試伺服器,請注意您需登入 App Inventor 測試伺服器而非 AI2 的正式版伺服器。

 
請按照以下步驟操作:
 
STEP1:請由此連結下載要匯入的 .aix 檔,這是 PuraVida 所寫的螢幕快照功能
 
STEP2:在畫面左側的 Extension 元件區中,點選 Import extension,會跳出以下視窗詢問 .aix 檔案路徑,確認之後請點選 Import 上傳檔案。
 
 
STEP3:命名元件,在此沒有改動
 
 
STEP4:匯入成功之後,就可以在 Extension 元件區看到這個元件了
 
 
STEP5:新增這個元件
 
 
STEP6:在 Blocks 頁面中也可以看到這個元件,相關的事件與方法當然都可以自由編寫 (Java)

STEP7:寫個小程式來看看吧


 
補充資料:本 .aix 檔的 Java source code,有興趣的朋友可以看看 event 與 method 是如何對應的喔
 

Java source code

package com.puravidaapps;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.os.Environment;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.*;

import java.io.File;
import java.io.FileOutputStream;

@DesignerComponent(version = TaifunScreenshot.VERSION,
    description = "Extension to take a screenshot.",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "https://puravidaapps.com/images/taifun16.png")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.WRITE_EXTERNAL_STORAGE")

public class TaifunScreenshot extends AndroidNonvisibleComponent implements Component {

  public static final int VERSION = 1;
  private ComponentContainer container;
  private Context context;
  private final Activity activity;
  private String fileName;
  private static final String LOG_TAG = "TaifunScreenshot";

  public TaifunScreenshot(ComponentContainer container) {
    super(container.$form());
    this.container = container;
    context = (Context) container.$context();
    activity = (Activity) container.$context();
    fileName = "screenshot.jpg";
    Log.d(LOG_TAG, "TaifunScreenshot Created" );
  }

  /**
   * Return fileName
   */
  @SimpleProperty(category = PropertyCategory.BEHAVIOR,
      description = "filename of the screenshot to be taken")
  public String FileName() {
    return fileName;
  }


  /**
   * Set fileName
   */
  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING,
      defaultValue = "screenshot.jpg")
  @SimpleProperty
  public void FileName(String fileName) {
    this.fileName = fileName;
  }


  /**
   * Take a screenshot
   */
  @SimpleFunction(description = "Take a screenshot.")
  public void TakeScreenshot() {
    // "http://stackoverflow.com/a/5651242/1545993
    try {
      // image naming and path  to include sd card  appending name you choose for file
      String mPath = Environment.getExternalStorageDirectory().toString() + File.separator + fileName;

      // create bitmap screen capture
      View v1 = activity.getWindow().getDecorView().getRootView();
      v1.setDrawingCacheEnabled(true);
      Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
      v1.setDrawingCacheEnabled(false);

      File imageFile = new java.io.File(mPath);

      FileOutputStream outputStream = new FileOutputStream(imageFile);
      int quality = 100;
      bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
      outputStream.flush();
      outputStream.close();

      Log.d(LOG_TAG, "screenshot taken: " + imageFile.toString());
      AfterScreenshot(imageFile.toString());
    } catch (Throwable e) {
      // Several error may come out with file handling or OOM
      Log.e(LOG_TAG, e.getMessage(), e);
      e.printStackTrace();
    }
  }

  /**
   * The AfterScreenshot event
   */
  @SimpleEvent(description = "Return image.")
  public void AfterScreenshot(String image){
    EventDispatcher.dispatchEvent(this, "AfterScreenshot", image);
  }

}


 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *