Skip to content

Commit

Permalink
doodle and mosaic optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
minetsh committed Dec 4, 2017
1 parent d478c51 commit dcea911
Show file tree
Hide file tree
Showing 23 changed files with 352 additions and 172 deletions.
34 changes: 27 additions & 7 deletions image/src/main/java/com/xingren/imaging/ImageEditActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.xingren.imaging;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.ViewSwitcher;

import com.xingren.imaging.core.IMGMode;
import com.xingren.imaging.core.IMGText;
import com.xingren.imaging.view.IMGStickerTextView;
import com.xingren.imaging.view.IMGView;

/**
Expand Down Expand Up @@ -44,11 +45,6 @@ protected void onCreate(Bundle savedInstanceState) {
mPathOpSwitcher = findViewById(R.id.vs_dm_op);

mImageView = findViewById(R.id.image_canvas);

IMGStickerTextView sticker = new IMGStickerTextView(getApplicationContext());

mImageView.addStickerView(sticker);

}

@Override
Expand All @@ -62,6 +58,12 @@ public void onClick(View v) {
onModeClick(IMGMode.MOSAIC);
} else if (vid == R.id.btn_clip) {
onModeClick(IMGMode.CLIP);
} else if (vid == R.id.btn_undo) {
onUndoClick();
} else if (vid == R.id.tv_done) {
onDoneClick();
} else if (vid == R.id.tv_cancel) {
onCancelClick();
}
}

Expand All @@ -82,7 +84,6 @@ private void onTextClick() {
if (mTextDialog == null) {
mTextDialog = new ImageTextDialog(this, this);
}
mTextDialog.reset();
mTextDialog.show();
}

Expand Down Expand Up @@ -110,4 +111,23 @@ private void updateModeUI() {
public void onText(IMGText text) {
mImageView.addStickerText(text);
}

private void onUndoClick() {
IMGMode mode = mImageView.getMode();
if (mode == IMGMode.DOODLE) {
mImageView.undoDoodle();
} else if (mode == IMGMode.MOSAIC) {
mImageView.undoMosaic();
}
}

private void onDoneClick() {
Bitmap bitmap = mImageView.saveBitmap();
setResult(RESULT_OK, new Intent().putExtra("IMAGE", bitmap));
finish();
}

private void onCancelClick() {
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void setText(IMGText text) {
}
} else mDefaultText = text;
}

public void reset() {
setText(new IMGText(null, Color.WHITE));
}
Expand Down
108 changes: 74 additions & 34 deletions image/src/main/java/com/xingren/imaging/core/IMGImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
Expand Down Expand Up @@ -44,10 +45,14 @@ public class IMGImage {
*/
private RectF mHomeFrame = new RectF();

private IMGClip mClip;

/**
* 裁剪模式时当前触摸锚点
*/
private IMGClip.Anchor mAnchor;

/**
* 裁剪窗口
*/
private IMGClipWindow mClipWin = new IMGClipWindow();

