Advertisement
Crenox

Painter Android Studio Program

Jun 16th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.64 KB | None | 0 0
  1. "An android app that allows you to simply draw onto a canvas."
  2.  
  3. OBJECTIVES/HOW IT WORKS:
  4. - Once you start up the app, the canvas is right there with the buttons (save, gallery, about) right under it.
  5. - Have a palette to draw a picture using your finger on a canvas.
  6. - You'll be able to switch between the colors while drawing.
  7. - Have a 'save' button that enables you to save the picture and then it goes into a gallery.
  8. - Have a fully functioning gallery where all your saved pictures go, and you can share it to Facebook, Twitter, etc.
  9. - Have an about button that takes you to the about activity where it tells you the purpose of the program and the people that developed it.
  10.  
  11. BUTTONS:
  12. BOTTOM:
  13. - Save: Saves whatever is on the canvas and puts it into the gallery or your photos.
  14. - Reset: Resets the canvas.
  15. - About: Tells you about the app.
  16. TOP:
  17. - Red: Changes the paint to red. (#FF0000)
  18. - Yellow: Changes the paint to yellow. (#FFFF00)
  19. - Green: Changes the paint to green. (#00CC00)
  20. - Blue: Changes the paint to blue. (#0000FF)
  21. - Black: Changes the paint to black. (#000000)
  22. - Purple: Changes the paint to purple. (#CC00CC)
  23. - Orange: Changes the paint to orange. (#FF8000)
  24. - Erase: Changes the paint to white. (#FFFFFF)
  25. - Thickness Plus Button: Increases the thickness of the paintbrush.
  26. - Thickness Minus Button: Decreases the thickness of the paintbrush.
  27.  
  28. 3 Buttons
  29. 10 Image Buttons
  30. 13 Total Buttons
  31.  
  32. LINKS:
  33. http://code.tutsplus.com/tutorials/android-sdk-create-a-drawing-app-touch-interaction--mobile-19202
  34. http://examples.javacodegeeks.com/android/core/graphics/canvas-graphics/android-canvas-example/
  35. http://www.rapidtables.com/web/color/RGB_Color.htm
  36. http://developer.android.com/reference/android/graphics/Paint.html
  37. https://www.airpair.com/android/photo-gallery-android-studio-list-fragments
  38. http://www.c-sharpcorner.com/UploadFile/e14021/capture-image-from-camera-and-selecting-image-from-gallery-o/
  39. https://stackoverflow.com/questions/2283444/android-image-button
  40. https://stackoverflow.com/questions/13946936/how-to-gap-between-two-buttons-in-linearlayout
  41. -----------------------------------------------------------------------------------------------------------------------------
  42. <?xml version="1.0" encoding="utf-8"?>
  43. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  44. package="com.samkough.painter" >
  45.  
  46. <application
  47. android:allowBackup="true"
  48. android:icon="@mipmap/ic_launcher"
  49. android:label="@string/app_name"
  50. android:theme="@style/AppTheme" >
  51. <activity
  52. android:name=".MainActivity"
  53. android:label="@string/app_name" >
  54. <intent-filter>
  55. <action android:name="android.intent.action.MAIN" />
  56.  
  57. <category android:name="android.intent.category.LAUNCHER" />
  58. </intent-filter>
  59. </activity>
  60. <activity
  61. android:name=".AboutActivity"
  62. android:label="@string/title_activity_about" >
  63. </activity>
  64. </application>
  65. <uses-permission android:name="android.permission.INTERNET" />
  66. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  67. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  68. </manifest>
  69. -----------------------------------------------------------------------------------------------------------------------------
  70. apply plugin: 'com.android.application'
  71.  
  72. android {
  73. compileSdkVersion 22
  74. buildToolsVersion "22.0.1"
  75.  
  76. defaultConfig {
  77. applicationId "com.samkough.painter"
  78. minSdkVersion 22
  79. targetSdkVersion 22
  80. versionCode 1
  81. versionName "1.0"
  82. }
  83. buildTypes {
  84. release {
  85. minifyEnabled false
  86. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  87. }
  88. }
  89. }
  90.  
  91. dependencies {
  92. compile fileTree(dir: 'libs', include: ['*.jar'])
  93. compile 'com.squareup.picasso:picasso:2.5.2'
  94. }
  95. -----------------------------------------------------------------------------------------------------------------------------
  96. package samkough.com.painter;
  97.  
  98. import android.content.Context;
  99. import android.graphics.Bitmap;
  100. import android.graphics.Canvas;
  101. import android.graphics.Color;
  102. import android.graphics.Paint;
  103. import android.graphics.Path;
  104. import android.util.AttributeSet;
  105. import android.util.TypedValue;
  106. import android.view.MotionEvent;
  107. import android.view.View;
  108.  
  109. public class CanvasView extends View
  110. {
  111. /*
  112. * When the user touches the screen and moves their finger to draw,
  113. * we will use a Path to trace their drawing action on the canvas.
  114. * Both the canvas and the drawing on top of it are represented by Paint
  115. * objects. The initial paint color corresponds to the first color
  116. * in the palette we created last time, which will be initially selected
  117. * when the app launches. Finally we declare variables for the canvas
  118. * and bitmap - the user paths drawn with drawPaint will be drawn onto
  119. * the canvas, which is drawn with canvasPaint.
  120. * */
  121. //drawing paint
  122. private Paint paint = new Paint();
  123. // canvas paint
  124. private Paint canvasPaint = new Paint();
  125. //drawing path
  126. private Path path = new Path();
  127. // canvas
  128. private Canvas canvas = new Canvas();
  129. //canvas bitmap
  130. private Bitmap canvasBitmap;
  131. // brush size and pixel size
  132. private float brushSize, pixelAmount;
  133.  
  134. public CanvasView(Context context, AttributeSet attrs)
  135. {
  136. // Setting the anti-alias, stroke join and cap styles will make the user's drawings appear smoother.
  137. super(context, attrs);
  138. paint.setAntiAlias(true);
  139. paint.setStrokeWidth(5);
  140. paint.setColor(Color.BLACK);
  141. paint.setStyle(Paint.Style.STROKE);
  142. paint.setStrokeJoin(Paint.Join.ROUND);
  143. paint.setStrokeCap(Paint.Cap.ROUND);
  144. }
  145.  
  146. @Override
  147. protected void onSizeChanged(int w, int h, int oldw, int oldh)
  148. {
  149. super.onSizeChanged(w, h, oldw, oldh);
  150. canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  151. canvas = new Canvas(canvasBitmap);
  152. }
  153.  
  154. @Override
  155. protected void onDraw(Canvas drawCanvas)
  156. {
  157. drawCanvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
  158. drawCanvas.drawPath(path, paint);
  159. }
  160.  
  161. @Override
  162. public boolean onTouchEvent(MotionEvent e)
  163. {
  164. // get the coords of the touch event
  165. float eventX = e.getX();
  166. float eventY = e.getY();
  167.  
  168. switch (e.getAction()) {
  169. case MotionEvent.ACTION_DOWN:
  170. // set a new starting point
  171. path.moveTo(eventX, eventY);
  172. return true;
  173. case MotionEvent.ACTION_MOVE:
  174. // connect the points
  175. path.lineTo(eventX, eventY);
  176. break;
  177. default:
  178. return false;
  179. }
  180.  
  181. // makes you view repaint and call ondraw
  182. invalidate();
  183. return true;
  184. }
  185.  
  186. public void clearCanvas()
  187. {
  188. path.reset();
  189. invalidate();
  190. }
  191.  
  192. public void setStrokeWidth(float f)
  193. {
  194. pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, f, getResources().getDisplayMetrics());
  195. brushSize = pixelAmount;
  196.  
  197. paint.setAntiAlias(true);
  198. paint.setStyle(Paint.Style.STROKE);
  199. paint.setStrokeJoin(Paint.Join.ROUND);
  200. paint.setStrokeCap(Paint.Cap.ROUND);
  201. paint.setStrokeWidth(brushSize);
  202. invalidate();
  203. }
  204.  
  205. public void setColor(int p)
  206. {
  207. paint.setAntiAlias(true);
  208. paint.setStyle(Paint.Style.STROKE);
  209. paint.setStrokeJoin(Paint.Join.ROUND);
  210. paint.setStrokeCap(Paint.Cap.ROUND);
  211. paint.setColor(p);
  212. invalidate();
  213. }
  214. }
  215. -----------------------------------------------------------------------------------------------------------------------------
  216. package com.samkough.painter;
  217.  
  218. import android.app.Activity;
  219. import android.content.Intent;
  220. import android.graphics.Color;
  221. import android.os.Bundle;
  222. import android.view.Menu;
  223. import android.view.MenuItem;
  224. import android.view.View;
  225. import android.widget.Button;
  226. import android.widget.ImageButton;
  227.  
  228. public class MainActivity extends Activity {
  229.  
  230. private CanvasView canvasView;
  231. private int orange;
  232. private int purple;
  233. private float strokeWidth;
  234.  
  235. @Override
  236. protected void onCreate(Bundle savedInstanceState) {
  237. super.onCreate(savedInstanceState);
  238. setContentView(R.layout.activity_main);
  239.  
  240. canvasView = (CanvasView) findViewById(R.id.canvasView);
  241. orange = Color.rgb(250, 128, 0);
  242. purple = Color.rgb(128, 0, 128);
  243. strokeWidth = 0;
  244.  
  245. // REGULAR BUTTONS: save, about, reset
  246. Button saveB = (Button) findViewById(R.id.saveButton);
  247. Button aboutB = (Button) findViewById(R.id.aboutButton);
  248. Button resetB = (Button) findViewById(R.id.resetButton);
  249.  
  250. // IMAGE BUTTONS: red, blue, green, yellow, black, purple, orange, erase, brush thickness plus, brush thickness minus
  251. ImageButton redIb = (ImageButton) findViewById(R.id.redButton);
  252. ImageButton blueIb = (ImageButton) findViewById(R.id.blueButton);
  253. ImageButton greenIb = (ImageButton) findViewById(R.id.greenButton);
  254. ImageButton yellowIb = (ImageButton) findViewById(R.id.yellowButton);
  255. ImageButton blackIb = (ImageButton) findViewById(R.id.blackButton);
  256. ImageButton purpleIb = (ImageButton) findViewById(R.id.purpleButton);
  257. ImageButton orangeIb = (ImageButton) findViewById(R.id.orangeButton);
  258. ImageButton eraseIb = (ImageButton) findViewById(R.id.eraseButton);
  259. ImageButton plusIb = (ImageButton) findViewById(R.id.plusButton);
  260. ImageButton minusIb = (ImageButton) findViewById(R.id.minusButton);
  261.  
  262. minusIb.setOnClickListener(new View.OnClickListener() {
  263. @Override
  264. public void onClick(View v)
  265. {
  266. strokeWidth -= 2;
  267. canvasView.setStrokeWidth(strokeWidth);
  268. }
  269. });
  270.  
  271. plusIb.setOnClickListener(new View.OnClickListener() {
  272. @Override
  273. public void onClick(View v) {
  274. strokeWidth += 2;
  275. canvasView.setStrokeWidth(strokeWidth);
  276. }
  277. });
  278.  
  279. eraseIb.setOnClickListener(new View.OnClickListener() {
  280. @Override
  281. public void onClick(View v) {
  282. canvasView.setColor(Color.TRANSPARENT);
  283. }
  284. });
  285.  
  286. orangeIb.setOnClickListener(new View.OnClickListener() {
  287. @Override
  288. public void onClick(View v) {
  289. canvasView.setColor(orange);
  290. }
  291. });
  292.  
  293. purpleIb.setOnClickListener(new View.OnClickListener() {
  294. @Override
  295. public void onClick(View v) {
  296. canvasView.setColor(purple);
  297. }
  298. });
  299.  
  300. blackIb.setOnClickListener(new View.OnClickListener() {
  301. @Override
  302. public void onClick(View v) {
  303. canvasView.setColor(Color.BLACK);
  304. }
  305. });
  306.  
  307. yellowIb.setOnClickListener(new View.OnClickListener() {
  308. @Override
  309. public void onClick(View v) {
  310. canvasView.setColor(Color.YELLOW);
  311. }
  312. });
  313.  
  314. greenIb.setOnClickListener(new View.OnClickListener() {
  315. @Override
  316. public void onClick(View v) {
  317. canvasView.setColor(Color.GREEN);
  318. }
  319. });
  320.  
  321. blueIb.setOnClickListener(new View.OnClickListener() {
  322. @Override
  323. public void onClick(View v) {
  324. canvasView.setColor(Color.BLUE);
  325. }
  326. });
  327.  
  328. redIb.setOnClickListener(new View.OnClickListener() {
  329. @Override
  330. public void onClick(View v) {
  331. canvasView.setColor(Color.RED);
  332. }
  333. });
  334.  
  335. saveB.setOnClickListener(new View.OnClickListener() {
  336. @Override
  337. public void onClick(View v) {
  338. }
  339. });
  340.  
  341. aboutB.setOnClickListener(new View.OnClickListener() {
  342. @Override
  343. public void onClick(View v) {
  344. Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
  345.  
  346. startActivity(intent);
  347. }
  348. });
  349.  
  350. resetB.setOnClickListener(new View.OnClickListener() {
  351. @Override
  352. public void onClick(View v) {
  353. canvasView.clearCanvas();
  354. }
  355. });
  356. }
  357.  
  358.  
  359. @Override
  360. public boolean onCreateOptionsMenu(Menu menu) {
  361. // Inflate the menu; this adds items to the action bar if it is present.
  362. getMenuInflater().inflate(R.menu.menu_main, menu);
  363. return true;
  364. }
  365.  
  366. @Override
  367. public boolean onOptionsItemSelected(MenuItem item) {
  368. // Handle action bar item clicks here. The action bar will
  369. // automatically handle clicks on the Home/Up button, so long
  370. // as you specify a parent activity in AndroidManifest.xml.
  371. int id = item.getItemId();
  372.  
  373. //noinspection SimplifiableIfStatement
  374. if (id == R.id.action_settings) {
  375. return true;
  376. }
  377.  
  378. return super.onOptionsItemSelected(item);
  379. }
  380. }
  381. -----------------------------------------------------------------------------------------------------------------------------
  382. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  383. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  384. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  385. android:paddingRight="@dimen/activity_horizontal_margin"
  386. android:paddingTop="@dimen/activity_vertical_margin"
  387. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
  388. android:background="#B3B1B1">
  389.  
  390. <LinearLayout
  391. android:orientation="vertical"
  392. android:layout_width="match_parent"
  393. android:layout_height="match_parent"
  394. android:layout_alignParentTop="true"
  395. android:layout_alignParentStart="true"
  396. android:id="@+id/linearLayout">
  397.  
  398. <LinearLayout
  399. android:orientation="horizontal"
  400. android:layout_width="match_parent"
  401. android:layout_height="wrap_content"
  402. android:gravity="center"
  403. android:layout_gravity="center_horizontal">
  404.  
  405. <ImageButton
  406. android:layout_width="wrap_content"
  407. android:layout_height="wrap_content"
  408. android:src="@drawable/red_button"
  409. android:background="@null"
  410. android:padding="5dp"
  411. android:id="@+id/redButton" />
  412.  
  413. <ImageButton
  414. android:layout_width="wrap_content"
  415. android:layout_height="wrap_content"
  416. android:src="@drawable/blue_button"
  417. android:background="@null"
  418. android:padding="5dp"
  419. android:id="@+id/blueButton"
  420. android:layout_gravity="center_horizontal" />
  421.  
  422. <ImageButton
  423. android:layout_width="wrap_content"
  424. android:layout_height="wrap_content"
  425. android:padding="5dp"
  426. android:src="@drawable/green_button"
  427. android:background="@null"
  428. android:id="@+id/greenButton" />
  429.  
  430. <ImageButton
  431. android:layout_width="wrap_content"
  432. android:layout_height="wrap_content"
  433. android:src="@drawable/yellow_button"
  434. android:background="@null"
  435. android:padding="5dp"
  436. android:id="@+id/yellowButton" />
  437.  
  438. <ImageButton
  439. android:layout_width="wrap_content"
  440. android:layout_height="wrap_content"
  441. android:src="@drawable/orange_button"
  442. android:background="@null"
  443. android:id="@+id/orangeButton"
  444. android:padding="5dp"
  445. android:layout_gravity="right" />
  446.  
  447. <ImageButton
  448. android:layout_width="wrap_content"
  449. android:layout_height="wrap_content"
  450. android:src="@drawable/purple_button"
  451. android:background="@null"
  452. android:padding="5dp"
  453. android:id="@+id/purpleButton"
  454. android:layout_gravity="right" />
  455.  
  456. <ImageButton
  457. android:layout_width="wrap_content"
  458. android:layout_height="wrap_content"
  459. android:src="@drawable/black_button"
  460. android:background="@null"
  461. android:padding="5dp"
  462. android:id="@+id/blackButton"
  463. android:layout_gravity="center_horizontal" />
  464.  
  465. <ImageButton
  466. android:layout_width="wrap_content"
  467. android:layout_height="wrap_content"
  468. android:background="@null"
  469. android:padding="5dp"
  470. android:src="@drawable/eraser_button"
  471. android:id="@+id/eraseButton"
  472. android:layout_gravity="center_horizontal" />
  473.  
  474. <ImageButton
  475. android:layout_width="wrap_content"
  476. android:layout_height="wrap_content"
  477. android:padding="5dp"
  478. android:src="@drawable/minus_sign"
  479. android:background="@null"
  480. android:id="@+id/minusButton" />
  481.  
  482. <ImageButton
  483. android:layout_width="wrap_content"
  484. android:layout_height="wrap_content"
  485. android:src="@drawable/plus_sign"
  486. android:padding="5dp"
  487. android:background="@null"
  488. android:id="@+id/plusButton" />
  489.  
  490. </LinearLayout>
  491.  
  492. <com.samkough.painter.CanvasView
  493. android:layout_width="match_parent"
  494. android:layout_height="400dp"
  495. android:id="@+id/canvasView"
  496. android:background="#ffffff"
  497. android:layout_alignParentEnd="true"
  498. android:clickable="true" />
  499.  
  500. <LinearLayout
  501. android:orientation="horizontal"
  502. android:layout_width="match_parent"
  503. android:layout_height="wrap_content"
  504. android:id="@+id/buttonLayout"
  505. android:gravity="center"
  506. android:layout_alignParentTop="true"
  507. android:layout_alignParentStart="true">
  508.  
  509. <Button
  510. android:layout_width="wrap_content"
  511. android:layout_height="wrap_content"
  512. android:text="Save"
  513. android:id="@+id/saveButton" />
  514.  
  515. <Button
  516. android:layout_width="wrap_content"
  517. android:layout_height="wrap_content"
  518. android:text="About"
  519. android:id="@+id/aboutButton"/>
  520.  
  521. <Button
  522. android:layout_width="wrap_content"
  523. android:layout_height="wrap_content"
  524. android:text="Reset"
  525. android:id="@+id/resetButton" />
  526. </LinearLayout>
  527.  
  528. </LinearLayout>
  529.  
  530. </RelativeLayout>
  531. -----------------------------------------------------------------------------------------------------------------------------
  532. package com.samkough.painter;
  533.  
  534. import android.app.Activity;
  535. import android.os.Bundle;
  536. import android.view.Menu;
  537. import android.view.MenuItem;
  538.  
  539. public class AboutActivity extends Activity {
  540.  
  541. @Override
  542. protected void onCreate(Bundle savedInstanceState) {
  543. super.onCreate(savedInstanceState);
  544. setContentView(R.layout.activity_about);
  545. }
  546.  
  547.  
  548. @Override
  549. public boolean onCreateOptionsMenu(Menu menu) {
  550. // Inflate the menu; this adds items to the action bar if it is present.
  551. getMenuInflater().inflate(R.menu.menu_about, menu);
  552. return true;
  553. }
  554.  
  555. @Override
  556. public boolean onOptionsItemSelected(MenuItem item) {
  557. // Handle action bar item clicks here. The action bar will
  558. // automatically handle clicks on the Home/Up button, so long
  559. // as you specify a parent activity in AndroidManifest.xml.
  560. int id = item.getItemId();
  561.  
  562. //noinspection SimplifiableIfStatement
  563. if (id == R.id.action_settings) {
  564. return true;
  565. }
  566.  
  567. return super.onOptionsItemSelected(item);
  568. }
  569. }
  570. -----------------------------------------------------------------------------------------------------------------------------
  571. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  572. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  573. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  574. android:paddingRight="@dimen/activity_horizontal_margin"
  575. android:paddingTop="@dimen/activity_vertical_margin"
  576. android:paddingBottom="@dimen/activity_vertical_margin"
  577. android:background="#7A3737"
  578. tools:context="samkough.com.painter.AboutActivity">
  579.  
  580. <ScrollView
  581. android:layout_width="match_parent"
  582. android:layout_height="wrap_content"
  583. android:id="@+id/scrollView">
  584.  
  585. <LinearLayout
  586. android:orientation="vertical"
  587. android:layout_width="match_parent"
  588. android:layout_height="match_parent">
  589.  
  590. <ImageView
  591. android:layout_width="wrap_content"
  592. android:layout_height="wrap_content"
  593. android:id="@+id/paintLogo"
  594. android:src="@drawable/paint_palette"
  595. android:layout_below="@+id/scrollView"
  596. android:layout_centerHorizontal="true"
  597. android:layout_gravity="center_horizontal" />
  598.  
  599. <TextView
  600. android:layout_width="wrap_content"
  601. android:layout_height="250dp"
  602. android:textAppearance="?android:attr/textAppearanceLarge"
  603. android:text="Painter is a drawing app which allows you to draw onto a canvas and then save it onto your gallery. It gives you a variety of tools to work with as well."
  604. android:id="@+id/aboutText"
  605. android:gravity="center"
  606. android:fontFamily=""
  607. android:textColor="#FFFFFF"
  608. android:textSize="30dp"
  609. android:layout_marginTop="37dp"
  610. android:layout_below="@+id/paintLogo"
  611. android:layout_toEndOf="@+id/scrollView"
  612. android:layout_column="8" />
  613.  
  614. <ImageView
  615. android:layout_width="match_parent"
  616. android:layout_height="wrap_content"
  617. android:src="@drawable/profile_pic"
  618. android:id="@+id/profilePic" />
  619.  
  620. <TextView
  621. android:layout_width="wrap_content"
  622. android:layout_height="wrap_content"
  623. android:text="Sammy Samkough is a young ambitious programmer. With new ideas and new creations, he hopes to appease to the masses with his newfound apps."
  624. android:id="@+id/meText"
  625. android:gravity="center"
  626. android:fontFamily=""
  627. android:textColor="#FFFFFF"
  628. android:textSize="30dp"
  629. />
  630.  
  631. </LinearLayout>
  632. </ScrollView>
  633.  
  634. </RelativeLayout>
  635. -----------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement