/*
* Copyright 2015 Daniel Dittmar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package dan.dit.whatsthat.image;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import dan.dit.whatsthat.R;
import dan.dit.whatsthat.preferences.User;
import dan.dit.whatsthat.util.image.ImageUtil;
/**
* Created by daniel on 13.10.15.
*/
public class LogoView extends View {
private Path mPath;
private Paint mBitmapPaint;
private Paint mCirclePaint;
private Path mCirclePath;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
private Paint mClearPaint;
private boolean mChanged;
public LogoView(Context context) {
super(context);
init();
}
public LogoView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setWillNotDraw(false);
mClearPaint = new Paint();
mClearPaint.setColor(Color.WHITE);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(15);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mCirclePaint = new Paint();
mCirclePath = new Path();
mCirclePaint.setAntiAlias(true);
mCirclePaint.setColor(Color.DKGRAY);
mCirclePaint.setStyle(Paint.Style.STROKE);
mCirclePaint.setStrokeJoin(Paint.Join.MITER);
mCirclePaint.setStrokeWidth(4f);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
initFromLogo();
}
private void initFromLogo() {
Logo logo = User.getInstance().getLogo(getResources());
mBitmap = logo.getSized(getWidth(), getHeight());
mCanvas = new Canvas(mBitmap);
mChanged = false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
canvas.drawPath( mCirclePath, mCirclePaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
mCirclePath.reset();
mCirclePath.addCircle(mX, mY, 30, Path.Direction.CW);
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
mCirclePath.reset();
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
mChanged = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
public void onRedo() {
if (mChanged) {
initFromLogo();
} else {
mCanvas.drawPaint(mClearPaint);
}
invalidate();
}
public void onSave() {
if (ImageUtil.saveToFile(mBitmap, User.getLogoFile(), Bitmap.CompressFormat.PNG, 100)) {
Toast.makeText(getContext(), R.string.logo_saved, Toast.LENGTH_SHORT).show();
}
}
}