MP1_CalebFontenot
1
Book Files/31_TouchEvents/DragAndDraw/app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
30
Book Files/31_TouchEvents/DragAndDraw/app/build.gradle
Normal file
@@ -0,0 +1,30 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 25
|
||||
buildToolsVersion '25.0.2'
|
||||
defaultConfig {
|
||||
applicationId "com.bignerdranch.android.draganddraw"
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 25
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
||||
exclude group: 'com.android.support', module: 'support-annotations'
|
||||
})
|
||||
compile 'com.android.support:appcompat-v7:25.3.0'
|
||||
compile 'com.android.support.constraint:constraint-layout:1.0.2'
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
25
Book Files/31_TouchEvents/DragAndDraw/app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in /Users/chris/AndroidDeveloper/sdk/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the proguardFiles
|
||||
# directive in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
@@ -0,0 +1,26 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Instrumentation test, which will execute on an Android device.
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() throws Exception {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.bignerdranch.android.draganddraw", appContext.getPackageName());
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest package="com.bignerdranch.android.draganddraw"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".DragAndDrawActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
@@ -0,0 +1,25 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import android.graphics.PointF;
|
||||
|
||||
public class Box {
|
||||
private PointF mOrigin;
|
||||
private PointF mCurrent;
|
||||
|
||||
public Box(PointF origin) {
|
||||
mOrigin = origin;
|
||||
mCurrent = origin;
|
||||
}
|
||||
|
||||
public PointF getCurrent() {
|
||||
return mCurrent;
|
||||
}
|
||||
|
||||
public void setCurrent(PointF current) {
|
||||
mCurrent = current;
|
||||
}
|
||||
|
||||
public PointF getOrigin() {
|
||||
return mOrigin;
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PointF;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BoxDrawingView extends View {
|
||||
private static final String TAG = "BoxDrawingView";
|
||||
|
||||
private Box mCurrentBox;
|
||||
private List<Box> mBoxen = new ArrayList<>();
|
||||
private Paint mBoxPaint;
|
||||
private Paint mBackgroundPaint;
|
||||
|
||||
// Used when creating the view in code
|
||||
public BoxDrawingView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
// Used when inflating the view from XML
|
||||
public BoxDrawingView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
// Paint the boxes a nice semitransparent red (ARGB)
|
||||
mBoxPaint = new Paint();
|
||||
mBoxPaint.setColor(0x22ff0000);
|
||||
|
||||
// Paint the background off-white
|
||||
mBackgroundPaint = new Paint();
|
||||
mBackgroundPaint.setColor(0xfff8efe0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
// Fill the background
|
||||
canvas.drawPaint(mBackgroundPaint);
|
||||
|
||||
for (Box box : mBoxen) {
|
||||
float left = Math.min(box.getOrigin().x, box.getCurrent().x);
|
||||
float right = Math.max(box.getOrigin().x, box.getCurrent().x);
|
||||
float top = Math.min(box.getOrigin().y, box.getCurrent().y);
|
||||
float bottom = Math.max(box.getOrigin().y, box.getCurrent().y);
|
||||
|
||||
canvas.drawRect(left, top, right, bottom, mBoxPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
PointF current = new PointF(event.getX(), event.getY());
|
||||
String action = "";
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
action = "ACTION_DOWN";
|
||||
// Reset drawing state
|
||||
mCurrentBox = new Box(current);
|
||||
mBoxen.add(mCurrentBox);
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
action = "ACTION_MOVE";
|
||||
if (mCurrentBox != null) {
|
||||
mCurrentBox.setCurrent(current);
|
||||
invalidate();
|
||||
}
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
action = "ACTION_UP";
|
||||
mCurrentBox = null;
|
||||
break;
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
action = "ACTION_CANCEL";
|
||||
mCurrentBox = null;
|
||||
break;
|
||||
}
|
||||
|
||||
Log.i(TAG, action + " at x=" + current.x +
|
||||
", y=" + current.y);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
public class DragAndDrawActivity extends SingleFragmentActivity {
|
||||
|
||||
@Override
|
||||
protected Fragment createFragment() {
|
||||
return DragAndDrawFragment.newInstance();
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class DragAndDrawFragment extends Fragment {
|
||||
|
||||
public static DragAndDrawFragment newInstance() {
|
||||
return new DragAndDrawFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View v = inflater.inflate(R.layout.fragment_drag_and_draw, container, false);
|
||||
return v;
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
public abstract class SingleFragmentActivity extends AppCompatActivity {
|
||||
|
||||
protected abstract Fragment createFragment();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_fragment);
|
||||
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
|
||||
|
||||
if (fragment == null) {
|
||||
fragment = createFragment();
|
||||
fm.beginTransaction()
|
||||
.add(R.id.fragment_container, fragment)
|
||||
.commit();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
<FrameLayout android:id="@+id/fragment_container"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.bignerdranch.android.draganddraw.BoxDrawingView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
After Width: | Height: | Size: 2.6 KiB |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 3.5 KiB |
After Width: | Height: | Size: 4.5 KiB |
After Width: | Height: | Size: 5.3 KiB |
After Width: | Height: | Size: 7.4 KiB |
After Width: | Height: | Size: 7.1 KiB |
After Width: | Height: | Size: 9.8 KiB |
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
</resources>
|
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">DragAndDraw</string>
|
||||
</resources>
|
@@ -0,0 +1,11 @@
|
||||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@@ -0,0 +1,17 @@
|
||||
package com.bignerdranch.android.draganddraw;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|