Skip to content

Commit

Permalink
custom scribble activity, use alpha_8 instead of argb_8888
Browse files Browse the repository at this point in the history
  • Loading branch information
GilgameshxZero committed Aug 11, 2024
1 parent 10258c6 commit 1353bdc
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 138 deletions.
31 changes: 2 additions & 29 deletions res/layout/activity_scribble.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<ImageView android:id="@+id/activity_scribble_image_view"
<com.gilgamesh.xena.scribble.ScribbleView
android:id="@+id/activity_scribble_scribble_view"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<!-- <Button android:id="@+id/activity_scribble_button_up"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/button"
android:onClick="onClick" /> -->
<!-- <Button android:id="@+id/activity_scribble_button_right"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/button"
android:onClick="onClick" /> -->
<!-- <Button android:id="@+id/activity_scribble_button_down"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/button"
android:onClick="onClick" /> -->
<!-- <Button android:id="@+id/activity_scribble_button_left"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/button"
android:onClick="onClick" /> -->
</RelativeLayout>
6 changes: 1 addition & 5 deletions res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
<item name="activity_file_picker_button_load_svg" type="id" />
<item name="activity_file_picker_button_load_pdf" type="id" />

<item name="activity_scribble_image_view" type="id" />
<item name="activity_scribble_button_up" type="id" />
<item name="activity_scribble_button_right" type="id" />
<item name="activity_scribble_button_down" type="id" />
<item name="activity_scribble_button_left" type="id" />
<item name="activity_scribble_scribble_view" type="id" />
</resources>
22 changes: 19 additions & 3 deletions src/com/gilgamesh/xena/pdf/PdfReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.graphics.RectF;
import android.graphics.pdf.PdfRenderer;
import android.graphics.pdf.PdfRenderer.Page;
import android.graphics.Color;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
Expand Down Expand Up @@ -70,12 +71,27 @@ private PageBitmap getBitmapForPage(int pageIdx) {
+ page.getWidth()
+ "x"
+ page.getHeight() + ".");
this.pages[pageIdx].bitmap = Bitmap.createBitmap(
Bitmap bitmap = Bitmap.createBitmap(
Math.round(page.getWidth() * this.pointScale.x),
Math.round(page.getHeight() * this.pointScale.y),
Bitmap.Config.ARGB_8888);
page.render(this.pages[pageIdx].bitmap, null, null,
Page.RENDER_MODE_FOR_DISPLAY);
page.render(bitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY);
int cPixels = bitmap.getWidth() * bitmap.getHeight();
int pixels[] = new int[cPixels];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
bitmap.getHeight());
this.pages[pageIdx].bitmap = Bitmap.createBitmap(
Math.round(page.getWidth() * this.pointScale.x),
Math.round(page.getHeight() * this.pointScale.y),
Bitmap.Config.ALPHA_8);
for (int i = 0; i < cPixels; i++) {
Color color = Color.valueOf(pixels[i]);
pixels[i] = (int) ((1 - (color.red() + color.green() + color.blue())
/ 3)
* color.alpha() * 255) << 24;
}
this.pages[pageIdx].bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0,
bitmap.getWidth(), bitmap.getHeight());
page.close();
renderer.close();
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/gilgamesh/xena/scribble/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Chunk(PathManager pathManager, int width, int height, int offsetX,
this.pathManager = pathManager;
this.OFFSET_X = offsetX;
this.OFFSET_Y = offsetY;
this.bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
this.bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
this.canvas = new Canvas(this.bitmap);
this.canvas.drawRect(0, 0, width, height, Chunk.PAINT_ERASE);
}
Expand Down
158 changes: 58 additions & 100 deletions src/com/gilgamesh/xena/scribble/ScribbleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class ScribbleActivity extends Activity
implements View.OnClickListener {
Expand All @@ -59,15 +58,13 @@ public class ScribbleActivity extends Activity

static public final String EXTRA_PDF_URI = "EXTRA_PDF_URI";

private final int FOREVER_MS = 1000000000;

private SvgFileScribe svgFileScribe = new SvgFileScribe();
private PathManager pathManager;
private PdfReader pdfReader;

private ImageView imageView;
private Bitmap imageViewBitmap;
private Canvas imageViewCanvas;
private ScribbleView scribbleView;
private Bitmap scribbleViewBitmap;
private Canvas scribbleViewCanvas;
private TouchHelper touchHelper;
private Uri svgUri;
// pdfUri is null if no PDF is loaded.
Expand Down Expand Up @@ -249,35 +246,35 @@ public void onEndRawDrawing(boolean b, TouchPoint touchPoint) {

@Override
public void onRawDrawingTouchPointMoveReceived(TouchPoint touchPoint) {
// Log.v(XenaApplication.TAG,
// "ScribbleActivity::onRawDrawingTouchPointMoveReceived");

if (!isDrawing) {
return;
}

PointF lastPoint = this.currentPath.points
.get(this.currentPath.points.size() - 1);
PointF lastPoint = currentPath.points
.get(currentPath.points.size() - 1);
PointF newPoint = new PointF(
touchPoint.x - pathManager.getViewportOffset().x,
touchPoint.y - pathManager.getViewportOffset().y);
if (this.currentPath.points.size() > 1

if (currentPath.points.size() > 1
&& Geometry.distance(lastPoint, newPoint) < DRAW_MOVE_EPSILON) {
return;
}
if (imageView.isDirty()) {
// Log.v(XenaApplication.TAG,
// "ScribbleActivity::onRawDrawingTouchPointMoveReceived: ImageView is
// dirty, skipping.");
currentPath.addPoint(newPoint);

// Log.v(XenaApplication.TAG,
// "ScribbleActivity::onRawDrawingTouchPointMoveReceived "
// + scribbleView.isDrawing());

if (scribbleView.isDrawing()) {
// Log.v(XenaApplication.TAG, "Dirty ScribbleView.");
} else {
imageViewCanvas.drawLine(previousTentativeDrawPoint.x,
scribbleViewCanvas.drawLine(previousTentativeDrawPoint.x,
previousTentativeDrawPoint.y, touchPoint.x, touchPoint.y,
PAINT_TENTATIVE_LINE);
imageView.postInvalidate();
scribbleView.postInvalidate();
previousTentativeDrawPoint.set(touchPoint.x, touchPoint.y);
}

this.currentPath.addPoint(newPoint);
}

@Override
Expand Down Expand Up @@ -368,12 +365,12 @@ public void onPenUpRefresh(RectF refreshRect) {
}
};

private View.OnTouchListener imageViewOnTouchListener = new View.OnTouchListener() {
private View.OnTouchListener scribbleViewOnTouchListener = new View.OnTouchListener() {
private final int FLICK_LOWER_BOUND_MS = 80;
private final int FLICK_UPPER_BOUND_MS = 220;

private PointF previousPoint = new PointF();
private long actionDownTime = 0;
private long actionDownTimeMs = 0;

@Override
public boolean onTouch(View v, MotionEvent event) {
Expand All @@ -385,26 +382,24 @@ public boolean onTouch(View v, MotionEvent event) {
}

PointF touchPoint = new PointF(event.getX(), event.getY());
long eventDurationMs = (System.nanoTime() - this.actionDownTime)
/ 1000000;
long eventDurationMs = (System.currentTimeMillis()
- this.actionDownTimeMs);

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Sometimes, this fires slightly before a draw/erase event. The
// draw/erase event will cancel panning in that case.

if (isPanning) {
Log.v(XenaApplication.TAG,
"ScribbleActivity::onTouch:RESET "
+ pathManager.getViewportOffset());
Log.v(XenaApplication.TAG, "ScribbleActivity::onTouch:RESET "
+ pathManager.getViewportOffset());
} else {
Log.v(XenaApplication.TAG,
"ScribbleActivity::onTouch:DOWN "
+ pathManager.getViewportOffset());
Log.v(XenaApplication.TAG, "ScribbleActivity::onTouch:DOWN "
+ pathManager.getViewportOffset());

isPanning = true;

this.actionDownTime = System.nanoTime();
this.actionDownTimeMs = System.currentTimeMillis();
}

panBeginOffset = pathManager.getViewportOffset();
Expand All @@ -430,8 +425,8 @@ public boolean onTouch(View v, MotionEvent event) {
this.previousPoint.x = touchPoint.x;
this.previousPoint.y = touchPoint.y;

Log.v(XenaApplication.TAG, "ScribbleActivity::onTouch:MOVE "
+ pathManager.getViewportOffset());
// Log.v(XenaApplication.TAG, "ScribbleActivity::onTouch:MOVE "
// + pathManager.getViewportOffset());

drawBitmapToView(false);

Expand All @@ -453,15 +448,11 @@ public boolean onTouch(View v, MotionEvent event) {

// Determine flick direction.
int direction = panBeginOffset.y < pathManager.getViewportOffset().y
+ touchPoint.y
- this.previousPoint.y
? 1
: -1;

pathManager.setViewportOffset(
new PointF(panBeginOffset.x,
panBeginOffset.y
+ direction * imageView.getHeight() * 0.9f));
+ touchPoint.y - this.previousPoint.y ? 1 : -1;

pathManager
.setViewportOffset(new PointF(panBeginOffset.x, panBeginOffset.y
+ direction * scribbleView.getHeight() * 0.9f));
} else {
Log.v(XenaApplication.TAG, "ScribbleActivity::onTouch:UP");
}
Expand All @@ -482,16 +473,16 @@ protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.activity_scribble);
this.parseUri();

this.imageView = findViewById(R.id.activity_scribble_image_view);
this.imageView.post(new Runnable() {
this.scribbleView = findViewById(R.id.activity_scribble_scribble_view);
this.scribbleView.post(new Runnable() {
@Override
public void run() {
initDrawing();
}
});
this.imageView.setOnTouchListener(imageViewOnTouchListener);
this.scribbleView.setOnTouchListener(scribbleViewOnTouchListener);

this.touchHelper = TouchHelper.create(imageView, rawInputCallback)
this.touchHelper = TouchHelper.create(scribbleView, rawInputCallback)
.setStrokeWidth(Chunk.STROKE_WIDTH)
.setStrokeStyle(TouchHelper.STROKE_STYLE_PENCIL);
}
Expand Down Expand Up @@ -519,42 +510,6 @@ protected void onDestroy() {

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.activity_scribble_button_down:
this.pathManager.setViewportOffset(
new PointF(this.pathManager.getViewportOffset().x,
this.pathManager.getViewportOffset().y
- this.imageView.getHeight() * 0.9f));
this.svgFileScribe.debounceSave(this, this.svgUri, this.pathManager);
this.drawBitmapToView(true);
break;
case R.id.activity_scribble_button_right:
this.pathManager.setViewportOffset(
new PointF(
this.pathManager.getViewportOffset().x
- this.imageView.getWidth() * 0.5f,
this.pathManager.getViewportOffset().y));
this.svgFileScribe.debounceSave(this, this.svgUri, this.pathManager);
this.drawBitmapToView(true);
break;
case R.id.activity_scribble_button_up:
this.pathManager.setViewportOffset(
new PointF(this.pathManager.getViewportOffset().x,
this.pathManager.getViewportOffset().y
+ this.imageView.getHeight() * 0.9f));
this.svgFileScribe.debounceSave(this, this.svgUri, this.pathManager);
this.drawBitmapToView(true);
break;
case R.id.activity_scribble_button_left:
this.pathManager.setViewportOffset(
new PointF(
this.pathManager.getViewportOffset().x
+ this.imageView.getWidth() * 0.5f,
this.pathManager.getViewportOffset().y));
this.svgFileScribe.debounceSave(this, this.svgUri, this.pathManager);
this.drawBitmapToView(true);
break;
}
}

private void parseUri() {
Expand All @@ -577,53 +532,56 @@ private void parseUri() {

private void initDrawing() {
this.pathManager = new PathManager(
new Point(this.imageView.getWidth(), this.imageView.getHeight()));
new Point(this.scribbleView.getWidth(), this.scribbleView.getHeight()));
SvgFileScribe.loadPathsFromSvg(ScribbleActivity.this, this.svgUri,
this.pathManager);

this.imageViewBitmap = Bitmap.createBitmap(this.imageView.getWidth(),
this.imageView.getHeight(),
Bitmap.Config.ARGB_8888);
this.imageViewCanvas = new Canvas(this.imageViewBitmap);
this.imageView.setImageBitmap(this.imageViewBitmap);
this.scribbleViewBitmap = Bitmap.createBitmap(this.scribbleView.getWidth(),
this.scribbleView.getHeight(),
Bitmap.Config.ALPHA_8);
this.scribbleViewCanvas = new Canvas(this.scribbleViewBitmap);
this.scribbleView.setImageBitmap(this.scribbleViewBitmap);
drawBitmapToView(true);

this.touchHelper
.setLimitRect(
new Rect(0, 0, this.imageView.getWidth(),
this.imageView.getHeight()),
new Rect(0, 0, this.scribbleView.getWidth(),
this.scribbleView.getHeight()),
new ArrayList<>())
.openRawDrawing().setRawDrawingEnabled(true);
}

private void drawBitmapToView(boolean force) {
if (!force && imageView.isDirty()) {
// Log.v(XenaApplication.TAG,
// "ScribbleActivity::drawBitmapToView: ImageView is dirty, skipping.");
if (!force && scribbleView.isDrawing()) {
// Log.v(XenaApplication.TAG, "Dirty ScribbleView.");
return;
}
this.imageViewCanvas.drawRect(0, 0, imageView.getWidth(),
imageView.getHeight(),

this.scribbleViewCanvas.drawRect(0, 0, scribbleView.getWidth(),
scribbleView.getHeight(),
PAINT_TRANSPARENT);

if (this.pdfReader != null) {
ArrayList<PageBitmap> pages = this.pdfReader.getBitmapsForViewport(
new RectF(-this.pathManager.getViewportOffset().x,
-this.pathManager.getViewportOffset().y,
-this.pathManager.getViewportOffset().x
+ this.imageView.getWidth(),
+ this.scribbleView.getWidth(),
-this.pathManager.getViewportOffset().y
+ this.imageView.getHeight()));
+ this.scribbleView.getHeight()));
for (PageBitmap page : pages) {
this.imageViewCanvas.drawBitmap(page.bitmap,
this.scribbleViewCanvas.drawBitmap(page.bitmap,
page.location.left + this.pathManager.getViewportOffset().x,
page.location.top + this.pathManager.getViewportOffset().y, null);
}
}

for (Chunk chunk : pathManager.getVisibleChunks()) {
this.imageViewCanvas.drawBitmap(chunk.getBitmap(),
this.scribbleViewCanvas.drawBitmap(chunk.getBitmap(),
chunk.OFFSET_X + pathManager.getViewportOffset().x,
chunk.OFFSET_Y + pathManager.getViewportOffset().y, null);
}
this.imageView.postInvalidate();

this.scribbleView.postInvalidate();
}
}
Loading

0 comments on commit 1353bdc

Please sign in to comment.