/**
Expand Down Expand Up @@ -88,17 +93,21 @@ public class IMGImage {

private static final int MAX_SIZE = 10000;

private static final Matrix M = new Matrix();
private Paint mDoodlePaint, mMosaicPaint, mPaint;

private static final Paint P = new Paint(Paint.ANTI_ALIAS_FLAG);
private Matrix M = new Matrix();

private static final boolean DEBUG = true;

static {
P.setColor(Color.RED);
P.setStrokeWidth(2);
P.setTextSize(36);
P.setStyle(Paint.Style.STROKE);
{
// Doodle&Mosaic 's paint
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(IMGPath.BASE_DOODLE_WIDTH);
mPaint.setColor(Color.RED);
mPaint.setPathEffect(new CornerPathEffect(IMGPath.BASE_DOODLE_WIDTH));
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}

public IMGImage() {
Expand All @@ -113,6 +122,9 @@ public void setBitmap(Bitmap bitmap) {
this.mImage = bitmap;

// 清空马赛克图层
if (mMosaicImage != null) {
mMosaicImage.recycle();
}
this.mMosaicImage = null;

makeMosaicBitmap();
Expand All @@ -134,16 +146,51 @@ public void setMode(IMGMode mode) {
}
}

public boolean isMosaicEmpty() {
return mMosaics.isEmpty();
}

public boolean isDoodleEmpty() {
return mDoodles.isEmpty();
}

public void undoDoodle() {
if (!mDoodles.isEmpty()) {
mDoodles.remove(mDoodles.size() - 1);
}
}

public void undoMosaic() {
if (!mMosaics.isEmpty()) {
mMosaics.remove(mMosaics.size() - 1);
}
}

public RectF getClipFrame() {
return mClipFrame;
}

private void makeMosaicBitmap() {
if (mMosaicImage != null || mImage == null) {
return;
}

if (mMode == IMGMode.MOSAIC) {

// TODO
mMosaicImage = Bitmap.createScaledBitmap(mImage,
mImage.getWidth() / 30, mImage.getHeight() / 30, true);
int w = Math.round(mImage.getWidth() / 64f);
int h = Math.round(mImage.getHeight() / 64f);

w = Math.max(w, 8);
h = Math.max(h, 8);

// 马赛克画刷
if (mMosaicPaint == null) {
mMosaicPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mMosaicPaint.setFilterBitmap(false);
mMosaicPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
}

mMosaicImage = Bitmap.createScaledBitmap(mImage, w, h, false);
}
}

Expand Down Expand Up @@ -371,52 +418,45 @@ public PointF getPivot() {
public void onDrawImage(Canvas canvas) {
// canvas.clipRect(mClipFrame);

// TODO
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

canvas.drawBitmap(mImage, null, mClipFrame, null);


canvas.drawBitmap(mImage, null, mClipFrame, p);
}

public void onDrawMosaics(Canvas canvas) {
if (!mMosaics.isEmpty()) {

int sc = canvas.saveLayer(mClipFrame.left, mClipFrame.top, mClipFrame.right, mClipFrame.bottom, null, Canvas.ALL_SAVE_FLAG);
public int onDrawMosaicsPath(Canvas canvas) {
int layerCount = canvas.saveLayer(mClipFrame.left, mClipFrame.top,
mClipFrame.right, mClipFrame.bottom, null, Canvas.ALL_SAVE_FLAG);

if (!isMosaicEmpty()) {
canvas.save();
float scale = getScale();
canvas.translate(mClipFrame.left, mClipFrame.top);
canvas.scale(scale, scale);

for (IMGPath path : mMosaics) {
path.onDraw(canvas);
path.onDrawMosaic(canvas, mPaint);
}

canvas.restore();
}

Paint p = new Paint();

p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

canvas.drawBitmap(mMosaicImage, null, mClipFrame, p);
return layerCount;
}

canvas.restoreToCount(sc);
}
public void onDrawMosaic(Canvas canvas, int layerCount) {
canvas.drawBitmap(mMosaicImage, null, mFrame, mMosaicPaint);
canvas.restoreToCount(layerCount);
}

public void onDrawDoodles(Canvas canvas) {
if (!mDoodles.isEmpty()) {

if (!isDoodleEmpty()) {
canvas.save();

float scale = getScale();
canvas.translate(mClipFrame.left, mClipFrame.top);
canvas.scale(scale, scale);

for (IMGPath path : mDoodles) {
path.onDraw(canvas);
path.onDrawDoodle(canvas, mPaint);
}

canvas.restore();
}
}
Expand Down
54 changes: 28 additions & 26 deletions image/src/main/java/com/xingren/imaging/core/IMGPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
Expand All @@ -19,14 +18,20 @@ public class IMGPath {

private IMGMode mode = IMGMode.DOODLE;

private static final Paint P = new Paint(Paint.ANTI_ALIAS_FLAG);
public static final float BASE_DOODLE_WIDTH = 20f;

static {
P.setStyle(Paint.Style.STROKE);
P.setStrokeWidth(20f);
P.setPathEffect(new CornerPathEffect(20));
P.setStrokeCap(Paint.Cap.ROUND);
P.setStrokeJoin(Paint.Join.ROUND);
public static final float BASE_MOSAIC_WIDTH = 40f;

public IMGPath() {
this(new Path());
}

public IMGPath(Path path) {
this(path, IMGMode.DOODLE);
}

public IMGPath(Path path, IMGMode mode) {
this(path, mode, Color.RED);
}

public IMGPath(Path path, IMGMode mode, int color) {
Expand All @@ -38,31 +43,28 @@ public IMGPath(Path path, IMGMode mode, int color) {
}
}

public IMGPath(Path path, IMGMode mode) {
this.path = path;
public IMGMode getMode() {
return mode;
}

public void setMode(IMGMode mode) {
this.mode = mode;
if (mode == IMGMode.MOSAIC) {
path.setFillType(Path.FillType.EVEN_ODD);
}
}

public void onDraw(Canvas canvas) {
public void onDrawDoodle(Canvas canvas, Paint paint) {
if (mode == IMGMode.DOODLE) {
onDrawDoodle(canvas);
} else if (mode == IMGMode.MOSAIC) {
onDrawMosaic(canvas);
paint.setColor(color);
paint.setStrokeWidth(BASE_DOODLE_WIDTH);
// rewind
canvas.drawPath(path, paint);
}
}

private void onDrawDoodle(Canvas canvas) {
P.setColor(color);
// rewind
canvas.drawPath(path, P);
}

private void onDrawMosaic(Canvas canvas) {
P.setColor(Color.BLACK);
canvas.drawPath(path, P);
public void onDrawMosaic(Canvas canvas, Paint paint) {
if (mode == IMGMode.MOSAIC) {
paint.setStrokeWidth(BASE_MOSAIC_WIDTH);
canvas.drawPath(path, paint);
}
}

public void transform(Matrix matrix) {
Expand Down
43 changes: 0 additions & 43 deletions image/src/main/java/com/xingren/imaging/core/clip/IMGClipView.java

This file was deleted.

Loading

0 comments on commit dcea911

Please sign in to comment.