Skip to content

Commit

Permalink
sticker done.
Browse files Browse the repository at this point in the history
  • Loading branch information
minetsh committed Dec 21, 2017
1 parent ac1a684 commit 9dca9f9
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class IMGStickerAdjustHelper implements View.OnTouchListener {

private float mCenterX, mCenterY;

private double mRadius;
private double mRadius, mDegrees;

private Matrix M = new Matrix();

Expand All @@ -35,17 +35,22 @@ public IMGStickerAdjustHelper(IMGStickerView container, View view) {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

float x = event.getX();

float y = event.getY();

mCenterX = mCenterY = 0;

float pointX = (mView.getLeft() + mView.getRight()) / 2f;
float pointY = (mView.getTop() + mView.getBottom()) / 2f;
float pointX = mView.getX() + x - mContainer.getPivotX();

float pointY = mView.getY() + y - mContainer.getPivotY();

Log.d(TAG, String.format("X=%f, Y=%f, PivotX=%f, PivotY=%f", x, y, pointX, pointY));
Log.d(TAG, String.format("X=%f,Y=%f", pointX, pointY));

mRadius = toLength(mCenterX, mCenterY, pointX, pointY);
mRadius = toLength(0, 0, pointX, pointY);

mDegrees = toDegrees(pointY, pointX);

M.setTranslate(pointX - x, pointY - y);

Expand All @@ -58,22 +63,27 @@ public boolean onTouch(View v, MotionEvent event) {
case MotionEvent.ACTION_MOVE:

float[] xy = {event.getX(), event.getY()};
Log.d(TAG, String.format("Raw x=%f, y=%f", xy[0], xy[1]));

M.mapPoints(xy);
pointX = mView.getX() + xy[0] - mContainer.getPivotX();

pointY = mView.getY() + xy[1] - mContainer.getPivotY();

Log.d(TAG, String.format("X=%f,Y=%f", pointX, pointY));

double radius = toLength(0, 0, pointX, pointY);

double degrees = toDegrees(pointY, pointX);

float scale = (float) (radius / mRadius);

float scale = (float) (toLength(mCenterX, mCenterY, xy[0], xy[1]) / mRadius);

Log.d(TAG, "Scale=" + scale);
mContainer.addScale(scale);

Log.d(TAG, String.format("x=%f,y=%f", xy[0], xy[1]));
Log.d(TAG, String.format("dDegrees=%f", toDegrees(xy[1], xy[0])));
Log.d(TAG, " D = " + (degrees - mDegrees));

mContainer.setRotation((float) (mContainer.getRotation() + degrees - mDegrees));

float degrees = (float) ((mContainer.getRotation() + toDegrees(xy[1], xy[0])) % 360f);
Log.d(TAG, "degrees=" + degrees);
Log.d(TAG, " ");
mContainer.setRotation(degrees);
mRadius = radius;

return true;
}
Expand Down
31 changes: 13 additions & 18 deletions image/src/main/java/com/xingren/imaging/view/IMGStickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public void onInitialize(Context context) {

mRemoveView = new ImageView(context);
mRemoveView.setScaleType(ImageView.ScaleType.FIT_XY);
mRemoveView.setImageResource(R.drawable.image_ic_delete);
mRemoveView.setImageResource(R.mipmap.image_ic_delete);
addView(mRemoveView, getAnchorLayoutParams());
mRemoveView.setOnClickListener(this);

mAdjustView = new ImageView(context);
mAdjustView.setScaleType(ImageView.ScaleType.FIT_XY);
mAdjustView.setImageResource(R.drawable.image_ic_adjust);
mAdjustView.setImageResource(R.mipmap.image_ic_adjust);
addView(mAdjustView, getAnchorLayoutParams());

new IMGStickerAdjustHelper(this, mAdjustView);
Expand All @@ -113,12 +113,19 @@ public float getScale() {

@Override
public void setScale(float scale) {
float aScale = scale / mScale;

mScale = scale;

mContentView.setScaleX(mScale);
mContentView.setScaleY(mScale);

int pivotX = (getLeft() + getRight()) >> 1;
int pivotY = (getTop() + getBottom()) >> 1;

mFrame.set(pivotX, pivotY, pivotX, pivotY);
mFrame.inset(-(mContentView.getMeasuredWidth() >> 1), -(mContentView.getMeasuredHeight() >> 1));

Matrix m = new Matrix();
m.setScale(aScale, aScale, mFrame.centerX(), mFrame.centerY());
m.setScale(mScale, mScale, mFrame.centerX(), mFrame.centerY());
m.mapRect(mFrame);

mFrame.round(mTempFrame);
Expand All @@ -145,7 +152,6 @@ private LayoutParams getAnchorLayoutParams() {
@Override
public void draw(Canvas canvas) {
if (isShowing()) {
PAINT.setStrokeWidth(2f / getScaleX());
canvas.drawRect(ANCHOR_SIZE_HALF, ANCHOR_SIZE_HALF,
getWidth() - ANCHOR_SIZE_HALF,
getHeight() - ANCHOR_SIZE_HALF, PAINT);
Expand Down Expand Up @@ -173,7 +179,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}
}

// Check against our minimum height and width
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

Expand Down Expand Up @@ -209,17 +214,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
if (isShowing()) {
if (child == mContentView) {
canvas.save();
canvas.scale(getScale(), getScale(), getPivotX(), getPivotY());
boolean b = super.drawChild(canvas, child, drawingTime);
canvas.restore();
return b;
}
return super.drawChild(canvas, child, drawingTime);
}
return false;
return isShowing() && super.drawChild(canvas, child, drawingTime);
}

@Override
Expand Down
Binary file removed image/src/main/res/drawable-xhdpi/image_ic_adjust.png
Binary file not shown.
Binary file removed image/src/main/res/drawable-xhdpi/image_ic_delete.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9dca9f9

Please sign in to comment.