Commit 02507f9e authored by Ryan Rowe's avatar Ryan Rowe
Browse files

Add scaled coordinates to Doodle

parent c6d04b3a
package cse340.doodle;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.Scanner;
abstract class Doodler extends AppCompatActivity {
protected static final Point PHONE_DIMS = new Point();
protected static final Point PIXEL_DIMS = new Point(1440, 2712);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindowManager().getDefaultDisplay().getSize(PHONE_DIMS);
}
/**
* Adds an image to the main canvas with the given position and size.
*
......@@ -30,17 +47,56 @@ abstract class Doodler extends AppCompatActivity {
* @param color Color for text in RGB.
* @return TextView which has been added to canvas.
*/
abstract TextView addText(FrameLayout canvas, String text, float x, float y, int fontSize, int color);
abstract TextView addText(FrameLayout canvas, String text, float x, float y, int fontSize,
int color);
/**
* Adds text to the main canvas with the given position, size, and color.
*
* @param canvas Canvas to put new image in.
* @param start Starting point for line.
* @param end Ending point for line
* @param startX Starting x coordinate for line.
* @param startY Ending x coordinate for line.
* @param endX Ending x coordinate for line.
* @param endY Ending y coordinate for line.
* @param width Stroke width for line.
* @param color Color for text in RGB.
* @return ImageView which has been added to main canvas.
*/
abstract ImageView addLine(FrameLayout canvas, Point start, Point end, int width, int color);
abstract ImageView addLine(FrameLayout canvas, float startX, float startY, float endX,
float endY, int width, int color);
/**
* Scales a y coordinate from Pixel 2 XL to any phone.
*
* @param y Original coordinate.
* @return Coordinate scaled to the current device screen size.
*/
protected float scaleY(float y) {
return y * PHONE_DIMS.y / PIXEL_DIMS.y;
}
/**
* Scales an x coordinate from Pixel 2 XL to any phone.
*
* @param x Original coordinate.
* @return Coordinate scaled to the current device screen size.
*/
protected float scaleX(float x) {
return x * PHONE_DIMS.x / PIXEL_DIMS.x;
}
protected void addAllImagesFromData(FrameLayout canvas) {
try {
Scanner scan = new Scanner(new InputStreamReader(getAssets().open("data.csv")));
scan.useDelimiter("[,\n]");
while (scan.hasNextLine()) {
addImage(canvas, scan.next(), scaleX(scan.nextFloat()), scaleY(scan.nextFloat()),
(int) scaleX(scan.nextInt()));
}
} catch (IOException e) {
throw new IllegalStateException("data.csv not found in assets");
} catch (InputMismatchException e) {
throw new IllegalStateException("data.csv is malformed");
}
}
}
package cse340.doodle;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Part1Activity extends Doodler {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the UI layout to this activity.
setContentView(R.layout.activity_main);
......@@ -26,30 +21,22 @@ public class Part1Activity extends Doodler {
// Add all images as a heart collage.
addAllImagesFromData(canvas);
// To allow this to run on multiple screen sizes, we scale all of our coordinate values
// using scaleX and scaleY. For a more efficient way to do this, see EX2 Layout.
// Add "I" and "UW" text.
addText(canvas,"I", 50f, 50f, 70, Color.rgb(145,123,76));
addText(canvas,"UW", 50f, 1650f, 70, Color.rgb(51,0,111));
addText(canvas,"I", scaleX(50), scaleY(50), 50,
Color.rgb(145,123,76));
addText(canvas,"UW", scaleX(50), scaleY(1650), 50,
Color.rgb(51,0,111));
// Add a line under "UW".
addLine(canvas, new Point(0, 1950), new Point(1440, 1950), 5, Color.rgb(145,123,76));
addLine(canvas, 0, scaleY(1950), scaleX(1440), scaleY(1950), 5,
Color.rgb(145,123,76));
// Check your app screen with the screenshot in our assignment instruction.
}
private void addAllImagesFromData(FrameLayout canvas) {
try {
Scanner scan = new Scanner(new InputStreamReader(getAssets().open("data.csv")));
scan.useDelimiter("[,\n]");
while (scan.hasNextLine()) {
addImage(canvas, scan.next(), scan.nextFloat(), scan.nextFloat(), scan.nextInt());
}
} catch (IOException e) {
throw new IllegalStateException("data.csv not found in assets");
} catch (InputMismatchException e) {
throw new IllegalStateException("data.csv is malformed");
}
}
@Override
public ImageView addImage(FrameLayout canvas, String imageName, float x, float y, int size) {
// Create ImageView and add it to canvas.
......@@ -66,7 +53,8 @@ public class Part1Activity extends Doodler {
}
@Override
public TextView addText(FrameLayout canvas, String text, float x, float y, int fontSize, int color) {
public TextView addText(FrameLayout canvas, String text, float x, float y, int fontSize,
int color) {
// Create TextView and add it to canvas.
// Set size, location and color of text.
......@@ -76,7 +64,8 @@ public class Part1Activity extends Doodler {
}
@Override
public ImageView addLine(FrameLayout canvas, Point start, Point end, int width, int color) {
ImageView addLine(FrameLayout canvas, float startX, float startY, float endX, float endY,
int width, int color) {
// Create a line and add it to canvas.
// One way to do this is to use an ImageView with a Bitmap using ImageView#setImageBitmap().
......@@ -85,5 +74,4 @@ public class Part1Activity extends Doodler {
// Return line as ImageView.
return null;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment