mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-02-17 12:10:47 +01:00
some refactoring, no functionality change
This commit is contained in:
parent
25d649309b
commit
8b8324c9dd
@ -3,7 +3,7 @@ apply plugin: 'android-apt'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion "23.0.2"
|
||||
buildToolsVersion "23.0.3"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.simplemobiletools.draw"
|
||||
|
@ -12,7 +12,7 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:name=".activities.MainActivity"
|
||||
android:screenOrientation="portrait">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
@ -22,12 +22,12 @@
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".AboutActivity"
|
||||
android:name=".activities.AboutActivity"
|
||||
android:label="@string/about"
|
||||
android:screenOrientation="portrait"/>
|
||||
|
||||
<activity
|
||||
android:name=".LicenseActivity"
|
||||
android:name=".activities.LicenseActivity"
|
||||
android:label="@string/third_party_licences"
|
||||
android:screenOrientation="portrait"/>
|
||||
|
||||
|
@ -14,53 +14,54 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MyCanvas extends View {
|
||||
private Paint paint;
|
||||
private Path path;
|
||||
private Map<Path, Integer> paths;
|
||||
private int color;
|
||||
private float curX;
|
||||
private float curY;
|
||||
private float startX;
|
||||
private float startY;
|
||||
private PathsChangedListener listener;
|
||||
private Paint mPaint;
|
||||
private Path mPath;
|
||||
private Map<Path, Integer> mPaths;
|
||||
private PathsChangedListener mListener;
|
||||
|
||||
private int mColor;
|
||||
private float mCurX;
|
||||
private float mCurY;
|
||||
private float mStartX;
|
||||
private float mStartY;
|
||||
|
||||
public MyCanvas(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
path = new Path();
|
||||
paint = new Paint();
|
||||
paint.setColor(Color.BLACK);
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
paint.setStrokeJoin(Paint.Join.ROUND);
|
||||
paint.setStrokeCap(Paint.Cap.ROUND);
|
||||
paint.setStrokeWidth(5f);
|
||||
paint.setAntiAlias(true);
|
||||
mPath = new Path();
|
||||
mPaint = new Paint();
|
||||
mPaint.setColor(Color.BLACK);
|
||||
mPaint.setStyle(Paint.Style.STROKE);
|
||||
mPaint.setStrokeJoin(Paint.Join.ROUND);
|
||||
mPaint.setStrokeCap(Paint.Cap.ROUND);
|
||||
mPaint.setStrokeWidth(5f);
|
||||
mPaint.setAntiAlias(true);
|
||||
|
||||
paths = new LinkedHashMap<>();
|
||||
paths.put(path, paint.getColor());
|
||||
mPaths = new LinkedHashMap<>();
|
||||
mPaths.put(mPath, mPaint.getColor());
|
||||
pathsUpdated();
|
||||
}
|
||||
|
||||
public void setListener(PathsChangedListener listener) {
|
||||
this.listener = listener;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public void undo() {
|
||||
if (paths.size() <= 0)
|
||||
if (mPaths.size() <= 0)
|
||||
return;
|
||||
|
||||
Path lastKey = null;
|
||||
for (Path key : paths.keySet()) {
|
||||
for (Path key : mPaths.keySet()) {
|
||||
lastKey = key;
|
||||
}
|
||||
|
||||
paths.remove(lastKey);
|
||||
mPaths.remove(lastKey);
|
||||
pathsUpdated();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setColor(int newColor) {
|
||||
color = newColor;
|
||||
mColor = newColor;
|
||||
}
|
||||
|
||||
public Bitmap getBitmap() {
|
||||
@ -75,46 +76,46 @@ public class MyCanvas extends View {
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
||||
for (Map.Entry<Path, Integer> entry : paths.entrySet()) {
|
||||
paint.setColor(entry.getValue());
|
||||
canvas.drawPath(entry.getKey(), paint);
|
||||
for (Map.Entry<Path, Integer> entry : mPaths.entrySet()) {
|
||||
mPaint.setColor(entry.getValue());
|
||||
canvas.drawPath(entry.getKey(), mPaint);
|
||||
}
|
||||
|
||||
paint.setColor(color);
|
||||
canvas.drawPath(path, paint);
|
||||
mPaint.setColor(mColor);
|
||||
canvas.drawPath(mPath, mPaint);
|
||||
}
|
||||
|
||||
private void actionDown(float x, float y) {
|
||||
path.reset();
|
||||
path.moveTo(x, y);
|
||||
curX = x;
|
||||
curY = y;
|
||||
mPath.reset();
|
||||
mPath.moveTo(x, y);
|
||||
mCurX = x;
|
||||
mCurY = y;
|
||||
}
|
||||
|
||||
private void actionMove(float x, float y) {
|
||||
path.quadTo(curX, curY, (x + curX) / 2, (y + curY) / 2);
|
||||
curX = x;
|
||||
curY = y;
|
||||
mPath.quadTo(mCurX, mCurY, (x + mCurX) / 2, (y + mCurY) / 2);
|
||||
mCurX = x;
|
||||
mCurY = y;
|
||||
}
|
||||
|
||||
private void actionUp() {
|
||||
path.lineTo(curX, curY);
|
||||
mPath.lineTo(mCurX, mCurY);
|
||||
|
||||
// draw a dot on click
|
||||
if (startX == curX && startY == curY) {
|
||||
path.lineTo(curX, curY + 2);
|
||||
path.lineTo(curX + 1, curY + 2);
|
||||
path.lineTo(curX + 1, curY);
|
||||
if (mStartX == mCurX && mStartY == mCurY) {
|
||||
mPath.lineTo(mCurX, mCurY + 2);
|
||||
mPath.lineTo(mCurX + 1, mCurY + 2);
|
||||
mPath.lineTo(mCurX + 1, mCurY);
|
||||
}
|
||||
|
||||
paths.put(path, paint.getColor());
|
||||
mPaths.put(mPath, mPaint.getColor());
|
||||
pathsUpdated();
|
||||
path = new Path();
|
||||
mPath = new Path();
|
||||
}
|
||||
|
||||
private void pathsUpdated() {
|
||||
if (listener != null && paths != null) {
|
||||
listener.pathsChanged(paths.size());
|
||||
if (mListener != null && mPaths != null) {
|
||||
mListener.pathsChanged(mPaths.size());
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,8 +126,8 @@ public class MyCanvas extends View {
|
||||
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
startX = x;
|
||||
startY = y;
|
||||
mStartX = x;
|
||||
mStartY = y;
|
||||
actionDown(x, y);
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.simplemobiletools.draw;
|
||||
package com.simplemobiletools.draw.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
@ -8,6 +8,9 @@ import android.text.Html;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.simplemobiletools.draw.BuildConfig;
|
||||
import com.simplemobiletools.draw.R;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import butterknife.BindView;
|
||||
@ -15,17 +18,18 @@ import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class AboutActivity extends AppCompatActivity {
|
||||
@BindView(R.id.about_copyright) TextView copyright;
|
||||
@BindView(R.id.about_version) TextView version;
|
||||
@BindView(R.id.about_email) TextView emailTV;
|
||||
private Resources res;
|
||||
@BindView(R.id.about_copyright) TextView mCopyright;
|
||||
@BindView(R.id.about_version) TextView mVersion;
|
||||
@BindView(R.id.about_email) TextView mEmailTV;
|
||||
|
||||
private Resources mRes;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_about);
|
||||
ButterKnife.bind(this);
|
||||
res = getResources();
|
||||
mRes = getResources();
|
||||
|
||||
setupEmail();
|
||||
setupVersion();
|
||||
@ -33,23 +37,23 @@ public class AboutActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void setupEmail() {
|
||||
final String email = res.getString(R.string.email);
|
||||
final String appName = res.getString(R.string.app_name);
|
||||
final String email = mRes.getString(R.string.email);
|
||||
final String appName = mRes.getString(R.string.app_name);
|
||||
final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>";
|
||||
emailTV.setText(Html.fromHtml(href));
|
||||
emailTV.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
mEmailTV.setText(Html.fromHtml(href));
|
||||
mEmailTV.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
}
|
||||
|
||||
private void setupVersion() {
|
||||
final String versionName = BuildConfig.VERSION_NAME;
|
||||
final String versionText = String.format(res.getString(R.string.version), versionName);
|
||||
version.setText(versionText);
|
||||
final String versionText = String.format(mRes.getString(R.string.version), versionName);
|
||||
mVersion.setText(versionText);
|
||||
}
|
||||
|
||||
private void setupCopyright() {
|
||||
final int year = Calendar.getInstance().get(Calendar.YEAR);
|
||||
final String copyrightText = String.format(res.getString(R.string.copyright), year);
|
||||
copyright.setText(copyrightText);
|
||||
final String copyrightText = String.format(mRes.getString(R.string.copyright), year);
|
||||
mCopyright.setText(copyrightText);
|
||||
}
|
||||
|
||||
@OnClick(R.id.about_license)
|
@ -1,14 +1,17 @@
|
||||
package com.simplemobiletools.draw;
|
||||
package com.simplemobiletools.draw.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import com.simplemobiletools.draw.R;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class LicenseActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
@ -1,4 +1,4 @@
|
||||
package com.simplemobiletools.draw;
|
||||
package com.simplemobiletools.draw.activities;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Intent;
|
||||
@ -21,6 +21,10 @@ import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.simplemobiletools.draw.MyCanvas;
|
||||
import com.simplemobiletools.draw.R;
|
||||
import com.simplemobiletools.draw.Utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
@ -38,19 +42,20 @@ public class MainActivity extends AppCompatActivity implements MyCanvas.PathsCha
|
||||
private static final String SAVE_FOLDER_NAME = "Simple Draw";
|
||||
private static final int STORAGE_PERMISSION = 1;
|
||||
|
||||
@BindView(R.id.my_canvas) MyCanvas myCanvas;
|
||||
@BindView(R.id.undo) View undoBtn;
|
||||
@BindView(R.id.color_picker) View colorPicker;
|
||||
@BindView(R.id.my_canvas) MyCanvas mMyCanvas;
|
||||
@BindView(R.id.undo) View mUndoBtn;
|
||||
@BindView(R.id.color_picker) View mColorPicker;
|
||||
|
||||
private String curFileName;
|
||||
|
||||
private int color;
|
||||
private String curFileName;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
myCanvas.setListener(this);
|
||||
mMyCanvas.setListener(this);
|
||||
|
||||
setColor(Color.BLACK);
|
||||
}
|
||||
@ -141,7 +146,7 @@ public class MainActivity extends AppCompatActivity implements MyCanvas.PathsCha
|
||||
}
|
||||
}
|
||||
|
||||
final Bitmap bitmap = myCanvas.getBitmap();
|
||||
final Bitmap bitmap = mMyCanvas.getBitmap();
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
final File file = new File(directory, fileName);
|
||||
@ -166,7 +171,7 @@ public class MainActivity extends AppCompatActivity implements MyCanvas.PathsCha
|
||||
|
||||
private void shareImage() {
|
||||
final String shareTitle = getResources().getString(R.string.share_via);
|
||||
final Bitmap bitmap = myCanvas.getBitmap();
|
||||
final Bitmap bitmap = mMyCanvas.getBitmap();
|
||||
final Intent sendIntent = new Intent();
|
||||
final Uri uri = getImageUri(bitmap);
|
||||
if (uri == null)
|
||||
@ -211,7 +216,7 @@ public class MainActivity extends AppCompatActivity implements MyCanvas.PathsCha
|
||||
|
||||
@OnClick(R.id.undo)
|
||||
public void undo() {
|
||||
myCanvas.undo();
|
||||
mMyCanvas.undo();
|
||||
}
|
||||
|
||||
@OnClick(R.id.color_picker)
|
||||
@ -232,12 +237,12 @@ public class MainActivity extends AppCompatActivity implements MyCanvas.PathsCha
|
||||
|
||||
private void setColor(int pickedColor) {
|
||||
color = pickedColor;
|
||||
colorPicker.setBackgroundColor(color);
|
||||
myCanvas.setColor(color);
|
||||
mColorPicker.setBackgroundColor(color);
|
||||
mMyCanvas.setColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pathsChanged(int cnt) {
|
||||
undoBtn.setVisibility(cnt > 0 ? View.VISIBLE : View.GONE);
|
||||
mUndoBtn.setVisibility(cnt > 0 ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user