@Override public int insertImageFromFile(String filename) { final BaseViewAdapter adapter = internalAdapter(); if (adapter == null || filename == null) return 0; String name = filename.substring(filename.lastIndexOf('/') + 1).toLowerCase(Locale.US); Drawable d; if (name.endsWith(".svg")) { d = mView.getImageCache().addSVGFile(filename, name); } else { d = mView.getImageCache().addBitmapFile(getContext().getResources(), filename, name); } if (d != null) { final String destPath = adapter.getRecordPath(); if (destPath != null) { ImageCache.copyFileTo(filename, destPath); } synchronized (mView.coreView()) { int w = ImageCache.getWidth(d); int h = ImageCache.getHeight(d); return mView.coreView().addImageShape(name, w, h); } } return 0; }
private Bitmap extentSnapshot(int doc, int gs, int spaceAround, boolean transparent) { final LogHelper log = new LogHelper(); final Rect extent = getDisplayExtent(); if (!extent.isEmpty()) { extent.inset(-spaceAround, -spaceAround); extent.intersect(0, 0, mView.getView().getWidth(), mView.getView().getHeight()); } if (extent.isEmpty()) { return null; } final Bitmap viewBitmap = mView.snapshot(doc, gs, transparent); if (viewBitmap == null) { return null; } if (extent.width() == mView.getView().getWidth() && extent.height() == mView.getView().getHeight()) { log.r(viewBitmap.getByteCount()); return viewBitmap; } final Bitmap realBitmap = Bitmap.createBitmap(viewBitmap, extent.left, extent.top, extent.width(), extent.height()); viewBitmap.recycle(); log.r(realBitmap.getByteCount()); return realBitmap; }
@Override public void onDestroy() { final Context context = getContext(); for (BaseGraphView view : ViewUtil.views()) { if (view.getView().getContext() == context) { view.stop(); } } }
private boolean saveToFile(String vgfile, int doc) { final LogHelper log = new LogHelper(); vgfile = addExtension(vgfile, ".vg"); if (mView.coreView().getShapeCount(doc) == 0) { final File f = new File(vgfile); return log.r(!f.exists() || f.delete()); } else { return log.r(createDirectory(vgfile, false) && mView.coreView().saveToFile(doc, vgfile)); } }
@Override public int insertBitmapFromResource(String name) { int id = ResourceUtil.getDrawableIDFromName(getContext(), name); name = ImageCache.BITMAP_PREFIX + name; final Drawable d = mView.getImageCache().addBitmap(getContext().getResources(), id, name); synchronized (mView.coreView()) { return d == null ? 0 : mView.coreView().addImageShape(name, ImageCache.getWidth(d), ImageCache.getHeight(d)); } }
@Override public String getContent() { final LogHelper log = new LogHelper(); int doc = acquireFrontDoc(); final StringCallback c = new StringCallback(); mView.coreView().getContent(doc, c); GiCoreView.releaseDoc(doc); mView.coreView().freeContent(); return log.r(c.toString()); }
@Override public void close() { if (mView != null) { mView.onPause(); mView.stop(); if (mView.getView() != null) { final ViewGroup parent = (ViewGroup) mView.getView().getParent(); if (parent != null) parent.removeAllViews(); } mView = null; } }
@Override public boolean onResume() { Log.d(TAG, "onResume"); final Context context = getContext(); boolean ret = false; for (BaseGraphView view : ViewUtil.views()) { if (view.getView().getContext() == context) { ret = view.onResume() || ret; } } return ret; }
@Override public int insertSVGFromResource(String name, int xc, int yc) { int id = ResourceUtil.getResIDFromName(getContext(), "raw", name); name = ImageCache.SVG_PREFIX + name; final Drawable d = mView.getImageCache().addSVG(getContext().getResources(), id, name); synchronized (mView.coreView()) { return d == null ? 0 : mView .coreView() .addImageShape(name, xc, yc, ImageCache.getWidth(d), ImageCache.getHeight(d)); } }
// ! 放大镜视图(mainView有效)或临时视图(mainView=nil)çš„æž„é€ å‡½æ•° public StdGraphView(Context context, BaseGraphView mainView) { super(context); mImageCache = mainView != null ? mainView.getImageCache() : new ImageCache(); createAdapter(context, null); mMainView = mainView; if (mainView != null) { mCoreView = GiCoreView.createMagnifierView(mViewAdapter, mainView.coreView(), mainView.viewAdapter()); } else { mCoreView = GiCoreView.createView(mViewAdapter, GiCoreView.kNoCmdType); } initView(context); }
@Override public boolean hasImageShape() { int doc = acquireFrontDoc(); boolean ret = mView.coreView().hasImageShape(doc); GiCoreView.releaseDoc(doc); return ret; }
@Override public int findShapeByImageID(String name) { int doc = acquireFrontDoc(); int ret = mView.coreView().findShapeByImageID(doc, name); GiCoreView.releaseDoc(doc); return ret; }
@Override public Bitmap extentSnapshot(int spaceAround, boolean transparent) { final GiCoreView v = mView.coreView(); int doc, gs; synchronized (v) { doc = v.acquireFrontDoc(); gs = v.acquireGraphics(mView.viewAdapter()); } try { return extentSnapshot(doc, gs, spaceAround, transparent); } finally { GiCoreView.releaseDoc(doc); v.releaseGraphics(gs); } }
@Override public Bitmap snapshot(boolean transparent) { final GiCoreView v = mView.coreView(); int doc, gs; synchronized (v) { doc = v.acquireFrontDoc(); gs = v.acquireGraphics(mView.viewAdapter()); } try { final LogHelper log = new LogHelper(); final Bitmap bitmap = mView.snapshot(doc, gs, transparent); log.r(bitmap != null ? bitmap.getByteCount() : 0); return bitmap; } finally { GiCoreView.releaseDoc(doc); v.releaseGraphics(gs); } }
@Override public boolean setCommand(String name, String params) { boolean ret = false; if (mView != null) { ret = mView.coreView().setCommand(name, params); Log.d(TAG, "setCommand " + name + params + ": " + ret); } return ret; }
@Override public Rect getBoundingBox() { final Floats box = new Floats(4); if (mView.coreView().getBoundingBox(box)) { return new Rect( Math.round(box.get(0)), Math.round(box.get(1)), Math.round(box.get(2)), Math.round(box.get(3))); } return new Rect(); }
@Override public boolean startPlay(String path) { final BaseViewAdapter adapter = internalAdapter(); if (adapter == null || adapter.getSavedState() != null || mView.coreView().isPlaying() || !new File(path).exists()) { return false; } return adapter.startPlay(path); }
@Override public Rect getDisplayExtent(int doc, int gs) { final Floats box = new Floats(4); if (mView.coreView().getDisplayExtent(doc, gs, box)) { return new Rect( Math.round(box.get(0)), Math.round(box.get(1)), Math.round(box.get(2)), Math.round(box.get(3))); } return new Rect(); }
@Override public boolean startRecord(String path) { final BaseViewAdapter adapter = internalAdapter(); if (adapter == null || adapter.getSavedState() != null || mView.coreView().isRecording() || !deleteDirectory(new File(path)) || !createDirectory(path, true)) { return false; } return adapter.startRecord(path); }
@Override public void onSaveInstanceState(Bundle outState, String path) { final BaseViewAdapter adapter = internalAdapter(); if (adapter == null) { Log.w(TAG, "onSaveInstanceState fail due to no view adapter"); } else { final LogHelper log = new LogHelper(); Bundle state = adapter.getSavedState(); if (state == null) { final String filename = new File(path, "resume.vg").getPath(); final String playFile = new File(path, "playresume.vg").getPath(); state = new Bundle(); adapter.onSaveInstanceState(state); if (mView.coreView().isPlaying()) { if (saveToFile(playFile)) { Log.d(TAG, "Auto save playing shapes to " + playFile); state.putString("playFile", playFile); } if (saveToFile(filename, mView.coreView().backDoc())) { Log.d(TAG, "Auto save to " + filename); state.putString("bakFile", filename); } } else { if (saveToFile(filename)) { Log.d(TAG, "Auto save to " + filename); state.putString("bakFile", filename); } } state.putBoolean("readOnly", cmdView().isReadOnly()); } outState.putBundle("vg", state); log.r(); } }
@Override public View getView() { return mView != null ? mView.getView() : null; }
@Override public int getDrawCount() { return mView.coreView().getDrawCount(); }
@Override public void setImagePath(String path) { mView.getImageCache().setImagePath(path); }
@Override public String getImagePath() { return mView.getImageCache().getImagePath(); }
private int acquireFrontDoc() { synchronized (mView.coreView()) { return mView.coreView().acquireFrontDoc(); } }
@Override public boolean setContent(String content) { final LogHelper log = new LogHelper(); return log.r(mView.coreView().setContent(content)); }
@Override public int cmdViewHandle() { final GiCoreView v = mView != null ? mView.coreView() : null; return v != null ? v.viewAdapterHandle() : 0; }
@Override public boolean loadFromFile(String vgfile, boolean readOnly) { final LogHelper log = new LogHelper(); vgfile = addExtension(vgfile, ".vg"); return log.r(mView != null && mView.coreView().loadFromFile(vgfile, readOnly)); }
@Override public void clearShapes() { mView.coreView().clear(); }
@Override public Context getContext() { return mView != null ? mView.getView().getContext() : null; }