diff --git a/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/EditPhoto.java b/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/EditPhoto.java index 5b6d269..2a42502 100644 --- a/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/EditPhoto.java +++ b/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/EditPhoto.java @@ -26,517 +26,625 @@ import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; + +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +public class EditPhoto extends Activity implements View.OnTouchListener, View.OnDragListener { + + private static final String FILE_EXTERNAL = "myFile.jpg"; + private static final String TAG_YULIYA = "logTags"; + + Bitmap b; + Bitmap bitmap; + /* + the next two variables are never used, could be deleted + */ + private Button Vanilla; + private Button demotivational; + private Button save; + private EditText editText, editText2, demoTitle, demoText; + private ImageView imageView, demoImage; + private String TAG = "GallerySaving"; + private RelativeLayout memeLayout; + private LinearLayout linearLayout2, linearLayout3, textTop, textMid, textBot; + + Button ten; + Button fifteen; + Button twenty; + Button twentyfive; + Button black; + Button white; + Button red; + Button blue; + RelativeLayout root; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_edit_photo); + + /* + onCreate is a pretty big method, a good idea to break it down into smaller pieces + */ + setViews(); + setListeners(); + shareButtonOnClick(); + getIntentInfo(); + extraEffects(); + save.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + saveMeme(getBitmapFromView(memeLayout)); - public class EditPhoto extends Activity implements View.OnTouchListener, View.OnDragListener { - - - Bitmap b; - Bitmap bitmap; - private Button Vanilla; - private Button demotivational; - private EditText editText, editText2, demoTitle, demoText; - private ImageView imageView, demoImage; - private String TAG = "GallerySaving"; - RelativeLayout memeLayout; - LinearLayout linearLayout2; - LinearLayout linearLayout3; - Button ten; - Button fifteen; - Button twenty; - Button twentyfive; - Button black; - Button white; - Button red; - Button blue; - RelativeLayout root; - - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_edit_photo); - - imageView = (ImageView) findViewById(R.id.mImageView); - demoImage = (ImageView) findViewById(R.id.demotivationalImage); - - root = (RelativeLayout) findViewById(R.id.root); - linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - ten = (Button) findViewById(R.id.ten); - fifteen = (Button) findViewById(R.id.fifteen); - twenty = (Button) findViewById(R.id.twenty); - twentyfive = (Button) findViewById(R.id.twentyfive); - black = (Button) findViewById(R.id.black); - white = (Button) findViewById(R.id.white); - red = (Button) findViewById(R.id.red); - blue = (Button) findViewById(R.id.blue); - Vanilla = (Button) findViewById(R.id.vanilla); - - - editText = (EditText) findViewById(R.id.editText); - editText2 = (EditText) findViewById(R.id.editText2); - Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/impact.ttf"); - - editText.setTypeface(custom_font); - editText2.setTypeface(custom_font); - - - editText.setOnTouchListener(this); - editText2.setOnTouchListener(this); - - - //Drag and drop layouts for drag and drop EditText feature - LinearLayout textTop = (LinearLayout) findViewById(R.id.textTop); - LinearLayout textMid = (LinearLayout) findViewById(R.id.textMid); - LinearLayout textBot = (LinearLayout) findViewById(R.id.textBottom); - - textBot.setOnDragListener(this); - textMid.setOnDragListener(this); - textTop.setOnDragListener(this); - - imageView = (ImageView) findViewById(R.id.mImageView); - demoImage = (ImageView) findViewById(R.id.demotivationalImage); - memeLayout = (RelativeLayout) findViewById(R.id.meme); - - - //----------------------------GET IMAGE FROM PREVIOUS INTENT--------------------------// - - //opens pic in this activity + } + }); + + } + + //method to set up views + public void setViews() { + imageView = (ImageView) findViewById(R.id.mImageView); + demoImage = (ImageView) findViewById(R.id.demotivationalImage); + root = (RelativeLayout) findViewById(R.id.root); + linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); + linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + ten = (Button) findViewById(R.id.ten); + fifteen = (Button) findViewById(R.id.fifteen); + twenty = (Button) findViewById(R.id.twenty); + twentyfive = (Button) findViewById(R.id.twentyfive); + black = (Button) findViewById(R.id.black); + white = (Button) findViewById(R.id.white); + red = (Button) findViewById(R.id.red); + blue = (Button) findViewById(R.id.blue); + Vanilla = (Button) findViewById(R.id.vanilla); + editText = (EditText) findViewById(R.id.editText); + editText2 = (EditText) findViewById(R.id.editText2); + textTop = (LinearLayout) findViewById(R.id.textTop); + textMid = (LinearLayout) findViewById(R.id.textMid); + textBot = (LinearLayout) findViewById(R.id.textBottom); + memeLayout = (RelativeLayout) findViewById(R.id.meme); + demoTitle = (EditText) findViewById(R.id.demotivationalTitle); + demoText = (EditText) findViewById(R.id.demotivationalText); + save = (Button) findViewById(R.id.save); + + } + + //method to set up listeners + public void setListeners() { + Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/impact.ttf"); + + editText.setTypeface(custom_font); + editText2.setTypeface(custom_font); + + editText.setOnTouchListener(this); + editText2.setOnTouchListener(this); + + textBot.setOnDragListener(this); + textMid.setOnDragListener(this); + textTop.setOnDragListener(this); + + } + + + //method to implement shareButton + public void shareButtonOnClick() { + // Shares image via Email, Text, Bluetooth, etc... + + Button share = (Button) findViewById(R.id.share); + share.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { +/* + +you dont need to set up views of activity in every method. One time is enough OnCreatwe. Redundant code + */ +// imageView = (ImageView) findViewById(R.id.mImageView); +// Vanilla = (Button) findViewById(R.id.vanilla); +// editText = (EditText) findViewById(R.id.editText); +// editText2 = (EditText) findViewById(R.id.editText2); + editText.setHint(""); + editText2.setHint(""); + //opens pic in this activity if (getIntent().hasExtra("byteArray")) { - b = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length); - imageView = (ImageView) findViewById(R.id.mImageView); - Vanilla = (Button) findViewById(R.id.vanilla); - editText = (EditText) findViewById(R.id.editText); - editText2 = (EditText) findViewById(R.id.editText2); - demoTitle = (EditText) findViewById(R.id.demotivationalTitle); - demoText = (EditText) findViewById(R.id.demotivationalText); - imageView.setImageBitmap(b); - demoImage.setImageBitmap(b); - - } else { - - //retrieve passed uri - Uri uri = getIntent().getExtras().getParcelable("image"); - //retrieve bitmap uri from intent - try { - bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); - - } catch (IOException e) { - e.printStackTrace(); - } - //create bitmap for use within activity - try { - bitmap = Bitmap.createBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri)); - } catch (IOException e) { - e.printStackTrace(); - } - imageView.setImageBitmap(bitmap); - demoImage.setImageBitmap(bitmap); + Bundle extras = getIntent().getExtras(); + byte[] byteArray = extras.getByteArray("byteArray"); + Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); + imageView.setImageBitmap(bm); } - //-----------------------------SHARE BUTTON ONCLICKLISTENER---------------------------// - - // Shares image via Email, Text, Bluetooth, etc... - Button share = (Button) findViewById(R.id.share); - share.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - - imageView = (ImageView) findViewById(R.id.mImageView); - Vanilla = (Button) findViewById(R.id.vanilla); - editText = (EditText) findViewById(R.id.editText); - editText2 = (EditText) findViewById(R.id.editText2); - editText.setHint(""); - editText2.setHint(""); - //opens pic in this activity - if (getIntent().hasExtra("byteArray")) { - Bundle extras = getIntent().getExtras(); - byte[] byteArray = extras.getByteArray("byteArray"); - Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); - imageView.setImageBitmap(bm); - } - Button share = (Button) findViewById(R.id.share); - share.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - String pathOfBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", null); - Uri bmpUri = Uri.parse(pathOfBmp); - Intent attachIntent = new Intent(Intent.ACTION_SEND); - attachIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); - attachIntent.setType("image/png"); - startActivity(attachIntent); - } - }); - - b = getBitmapFromView(memeLayout); - - String pathOfBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", null); - Uri bmpUri = Uri.parse(pathOfBmp); - - Intent attachIntent = new Intent(Intent.ACTION_SEND); - attachIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); - attachIntent.setType("image/png"); - startActivity(attachIntent); - } - }); - } - - //----------------------------VANILLA AND DEMOTIVATIONAL METHODS--------------------------// - - //Sets Vanilla meme editing view - public void vanillaM(View v) { - linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - editText = (EditText) findViewById(R.id.editText); - editText2 = (EditText) findViewById(R.id.editText2); - memeLayout = (RelativeLayout) findViewById(R.id.meme); - demoImage = (ImageView) findViewById(R.id.demotivationalImage); - demoTitle = (EditText) findViewById(R.id.demotivationalTitle); - demoText = (EditText) findViewById(R.id.demotivationalText); - linearLayout2.setVisibility(View.VISIBLE); - imageView.setVisibility(View.VISIBLE); - editText.setVisibility(View.VISIBLE); - editText2.setVisibility(View.VISIBLE); - demoImage.setVisibility(View.INVISIBLE); - demoTitle.setVisibility(View.INVISIBLE); - demoText.setVisibility(View.INVISIBLE); - } + b = getBitmapFromView(memeLayout); - //Sets demotivational meme editing view - public void demotivate(View v) { + String pathOfBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", null); + Uri bmpUri = Uri.parse(pathOfBmp); - editText = (EditText) findViewById(R.id.editText); - editText2 = (EditText) findViewById(R.id.editText2); - memeLayout = (RelativeLayout) findViewById(R.id.meme); - demoImage = (ImageView) findViewById(R.id.demotivationalImage); - demoTitle = (EditText) findViewById(R.id.demotivationalTitle); - demoText = (EditText) findViewById(R.id.demotivationalText); - - - memeLayout.setBackgroundColor(Color.BLACK); - imageView.setVisibility(View.INVISIBLE); - editText.setVisibility(View.INVISIBLE); - editText2.setVisibility(View.INVISIBLE); - demoImage.setVisibility(View.VISIBLE); - demoTitle.setVisibility(View.VISIBLE); - demoText.setVisibility(View.VISIBLE); - - } - - //-------------------------------IMAGE STORE AND SAVE METHODS-----------------------------// - - //onClick method for the save button. Calls other methods to create the save image function - public void storeImage(View v) { - editText.setHint(""); - editText2.setHint(""); - Bitmap image = getBitmapFromView(memeLayout); - File pictureFile = createImageFile(); - addImageToFile(image, pictureFile); - - } - - /** - * Create a File for saving an image - * Handles file name, and where to store it - */ - private File createImageFile() { - String timeStamp = new SimpleDateFormat("MMddyyyy_HHmmss").format(new Date()); - String imageName = "MEME_" + timeStamp + ".jpg"; - - File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); - File imageFile = new File(path, imageName); - Log.d("Path: ", path.getPath()); - return imageFile; + Intent attachIntent = new Intent(Intent.ACTION_SEND); + attachIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); + attachIntent.setType("image/png"); + startActivity(attachIntent); + } + }); + } + + //method to extract info from Intent + public void getIntentInfo() { + if (getIntent().hasExtra("byteArray")) { + b = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length); + + /* + * + * + * + * + NO NEED TO SET UP VIEWS IN EVERY METHOD + * + * + */ +// imageView = (ImageView) findViewById(R.id.mImageView); +// Vanilla = (Button) findViewById(R.id.vanilla); +// editText = (EditText) findViewById(R.id.editText); +// editText2 = (EditText) findViewById(R.id.editText2); +// demoTitle = (EditText) findViewById(R.id.demotivationalTitle); +// demoText = (EditText) findViewById(R.id.demotivationalText); +// imageView.setImageBitmap(b); +// demoImage.setImageBitmap(b); + + } else { + + //retrieve passed uri + Uri uri = getIntent().getExtras().getParcelable("image"); + //retrieve bitmap uri from intent + try { + bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); + } catch (IOException e) { + e.printStackTrace(); + } + //create bitmap for use within activity + try { + bitmap = Bitmap.createBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri)); + } catch (IOException e) { + e.printStackTrace(); + } + imageView.setImageBitmap(bitmap); + demoImage.setImageBitmap(bitmap); } - - private void addImageToFile(Bitmap image, File file) { - FileOutputStream fos = null; - //Tries to add the bitmap image to the file + } + //----------------------------VANILLA AND DEMOTIVATIONAL METHODS--------------------------// + + //Sets Vanilla meme editing view + public void vanillaM(View v) { + /* + * + * Again no need to set up views in every method + * + */ +// linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); +// editText = (EditText) findViewById(R.id.editText); +// editText2 = (EditText) findViewById(R.id.editText2); +// memeLayout = (RelativeLayout) findViewById(R.id.meme); +// demoImage = (ImageView) findViewById(R.id.demotivationalImage); + + + linearLayout2.setVisibility(View.VISIBLE); + imageView.setVisibility(View.VISIBLE); + editText.setVisibility(View.VISIBLE); + editText2.setVisibility(View.VISIBLE); + demoImage.setVisibility(View.INVISIBLE); + demoTitle.setVisibility(View.INVISIBLE); + demoText.setVisibility(View.INVISIBLE); + } + + //Sets demotivational meme editing view + public void demotivate(View v) { + + /* + * + * the same thing=))) Somebody really loves to play with all these layout elements. Once you set them up + * in onCreate they will be there. They never disappear=)))) + * + */ +// editText = (EditText) findViewById(R.id.editText); +// editText2 = (EditText) findViewById(R.id.editText2); +// memeLayout = (RelativeLayout) findViewById(R.id.meme); +// demoImage = (ImageView) findViewById(R.id.demotivationalImage); +// demoTitle = (EditText) findViewById(R.id.demotivationalTitle); +// demoText = (EditText) findViewById(R.id.demotivationalText); + + + memeLayout.setBackgroundColor(Color.BLACK); + imageView.setVisibility(View.INVISIBLE); + editText.setVisibility(View.INVISIBLE); + editText2.setVisibility(View.INVISIBLE); + demoImage.setVisibility(View.VISIBLE); + demoTitle.setVisibility(View.VISIBLE); + demoText.setVisibility(View.VISIBLE); + + } + + //-------------------------------IMAGE STORE AND SAVE METHODS-----------------------------// + + //onClick method for the save button. Calls other methods to create the save image function + + /* + * + * Great job creating separate methods for creating a file and getting Bitmap from the layout!!! + * + * + */ + public void storeImage(View v) { + editText.setHint(""); + editText2.setHint(""); + Bitmap image = getBitmapFromView(memeLayout); + File pictureFile = createImageFile(); + addImageToFile(image, pictureFile); + + } + + /** + * Create a File for saving an image + * Handles file name, and where to store it + */ + private File createImageFile() { + String timeStamp = new SimpleDateFormat("MMddyyyy_HHmmss").format(new Date()); + String imageName = "MEME_" + timeStamp + ".jpg"; + + File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); + File imageFile = new File(path, imageName); + Log.d("Path: ", path.getPath()); + return imageFile; + + } + + private void addImageToFile(Bitmap image, File file) { + FileOutputStream fos = null; + //Tries to add the bitmap image to the file + try { + fos = new FileOutputStream(file); + image.compress(Bitmap.CompressFormat.PNG, 90, fos); + + Toast.makeText(this, "Saved image to camera pictures", Toast.LENGTH_SHORT).show(); + + } catch (Exception e) { + Log.d(TAG, "trying to compress image did not work" + e.getMessage()); + } finally { try { - fos = new FileOutputStream(file); - image.compress(Bitmap.CompressFormat.PNG, 90, fos); - - Toast.makeText(this, "Saved image to camera pictures", Toast.LENGTH_SHORT).show(); - - } catch (Exception e) { - Log.d(TAG, "trying to compress image did not work" + e.getMessage()); - } finally { - try { - if (fos != null) { - fos.close(); //Closes the fileoutput stream - //Scans the image file that was just created so user can immediately see it in Pictures - try { - Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); - Uri uri = Uri.fromFile(file); - mediaScanIntent.setData(uri); - sendBroadcast(mediaScanIntent); - } catch (Exception e) { - Log.d("scanIntent", "Failed: "); - } + if (fos != null) { + fos.close(); //Closes the fileoutput stream + //Scans the image file that was just created so user can immediately see it in Pictures + try { + Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + Uri uri = Uri.fromFile(file); + mediaScanIntent.setData(uri); + sendBroadcast(mediaScanIntent); + } catch (Exception e) { + Log.d("scanIntent", "Failed: "); } - } catch (IOException e) { - Log.d(TAG, "fos did not close " + e.getMessage()); } + } catch (IOException e) { + Log.d(TAG, "fos did not close " + e.getMessage()); } - } - //------------------------------CREATE BITMAP FROM VIEW METHOD----------------------------// - - //Takes the current view and creates a bitmap representing that view. - public Bitmap getBitmapFromView(View view) { - Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(returnedBitmap); - - Drawable bgDrawable = view.getBackground(); - if (bgDrawable != null) - bgDrawable.draw(canvas); - else - canvas.drawColor(Color.WHITE); - view.draw(canvas); - return returnedBitmap; - } - - - //---------------------------------DRAGGING EDITTEXT METHODS------------------------------// - - // onTouch and onDrag work together to allow for views to be moved around within the layout - //to children of that layout - public boolean onTouch(View v, MotionEvent e) { - if (e.getAction() == MotionEvent.ACTION_DOWN) {//Action_Down means a pressed gesture had started, view has been set in motion - View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);//Creates an image that the system displays during the drag and drop operation. - v.startDrag(null, shadowBuilder, v, 0); - v.isInEditMode(); - v.setVisibility(View.INVISIBLE); - return true; - } else { - return false; - } - } - - - public boolean onDrag(View v, DragEvent e) { - if (e.getAction() == DragEvent.ACTION_DROP) {//if the shadow has been released within the view - View view = (View) e.getLocalState(); - ViewGroup from = (ViewGroup) view.getParent(); - - LinearLayout to = (LinearLayout) v; - - if((to.getResources().getInteger(Integer.valueOf(R.id.editText)) == (from.getResources().getInteger(Integer.valueOf(R.id.editText2))))) { - + } + /* + storing meme externally - Yuliya Kaleda + */ + public void saveMeme(Bitmap bm) { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + Toast.makeText(getApplicationContext(),"Media is mounted",Toast.LENGTH_LONG).show(); + File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); + File myFile = new File(root, FILE_EXTERNAL); + try { + Toast.makeText(getApplicationContext(),"FOS",Toast.LENGTH_LONG).show(); + FileOutputStream fos = new FileOutputStream(myFile); + bm.compress(Bitmap.CompressFormat.JPEG, 100, fos); + fos.flush(); + fos.close(); + //optional if the user wants to see the image in the folder + try { + Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + Uri uri = Uri.fromFile(myFile); + mediaScanIntent.setData(uri); + sendBroadcast(mediaScanIntent); + } catch (Exception e) { + Log.d("scanIntent", "Failed: "); } - from.removeView(view); - to.addView(view); - view.setVisibility(View.VISIBLE); - - - view.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); - - + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + Log.d(TAG, "fos did not close " + e.getMessage()); } - return true; - } - - - //----------------------------VANILLA EDITTEXT FONT SIZE METHODS--------------------------// - - - // Sets Vanilla font size to 10sp - public void setTen(View v) { - linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - - linearLayout2.setVisibility(View.GONE); - linearLayout3.setVisibility(View.VISIBLE); - editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); - editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); - } - - // Sets Vanilla font size to 15sp - public void setFifteen(View v) { - linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout2.setVisibility(View.GONE); - linearLayout3.setVisibility(View.VISIBLE); - editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); - editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); - } - - // Sets Vanilla font size to 20sp - public void setTwenty(View v) { - linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout2.setVisibility(View.GONE); - linearLayout3.setVisibility(View.VISIBLE); - editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); - editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); - } - - // Sets Vanilla font size to 25sp - public void setTwentyfive(View v) { - linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout2.setVisibility(View.GONE); - linearLayout3.setVisibility(View.VISIBLE); - editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25); - editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25); - } - - //---------------------------VANILLA EDITTEXT FONT COLOR METHODS--------------------------// - - // Sets Vanilla font to black - public void setBlack(View v) { - - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout3.setVisibility(View.GONE); - editText.setVisibility(View.VISIBLE); - editText2.setVisibility(View.VISIBLE); - } - - // Sets Vanilla font to white - public void setWhite(View v) { - - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout3.setVisibility(View.GONE); - editText.setVisibility(View.VISIBLE); - editText2.setVisibility(View.VISIBLE); - editText.setTextColor(Color.WHITE); - editText2.setTextColor(Color.WHITE); } - - // Sets Vanilla font to red - public void setRed(View v) { - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout3.setVisibility(View.GONE); - editText.setVisibility(View.VISIBLE); - editText2.setVisibility(View.VISIBLE); - editText.setTextColor(Color.RED); - editText2.setTextColor(Color.RED); + else { + Log.d(TAG_YULIYA, "External Storage is not available"); } - - // Sets Vanilla font to blue - public void setBlue(View v) { - - linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); - linearLayout3.setVisibility(View.GONE); - editText.setVisibility(View.VISIBLE); - editText2.setVisibility(View.VISIBLE); - editText.setTextColor(Color.BLUE); - editText2.setTextColor(Color.BLUE); + } + + //------------------------------CREATE BITMAP FROM VIEW METHOD----------------------------// + + //Takes the current view and creates a bitmap representing that view. + public Bitmap getBitmapFromView(View view) { + Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(returnedBitmap); + + Drawable bgDrawable = view.getBackground(); + if (bgDrawable != null) + bgDrawable.draw(canvas); + else + canvas.drawColor(Color.WHITE); + view.draw(canvas); + return returnedBitmap; + } + + + //---------------------------------DRAGGING EDITTEXT METHODS------------------------------// + + /* + * + * I was trying to add drag and drop functionality to our app, but could not figure out + * drop stage. Good to know. Thank you!!! + * + * + */ + // onTouch and onDrag work together to allow for views to be moved around within the layout + //to children of that layout + public boolean onTouch(View v, MotionEvent e) { + if (e.getAction() == MotionEvent.ACTION_DOWN) {//Action_Down means a pressed gesture had started, view has been set in motion + View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);//Creates an image that the system displays during the drag and drop operation. + v.startDrag(null, shadowBuilder, v, 0); + v.isInEditMode(); + v.setVisibility(View.INVISIBLE); + return true; + } else { + return false; } + } - //----------------------------------IMAGE EFFECTS METHODS---------------------------------// + public boolean onDrag(View v, DragEvent e) { + if (e.getAction() == DragEvent.ACTION_DROP) {//if the shadow has been released within the view + View view = (View) e.getLocalState(); + ViewGroup from = (ViewGroup) view.getParent(); - // Applies engrave effect to image - public void engravedImage(View view) { - if (getIntent().hasExtra("byteArray")) { - Bitmap engraved = ApplyFilters.engrave(b); - imageView.setImageBitmap(engraved); - demoImage.setImageBitmap(engraved); - } else { - Bitmap engraved = ApplyFilters.engrave(bitmap); - imageView.setImageBitmap(engraved); - demoImage.setImageBitmap(engraved); - } - Toast.makeText(this, "Engraved!", Toast.LENGTH_SHORT).show(); - } + LinearLayout to = (LinearLayout) v; - // Applies inverted colors effect to image - public void invertColors(View view) { - if (getIntent().hasExtra("byteArray")) { - Bitmap inverted = ApplyFilters.doInvert(b); - imageView.setImageBitmap(inverted); - demoImage.setImageBitmap(inverted); - } else { - Bitmap inverted = ApplyFilters.doInvert(bitmap); - imageView.setImageBitmap(inverted); - demoImage.setImageBitmap(inverted); + if ((to.getResources().getInteger(Integer.valueOf(R.id.editText)) == (from.getResources().getInteger(Integer.valueOf(R.id.editText2))))) { } - Toast.makeText(this,"Inverted!",Toast.LENGTH_SHORT).show(); + from.removeView(view); + to.addView(view); + view.setVisibility(View.VISIBLE); + view.requestFocus(); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } - - // Applies greyscale effect to image - public void greyscaleImage(View view) { - if (getIntent().hasExtra("byteArray")) { - Bitmap greyscaled = ApplyFilters.doGreyscale(b); - imageView.setImageBitmap(greyscaled); - demoImage.setImageBitmap(greyscaled); - } else { - Bitmap greyscaled = ApplyFilters.doGreyscale(bitmap); - imageView.setImageBitmap(greyscaled); - demoImage.setImageBitmap(greyscaled); - } - Toast.makeText(this,"Old School Flow!",Toast.LENGTH_SHORT).show(); - } - - // Applies blue shading effect to image - public void shadingFilterBlue(View view) { - if (getIntent().hasExtra("byteArray")) { - Bitmap blueShade = ApplyFilters.applyShadingFilter(b, Color.BLUE); - imageView.setImageBitmap(blueShade); - demoImage.setImageBitmap(blueShade); - } else { - Bitmap blueShade = ApplyFilters.applyShadingFilter(bitmap, Color.BLUE); - imageView.setImageBitmap(blueShade); - demoImage.setImageBitmap(blueShade); - } - Toast.makeText(this,"BLUE!",Toast.LENGTH_SHORT).show(); - } - - // Applies red shading effect to image - public void shadingFilterRed(View view) { - if (getIntent().hasExtra("byteArray")) { - Bitmap redShade = ApplyFilters.applyShadingFilter(b, Color.RED); - imageView.setImageBitmap(redShade); - demoImage.setImageBitmap(redShade); - } else { - Bitmap redShade = ApplyFilters.applyShadingFilter(bitmap, Color.RED); - imageView.setImageBitmap(redShade); - demoImage.setImageBitmap(redShade); - } - Toast.makeText(this,"RED!",Toast.LENGTH_SHORT).show(); - } - - // Applies green shading effect to image - public void shadingFilterGreen(View view) { - - if (getIntent().hasExtra("byteArray")) { - Bitmap greenShade = ApplyFilters.applyShadingFilter(b, Color.GREEN); - imageView.setImageBitmap(greenShade); - demoImage.setImageBitmap(greenShade); - } else { - Bitmap greenShade = ApplyFilters.applyShadingFilter(bitmap, Color.GREEN); - imageView.setImageBitmap(greenShade); - demoImage.setImageBitmap(greenShade); - } - Toast.makeText(this,"GREEN FACES ONLY!!",Toast.LENGTH_SHORT).show(); - } - - // Applies reflection effect to image - public void reflectionEffect(View view) { - if (getIntent().hasExtra("byteArray")) { - Bitmap reflected = ApplyFilters.applyReflection(b); - imageView.setImageBitmap(reflected); - demoImage.setImageBitmap(reflected); - } else { - Bitmap reflected = ApplyFilters.applyReflection(bitmap); - imageView.setImageBitmap(reflected); - demoImage.setImageBitmap(reflected); - } - Toast.makeText(this,"Reflected!",Toast.LENGTH_SHORT).show(); + return true; + } + + + //----------------------------VANILLA EDITTEXT FONT SIZE METHODS--------------------------// + + /* + + The same thing..set up redundant views=) I commented them out + + + */ + // Sets Vanilla font size to 10sp + public void setTen(View v) { +// linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + + linearLayout2.setVisibility(View.GONE); + linearLayout3.setVisibility(View.VISIBLE); + editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); + editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); + } + + // Sets Vanilla font size to 15sp + public void setFifteen(View v) { +// linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout2.setVisibility(View.GONE); + linearLayout3.setVisibility(View.VISIBLE); + editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); + editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); + } + + // Sets Vanilla font size to 20sp + public void setTwenty(View v) { +// linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout2.setVisibility(View.GONE); + linearLayout3.setVisibility(View.VISIBLE); + editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); + editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); + } + + // Sets Vanilla font size to 25sp + public void setTwentyfive(View v) { +// linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2); +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout2.setVisibility(View.GONE); + linearLayout3.setVisibility(View.VISIBLE); + editText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25); + editText2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25); + } + + //---------------------------VANILLA EDITTEXT FONT COLOR METHODS--------------------------// + + // Sets Vanilla font to black + public void setBlack(View v) { + +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout3.setVisibility(View.GONE); + editText.setVisibility(View.VISIBLE); + editText2.setVisibility(View.VISIBLE); + } + + // Sets Vanilla font to white + public void setWhite(View v) { + + // linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout3.setVisibility(View.GONE); + editText.setVisibility(View.VISIBLE); + editText2.setVisibility(View.VISIBLE); + editText.setTextColor(Color.WHITE); + editText2.setTextColor(Color.WHITE); + } + + // Sets Vanilla font to red + public void setRed(View v) { +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout3.setVisibility(View.GONE); + editText.setVisibility(View.VISIBLE); + editText2.setVisibility(View.VISIBLE); + editText.setTextColor(Color.RED); + editText2.setTextColor(Color.RED); + } + + // Sets Vanilla font to blue + public void setBlue(View v) { + +// linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3); + linearLayout3.setVisibility(View.GONE); + editText.setVisibility(View.VISIBLE); + editText2.setVisibility(View.VISIBLE); + editText.setTextColor(Color.BLUE); + editText2.setTextColor(Color.BLUE); + } + + + //----------------------------------IMAGE EFFECTS METHODS---------------------------------// + + /* + * + * + + So much of the code was repeated in the next block. It would be easier to setOnClickListener dynamically + and do switch cases. I rewrote the whole block of code + In xml file I commented out onClick for every ImageView + * + * + */ + public void extraEffects() { + ImageView engrave = (ImageView) findViewById(R.id.engrave); + ImageView invert = (ImageView) findViewById(R.id.invert); + ImageView greyScale = (ImageView) findViewById(R.id.greyscale); + ImageView blueShade = (ImageView) findViewById(R.id.blueShade); + ImageView greenShade = (ImageView) findViewById(R.id.greenShade); + ImageView redShade = (ImageView) findViewById(R.id.redShade); + ImageView reflect = (ImageView) findViewById(R.id.reflect); + + ArrayList arrayList = new ArrayList(); + arrayList.add(engrave); + arrayList.add(invert); + arrayList.add(greyScale); + arrayList.add(blueShade); + arrayList.add(greenShade); + arrayList.add(redShade); + arrayList.add(reflect); + + for (ImageView im : arrayList) { + im.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (getIntent().hasExtra("byteArray")) { + switch (view.getId()) { + + case R.id.reflect: + Bitmap reflected = ApplyFilters.applyReflection(b); + imageView.setImageBitmap(reflected); + demoImage.setImageBitmap(reflected); + break; + case R.id.engrave: + Bitmap engraved = ApplyFilters.engrave(b); + imageView.setImageBitmap(engraved); + demoImage.setImageBitmap(engraved); + break; + case R.id.invert: + Bitmap invert = ApplyFilters.doInvert(b); + imageView.setImageBitmap(invert); + demoImage.setImageBitmap(invert); + break; + case R.id.greyscale: + Bitmap greySc = ApplyFilters.doGreyscale(b); + imageView.setImageBitmap(greySc); + demoImage.setImageBitmap(greySc); + break; + case R.id.redShade: + Bitmap redShade = ApplyFilters.applyShadingFilter(b, Color.RED); + imageView.setImageBitmap(redShade); + demoImage.setImageBitmap(redShade); + break; + case R.id.greenShade: + Bitmap green = ApplyFilters.applyShadingFilter(b, Color.GREEN); + imageView.setImageBitmap(green); + demoImage.setImageBitmap(green); + break; + case R.id.blueShade: + Bitmap blue = ApplyFilters.applyShadingFilter(b, Color.BLUE); + imageView.setImageBitmap(blue); + demoImage.setImageBitmap(blue); + break; + } + } else { + switch (view.getId()) { + case R.id.reflect: + Bitmap reflected = ApplyFilters.applyReflection(bitmap); + imageView.setImageBitmap(reflected); + demoImage.setImageBitmap(reflected); + break; + + case R.id.engrave: + Bitmap engraved = ApplyFilters.engrave(bitmap); + imageView.setImageBitmap(engraved); + demoImage.setImageBitmap(engraved); + break; + case R.id.invert: + Bitmap inverted = ApplyFilters.doInvert(bitmap); + imageView.setImageBitmap(inverted); + demoImage.setImageBitmap(inverted); + break; + case R.id.greyscale: + Bitmap greyscaled = ApplyFilters.doGreyscale(bitmap); + imageView.setImageBitmap(greyscaled); + demoImage.setImageBitmap(greyscaled); + break; + case R.id.redShade: + Bitmap redShade = ApplyFilters.applyShadingFilter(bitmap, Color.RED); + imageView.setImageBitmap(redShade); + demoImage.setImageBitmap(redShade); + break; + case R.id.greenShade: + Bitmap greenShade = ApplyFilters.applyShadingFilter(bitmap, Color.GREEN); + imageView.setImageBitmap(greenShade); + demoImage.setImageBitmap(greenShade); + break; + case R.id.blueShade: + Bitmap blueShade = ApplyFilters.applyShadingFilter(bitmap, Color.BLUE); + imageView.setImageBitmap(blueShade); + demoImage.setImageBitmap(blueShade); + break; + } + } + } + }); } - } \ No newline at end of file + } +} diff --git a/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/MainActivity.java b/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/MainActivity.java index 6aa7d96..acbfa86 100644 --- a/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/MainActivity.java +++ b/MemeProject/app/src/main/java/madelyntav/c4q/nyc/memeproject/MainActivity.java @@ -1,3 +1,4 @@ + package madelyntav.c4q.nyc.memeproject; import android.app.Activity; @@ -17,12 +18,21 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; - +/* +The refactored code by Yuliya Kaleda is included in the comments with /* + */ public class MainActivity extends ActionBarActivity { + + /* + it is a good convention to initialize static variables first + */ + private static final int REQUEST_IMAGE_CAPTURE = 1; + private static final int EXTERNAL_CONTENT_URI = 0; + private static final String BYTE_ARRAY = "byteArray"; + private ImageView mImageView; - static final int REQUEST_IMAGE_CAPTURE = 1; - static final int EXTERNAL_CONTENT_URI = 0; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -35,6 +45,11 @@ protected void onCreate(Bundle savedInstanceState) { mImageView.setVisibility(View.INVISIBLE); } + /* + * + * + * Why do you need this code? + public boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || @@ -43,11 +58,26 @@ public boolean isExternalStorageReadable() { } return false; } + * + * + */ public void usePic(View v) { Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, - android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); - startActivityForResult(choosePictureIntent, 0); + android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); + /* + * + * + using constants is always more preferable rather than just their values, they are created for across app usage; + instead of 0 it is better to use EXTERNAL_CONTENT_URI. + I added Null Pointer Check for choosePictureIntent to eliminate the possible error (the same as you did for takePictureIntent) + * + * + */ + + if (choosePictureIntent.resolveActivity(getPackageManager())!=null){ + startActivityForResult(choosePictureIntent, 0); + } } @@ -59,7 +89,8 @@ public void takePic(View v) { } - @Override // saves pic and sends it to editPhoto activity + @Override + // saves pic and sends it to editPhoto activity protected void onActivityResult(int requestCode, int resultCode, Intent data) { Intent intent = new Intent(MainActivity.this, EditPhoto.class); @@ -69,7 +100,10 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { Bitmap imageBitmap = (Bitmap) extras.get("data"); ByteArrayOutputStream bs = new ByteArrayOutputStream(); imageBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs); - intent.putExtra("byteArray", bs.toByteArray()); + /* + for "byteArray" create a static variable + */ + intent.putExtra(BYTE_ARRAY, bs.toByteArray()); startActivity(intent); @@ -79,7 +113,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { // get Uri from selected image Uri targetUri = data.getData(); - Bitmap bitmap = null; + + // There is no necessity to convert image into Bitmap, because in your intent to the EditPhoto Activity + //you are sending Uri. Plus you don't need a bitmap to set ImageView, you can just use URI. Redundant code + /* + * + * + * Bitmap bitmap = null; ContentResolver cr = getContentResolver(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); @@ -89,13 +129,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } catch (IOException e) { e.printStackTrace(); } + * + * + */ //pass image to intent intent.putExtra("image", targetUri); - mImageView.setImageBitmap(bitmap); + mImageView.setImageURI(targetUri); //changed to setImageURI startActivity(intent); } } - -} \ No newline at end of file +}