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