Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): Image loading, setters, example #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Disable drawing
* undo()/redo() [Android only]
Undo or redo last action

* fill(color) [Android only]
Fills the whole paint view with a solid color


### Properties

Expand Down
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 5.0.1
version: 5.1.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: Provides a paint surface user interface view.
Expand Down
6 changes: 3 additions & 3 deletions android/src/ti/modules/titanium/paint/PaintModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Kroll.module(name = "Paint", id = "ti.paint")
public class PaintModule extends KrollModule {
public PaintModule() {
super();
}
public PaintModule() {
super();
}
}
202 changes: 106 additions & 96 deletions android/src/ti/modules/titanium/paint/PaintViewProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

package ti.modules.titanium.paint;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.AsyncResult;
import org.appcelerator.kroll.common.TiMessenger;
Expand All @@ -15,102 +19,108 @@
import org.appcelerator.titanium.view.TiUIView;
import org.jetbrains.annotations.NotNull;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

@Kroll.proxy(creatableInModule = PaintModule.class)
public class PaintViewProxy extends TiViewProxy {
private UIPaintView paintView;

public PaintViewProxy() {
super();
}

@Override
public TiUIView createView(Activity activity) {
paintView = new UIPaintView(this);
return paintView;
}

@Kroll.setProperty
@Kroll.method
public void setStrokeWidth(Object width) {
paintView.setStrokeWidth(TiConvert.toFloat(width));
}

@Kroll.setProperty
@Kroll.method
public void setStrokeColor(String color) {
paintView.setStrokeColor(color);
}

@Kroll.setProperty
@Kroll.method
public void setEraseMode(Boolean toggle) {
paintView.setEraseMode(toggle);
}

@Kroll.setProperty
@Kroll.method
public void setStrokeAlpha(int alpha) {
paintView.setStrokeAlpha(alpha);
}

@Kroll.setProperty
@Kroll.method
public void setImage(String imagePath) {
paintView.setImage(imagePath);
}

@Kroll.method
public void lineTo(int x, int y) {
paintView.lineTo(x, y);
}

@Kroll.method
public void moveTo(int x, int y) {
paintView.moveTo(x, y);
}

@Kroll.method
public void undo() {
paintView.undo();
}

@Kroll.method
public void redo() {
paintView.redo();
}

@Kroll.method
public void enable(boolean enable) {
paintView.enable(enable);
}

@Kroll.method
public void clear() {
if (paintView != null) {
if (!TiApplication.isUIThread()) {
TiMessenger.sendBlockingMainMessage(handler.obtainMessage(MSG_CLEAR));
} else {
paintView.clear();
}
}
}

private static final int MSG_CLEAR = 60000;
private final Handler handler = new Handler(TiMessenger.getMainMessenger().getLooper(), new Handler.Callback() {
public boolean handleMessage(@NotNull Message msg) {
switch (msg.what) {
case MSG_CLEAR: {
AsyncResult result = (AsyncResult) msg.obj;
paintView.clear();
result.setResult(null);
return true;
}
}
return false;
}
});
private static final int MSG_CLEAR = 60000;
private static final int MSG_LOAD = 60001;
private UIPaintView paintView;
private final Handler handler = new Handler(TiMessenger.getMainMessenger().getLooper(), new Handler.Callback() {
public boolean handleMessage(@NotNull Message msg) {
switch (msg.what) {
case MSG_CLEAR: {
AsyncResult result = (AsyncResult) msg.obj;
paintView.clear();
result.setResult(null);
return true;
}
case MSG_LOAD: {
AsyncResult result = (AsyncResult) msg.obj;
paintView.setImage(result.getResult().toString());
result.setResult(null);
return true;
}
}
return false;
}
});

public PaintViewProxy() {
super();
}

@Override
public TiUIView createView(Activity activity) {
paintView = new UIPaintView(this);
return paintView;
}

@Kroll.setProperty
public void setStrokeWidth(Object width) {
paintView.setStrokeWidth(TiConvert.toFloat(width));
}

@Kroll.setProperty
public void setStrokeColor(String color) {
paintView.setStrokeColor(color);
}

@Kroll.setProperty
public void setEraseMode(Boolean toggle) {
paintView.setEraseMode(toggle);
}

@Kroll.setProperty
public void setStrokeAlpha(int alpha) {
paintView.setStrokeAlpha(alpha);
}

@Kroll.setProperty
public void setImage(String imagePath) {
if (!TiApplication.isUIThread()) {
TiMessenger.sendBlockingMainMessage(handler.obtainMessage(MSG_LOAD));
} else {
paintView.setImage(imagePath);
}
}

@Kroll.method
public void fill(String color) {
paintView.fill(TiConvert.toColor(color, TiApplication.getAppCurrentActivity()));
}

@Kroll.method
public void lineTo(int x, int y) {
paintView.lineTo(x, y);
}

@Kroll.method
public void moveTo(int x, int y) {
paintView.moveTo(x, y);
}

@Kroll.method
public void undo() {
paintView.undo();
}

@Kroll.method
public void redo() {
paintView.redo();
}

@Kroll.method
public void enable(boolean enable) {
paintView.enable(enable);
}

@Kroll.method
public void clear() {
if (paintView != null) {
if (!TiApplication.isUIThread()) {
TiMessenger.sendBlockingMainMessage(handler.obtainMessage(MSG_CLEAR));
} else {
paintView.clear();
}
}
}
}
93 changes: 47 additions & 46 deletions android/src/ti/modules/titanium/paint/PathPaint.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,55 @@
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiConvert;

public class PathPaint {

private Path myPath;
private Paint myPaint;
private Boolean isErease = false;

public void setPaint(Paint p) {
myPaint = p;
}

public Paint getPaint() {
if (isErease) {
myPaint.setColor(TiConvert.toColor("black"));
myPaint.setAlpha(0xFF);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
myPaint.setColor(Color.TRANSPARENT);
}
return myPaint;
}

public Path getPath() {
return myPath;
}

public void setPath(Path p) {
myPath = p;
}

public Boolean getEarase() {
return isErease;
}

public void setEarase(Boolean p) {
isErease = p;
}

public PathPaint() {
myPath = new Path();
myPaint = new Paint();

myPaint = new Paint();
myPaint.setAntiAlias(true);
myPaint.setDither(true);
myPaint.setColor(TiConvert.toColor("black"));
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeJoin(Paint.Join.ROUND);
myPaint.setStrokeCap(Paint.Cap.ROUND);
}
private Path myPath;
private Paint myPaint;
private Boolean isErease = false;

public PathPaint() {
myPath = new Path();
myPaint = new Paint();

myPaint = new Paint();
myPaint.setAntiAlias(true);
myPaint.setDither(true);
myPaint.setColor(TiConvert.toColor("black", TiApplication.getAppCurrentActivity()));
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeJoin(Paint.Join.ROUND);
myPaint.setStrokeCap(Paint.Cap.ROUND);
}

public Paint getPaint() {
if (isErease) {
myPaint.setColor(TiConvert.toColor("black", TiApplication.getAppCurrentActivity()));
myPaint.setAlpha(0xFF);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
myPaint.setColor(Color.TRANSPARENT);
}
return myPaint;
}

public void setPaint(Paint p) {
myPaint = p;
}

public Path getPath() {
return myPath;
}

public void setPath(Path p) {
myPath = p;
}

public Boolean getEarase() {
return isErease;
}

public void setEarase(Boolean p) {
isErease = p;
}
}
Loading
Loading