-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
baece5b
commit 2f817f4
Showing
7 changed files
with
249 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/luxlunaris/noadpadlight/ui/DoodleActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.luxlunaris.noadpadlight.ui; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.constraintlayout.widget.ConstraintLayout; | ||
|
||
import android.content.Intent; | ||
import android.graphics.Paint; | ||
import android.graphics.Path; | ||
import android.os.Bundle; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
|
||
import com.luxlunaris.noadpadlight.R; | ||
|
||
import java.io.File; | ||
|
||
public class DoodleActivity extends AppCompatActivity { | ||
|
||
|
||
DoodleView doodleView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
setContentView(R.layout.activity_doodle); | ||
|
||
ConstraintLayout l = findViewById(R.id.doodle_activity_layout); | ||
|
||
doodleView = new DoodleView(this); | ||
|
||
l.addView(doodleView); | ||
|
||
} | ||
|
||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
|
||
//inflate the menu's layout xml | ||
getMenuInflater().inflate(R.menu.doodle_activity_toolbar, menu); | ||
|
||
return super.onCreateOptionsMenu(menu); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | ||
|
||
switch (item.getItemId()){ | ||
case R.id.done_doodling: | ||
File doodleFile = doodleView.getSnapshot(); | ||
Intent resultIntent = new Intent(); | ||
resultIntent.putExtra("DOODLE_FILE", doodleFile); | ||
setResult(RESULT_OK, resultIntent); | ||
finish(); | ||
break; | ||
|
||
} | ||
|
||
return super.onOptionsItemSelected(item); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
app/src/main/java/com/luxlunaris/noadpadlight/ui/DoodleView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.luxlunaris.noadpadlight.ui; | ||
|
||
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.Log; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
public class DoodleView extends View { | ||
|
||
|
||
Paint paint; | ||
|
||
Path path; | ||
|
||
|
||
public DoodleView(Context context) { | ||
super(context); | ||
paint = new Paint(); | ||
setColor(Color.BLACK); | ||
setStrokeWidth(20); | ||
paint.setStyle(Paint.Style.STROKE); | ||
paint.setStrokeCap(Paint.Cap.ROUND); | ||
path =new Path(); | ||
} | ||
|
||
|
||
|
||
public void setColor(int color){ | ||
paint.setColor(color); | ||
} | ||
|
||
public void setStrokeWidth(int width){ | ||
paint.setStrokeWidth(width); | ||
} | ||
|
||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
setBackgroundColor(Color.WHITE); | ||
canvas.drawPath(path, paint); | ||
} | ||
|
||
|
||
@Override | ||
public boolean onTouchEvent(MotionEvent event) { | ||
|
||
float x = event.getX(); | ||
float y = event.getY(); | ||
Log.d("TOUCH_EVENT", "x: "+x+" y: "+y); | ||
|
||
|
||
switch(event.getAction()){ | ||
|
||
case MotionEvent.ACTION_MOVE: | ||
path.lineTo(x, y); | ||
break; | ||
case MotionEvent.ACTION_DOWN: | ||
path.moveTo(x, y); | ||
break; | ||
|
||
} | ||
|
||
//trigger onDraw | ||
invalidate(); | ||
|
||
//very important | ||
return true; | ||
} | ||
|
||
|
||
public File getSnapshot(){ | ||
|
||
File doodleFile = new File(getContext().getFilesDir(), "doodle.png"); | ||
|
||
try { | ||
FileOutputStream fos = new FileOutputStream(doodleFile); | ||
setDrawingCacheEnabled(true);/// | ||
Bitmap b = Bitmap.createBitmap( getDrawingCache()); | ||
setDrawingCacheEnabled(false);/// | ||
|
||
b.compress(Bitmap.CompressFormat.PNG, 100, fos); | ||
fos.flush(); | ||
fos.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return doodleFile; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/doodle_activity_layout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".ui.DoodleActivity"> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<item | ||
android:id="@+id/done_doodling" | ||
android:icon="@android:drawable/checkbox_on_background" | ||
android:title="done" | ||
app:showAsAction="always" /> | ||
|
||
|
||
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters