Skip to content

Commit

Permalink
mosaic test ok.
Browse files Browse the repository at this point in the history
  • Loading branch information
minetsh committed Nov 29, 2017
1 parent 058b728 commit 14da113
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 82 deletions.
86 changes: 70 additions & 16 deletions image/src/main/java/com/xingren/imaging/core/IMGImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;

import com.xingren.imaging.core.clip.IMGClip;
Expand All @@ -24,7 +26,7 @@ public class IMGImage {

private static final String TAG = "IMGImage";

private Bitmap mImage;
private Bitmap mImage, mMosaicImage;

/**
* 完整图片边框
Expand All @@ -46,7 +48,7 @@ public class IMGImage {
/**
* 编辑模式
*/
private IMGMode mMode = IMGMode.DOODLE;
private IMGMode mMode = IMGMode.MOSAIC;

private float mWindowPivotX, mWindowPivotY;

Expand Down Expand Up @@ -100,13 +102,28 @@ public IMGImage() {

public void setBitmap(Bitmap bitmap) {
this.mImage = bitmap;
mMosaicImage = null;
onMosaicBitmap();
onImageChanged();
}

public IMGMode getMode() {
return mMode;
}

public void setMode(IMGMode mode) {
this.mMode = mode;
onMosaicBitmap();
}

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

if (mMode == IMGMode.MOSAIC) {
mMosaicImage = Bitmap.createScaledBitmap(mImage, mImage.getWidth() / 30, mImage.getHeight() / 30, true);
}
}

public void onImageChanged() {
isInitial = false;
onWindowChanged(mWindowPivotX, mWindowPivotY, mWindowWidth, mWindowHeight);
Expand Down Expand Up @@ -156,12 +173,29 @@ public <S extends IMGSticker> void addSticker(S sticker) {
}
}

public void addDoodle(IMGPath doodle) {
public void addDoodle(IMGPath doodle, float sx, float sy) {
if (doodle != null) {

float scale = 1f / getScale();
M.setTranslate(sx - mClipFrame.left, sy - mClipFrame.top);
M.postScale(scale, scale);
doodle.transform(M);

mDoodles.add(doodle);
}
}

public void addMosaic(IMGPath mosaic, float sx, float sy) {
if (mosaic != null) {
float scale = 1f / getScale();
M.setTranslate(sx - mClipFrame.left, sy - mClipFrame.top);
M.postScale(scale, scale);
mosaic.transform(M);

mMosaics.add(mosaic);
}
}

private void moveToForeground(IMGSticker sticker) {
if (sticker == null) return;

Expand Down Expand Up @@ -261,7 +295,10 @@ public PointF getPivot() {

public void onDrawImage(Canvas canvas) {
// canvas.clipRect(mClipFrame);
canvas.drawBitmap(mImage, null, mFrame, null);

Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

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

if (DEBUG) {
canvas.drawText(String.format(Locale.CHINA, "[%.1f, %.1f]",
Expand All @@ -272,22 +309,45 @@ public void onDrawImage(Canvas canvas) {

}

public void onDrawDoodles(Canvas canvas) {
if (!mDoodles.isEmpty()) {
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);

canvas.save();
for (IMGPath path : mDoodles) {
float scale = getScale();
canvas.translate(mClipFrame.left, mClipFrame.top);
canvas.scale(scale, scale);

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

canvas.restore();

Paint p = new Paint();

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

canvas.drawBitmap(mMosaicImage, null, mClipFrame, p);

canvas.restoreToCount(sc);
}
}

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

canvas.save();
for (IMGPath path : mMosaics) {

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

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

canvas.restore();
}
}
Expand Down Expand Up @@ -343,10 +403,6 @@ public void onScale(float factor, float focusX, float focusY) {
M.mapRect(mFrame);
M.mapRect(mClipFrame);

for (IMGPath doodle : mDoodles) {
doodle.transform(M);
}

for (IMGSticker sticker : mBackStickers) {
M.mapRect(sticker.getFrame());
float tPivotX = sticker.getX() + sticker.getPivotX();
Expand All @@ -356,8 +412,6 @@ public void onScale(float factor, float focusX, float focusY) {
sticker.setX(sticker.getX() + sticker.getFrame().centerX() - tPivotX);
sticker.setY(sticker.getY() + sticker.getFrame().centerY() - tPivotY);
}

IMGPath.setStrokeWidthScale(getScale());
}

public void onScaleEnd() {
Expand Down
31 changes: 13 additions & 18 deletions image/src/main/java/com/xingren/imaging/core/IMGPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ public class IMGPath {

private int color = Color.RED;

private Matrix matrix = new Matrix();

private IMGMode mode = IMGMode.DOODLE;

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

private static final float SW_BASE = 3f;

static {
P.setStyle(Paint.Style.STROKE);
P.setStrokeWidth(SW_BASE);
P.setStrokeWidth(20f);
}

public IMGPath(Path path, IMGMode mode, int color) {
Expand All @@ -35,33 +31,32 @@ public IMGPath(Path path, IMGMode mode, int color) {
this.color = color;
}

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

public void onDraw(Canvas canvas) {
if (mode == IMGMode.DOODLE) {
onDrawDoodle(canvas);
} else if (mode == IMGMode.MOSAIC) {

onDrawMosaic(canvas);
}
}

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

public void transform(Matrix matrix) {
this.matrix.postConcat(matrix);
}

public static void setStrokeWidth(float width) {
P.setStrokeWidth(width);
private void onDrawMosaic(Canvas canvas) {
P.setColor(Color.BLACK);
path.setFillType(Path.FillType.EVEN_ODD);
canvas.drawPath(path, P);
}

public static void setStrokeWidthScale(float scale) {
setStrokeWidth(SW_BASE * scale);
public void transform(Matrix matrix) {
path.transform(matrix);
}
}
Loading

0 comments on commit 14da113

Please sign in to comment.