Advertisement
Crenox

Painter Tutorial Android Studio Program

Jun 18th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.68 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="samkough.com.painter" >
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme" >
  10. <activity
  11. android:name=".MainActivity"
  12. android:screenOrientation="portrait"
  13. android:label="@string/app_name" >
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16.  
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. </activity>
  20. </application>
  21. <uses-permission android:name="android.permission.INTERNET" />
  22.  
  23. </manifest>
  24. -------------------------------------------------------------------------------------------------------------------------------
  25. <resources>
  26. <string name="app_name">Painter</string>
  27.  
  28. <string name="hello_world">Hello world!</string>
  29. <string name="action_settings">Settings</string>
  30. <string name="title_activity_about">AboutActivity</string>
  31. <string name="title_activity_gallery">GalleryActivity</string>
  32. <string name="start_new">New</string>
  33. <string name="brush">Brush</string>
  34. <string name="erase">Erase</string>
  35. <string name="save">Save</string>
  36. <string name="paint">Paint</string>
  37. </resources>
  38. -------------------------------------------------------------------------------------------------------------------------------
  39. <resources>
  40. <!-- Default screen margins, per the Android Design guidelines. -->
  41. <dimen name="activity_horizontal_margin">16dp</dimen>
  42. <dimen name="activity_vertical_margin">16dp</dimen>
  43. <!-- Brush sizes -->
  44. <dimen name="small_brush">10dp</dimen>
  45. <integer name="small_size">10</integer>
  46. <dimen name="medium_brush">20dp</dimen>
  47. <integer name="medium_size">20</integer>
  48. <dimen name="large_brush">30dp</dimen>
  49. <integer name="large_size">30</integer>
  50. </resources>
  51. -------------------------------------------------------------------------------------------------------------------------------
  52. <?xml version="1.0" encoding="utf-8"?>
  53. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  54. android:layout_width="match_parent" android:layout_height="match_parent">
  55. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  56. <item>
  57. <shape android:shape="rectangle" >
  58. <stroke
  59. android:width="4dp"
  60. android:color="#FF999999" />
  61. <solid android:color="#00000000" />
  62. <padding
  63. android:bottom="0dp"
  64. android:left="0dp"
  65. android:right="0dp"
  66. android:top="0dp" />
  67. </shape>
  68. </item>
  69. <item>
  70. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  71. <stroke
  72. android:width="4dp"
  73. android:color="#FF999999" />
  74. <solid android:color="#00000000" />
  75. <corners android:radius="10dp" />
  76. </shape>
  77. </item>
  78. </layer-list>
  79.  
  80. </LinearLayout>
  81. -------------------------------------------------------------------------------------------------------------------------------
  82. <?xml version="1.0" encoding="utf-8"?>
  83. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  84. android:layout_width="match_parent" android:layout_height="match_parent">
  85. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  86. <item>
  87. <shape android:shape="rectangle" >
  88. <stroke
  89. android:width="4dp"
  90. android:color="#FF333333" />
  91.  
  92. <solid android:color="#00000000" />
  93.  
  94. <padding
  95. android:bottom="0dp"
  96. android:left="0dp"
  97. android:right="0dp"
  98. android:top="0dp" />
  99. </shape>
  100. </item>
  101. <item>
  102. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  103. <stroke
  104. android:width="4dp"
  105. android:color="#FF333333" />
  106.  
  107. <solid android:color="#00000000" />
  108.  
  109. <corners android:radius="10dp" />
  110. </shape>
  111. </item>
  112. </layer-list>
  113. </LinearLayout>
  114.  
  115. -------------------------------------------------------------------------------------------------------------------------------
  116. package samkough.com.painter;
  117.  
  118. import android.graphics.Bitmap;
  119. import android.graphics.Canvas;
  120. import android.graphics.Color;
  121. import android.graphics.Paint;
  122. import android.graphics.Path;
  123. import android.view.MotionEvent;
  124. import android.content.Context;
  125. import android.util.AttributeSet;
  126. import android.view.View;
  127.  
  128. public class CanvasView extends View
  129. {
  130. //drawing path
  131. /*When the user touches the screen and moves their finger to draw, we will use a Path to trace their drawing action on the canvas*/
  132. private Path drawPath;
  133. //drawing and canvas paint
  134. /*Both the canvas and the drawing on top of it are represented by Paint objects*/
  135. private Paint drawPaint, canvasPaint;
  136. //initial color
  137. private int paintColor = 0xFF660000;
  138. //canvas
  139. private Canvas drawCanvas;
  140. //canvas bitmap
  141. private Bitmap canvasBitmap;
  142.  
  143. public CanvasView(Context context, AttributeSet attrs)
  144. {
  145. super(context, attrs);
  146. setupDrawing();
  147. }
  148.  
  149. //get drawing area setup for interaction
  150. private void setupDrawing()
  151. {
  152. drawPath = new Path();
  153. drawPaint = new Paint();
  154.  
  155. // initial path color
  156. drawPaint.setColor(paintColor);
  157.  
  158. // path properties
  159. drawPaint.setStrokeWidth(20);
  160. drawPaint.setStyle(Paint.Style.STROKE);
  161. // drawings appear smoother with these methods
  162. drawPaint.setAntiAlias(true);
  163. drawPaint.setStrokeJoin(Paint.Join.ROUND);
  164. drawPaint.setStrokeCap(Paint.Cap.ROUND);
  165.  
  166. // we set dithering by passing a parameter to the constructor
  167. canvasPaint = new Paint(Paint.DITHER_FLAG);
  168. }
  169.  
  170. // called when the custom View is assigned a size
  171. @Override
  172. protected void onSizeChanged(int w, int h, int oldw, int oldh)
  173. {
  174. super.onSizeChanged(w, h, oldw, oldh);
  175.  
  176. canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  177. drawCanvas = new Canvas(canvasBitmap);
  178. }
  179. // To allow the class to function as a custom drawing View, we also need to override the onDraw method
  180. @Override
  181. protected void onDraw(Canvas canvas) {
  182. // draws the canvas and the drawing path
  183. canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
  184. canvas.drawPath(drawPath, drawPaint);
  185. }
  186.  
  187. //detect user touch
  188. @Override
  189. public boolean onTouchEvent(MotionEvent event) {
  190. // x and y positions
  191. float touchX = event.getX();
  192. float touchY = event.getY();
  193.  
  194. // this allows us to respond to the up, down, and up touch events
  195. switch (event.getAction()) {
  196. case MotionEvent.ACTION_DOWN:
  197. drawPath.moveTo(touchX, touchY);
  198. break;
  199. case MotionEvent.ACTION_MOVE:
  200. drawPath.lineTo(touchX, touchY);
  201. break;
  202. case MotionEvent.ACTION_UP:
  203. drawCanvas.drawPath(drawPath, drawPaint);
  204. drawPath.reset();
  205. break;
  206. default:
  207. return false;
  208. }
  209.  
  210. //invalidates the View and returns a true value
  211. // Calling invalidate will cause the onDraw method to execute.
  212. invalidate();
  213. return true;
  214. }
  215.  
  216. //set color
  217. public void setColor(String newColor){
  218. invalidate();
  219. paintColor = Color.parseColor(newColor);
  220. drawPaint.setColor(paintColor);
  221. }
  222.  
  223. }
  224. -------------------------------------------------------------------------------------------------------------------------------
  225. package samkough.com.painter;
  226.  
  227. import android.app.Activity;
  228. import android.os.Bundle;
  229. import android.view.Menu;
  230. import android.view.MenuItem;
  231. import android.widget.ImageButton;
  232. import android.widget.LinearLayout;
  233. import android.view.View;
  234. import java.util.UUID;
  235. import android.provider.MediaStore;
  236. import android.app.AlertDialog;
  237. import android.app.Dialog;
  238. import android.content.DialogInterface;
  239. import android.view.View.OnClickListener;
  240. import android.widget.Toast;
  241.  
  242. public class MainActivity extends Activity implements OnClickListener {
  243.  
  244. private CanvasView drawView;
  245. private ImageButton currPaint, drawBtn;
  246. private float smallBrush, mediumBrush, largeBrush;
  247.  
  248. @Override
  249. protected void onCreate(Bundle savedInstanceState) {
  250. super.onCreate(savedInstanceState);
  251. setContentView(R.layout.activity_main);
  252.  
  253. drawView = (CanvasView) findViewById(R.id.drawing);
  254. LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
  255. currPaint = (ImageButton)paintLayout.getChildAt(0);
  256.  
  257. // currPaint.setImageDrawable(getResources().getDrawable(R.layout.paint_pressed));
  258. currPaint.setImageDrawable(ContextCompat.getDrawable(this, R.layout.paint_pressed));
  259.  
  260. smallBrush = getResources().getInteger(R.integer.small_size);
  261. mediumBrush = getResources().getInteger(R.integer.medium_size);
  262. largeBrush = getResources().getInteger(R.integer.large_size);
  263.  
  264. drawBtn = (ImageButton)findViewById(R.id.draw_btn);
  265.  
  266. drawBtn.setOnClickListener(this);
  267. }
  268.  
  269.  
  270. @Override
  271. public boolean onCreateOptionsMenu(Menu menu) {
  272. // Inflate the menu; this adds items to the action bar if it is present.
  273. getMenuInflater().inflate(R.menu.menu_main, menu);
  274. return true;
  275. }
  276.  
  277. @Override
  278. public boolean onOptionsItemSelected(MenuItem item) {
  279. // Handle action bar item clicks here. The action bar will
  280. // automatically handle clicks on the Home/Up button, so long
  281. // as you specify a parent activity in AndroidManifest.xml.
  282. int id = item.getItemId();
  283.  
  284. //noinspection SimplifiableIfStatement
  285. if (id == R.id.action_settings) {
  286. return true;
  287.  
  288. }
  289.  
  290. //use chosen color
  291. public void paintClicked(View view)
  292. {
  293. //update color
  294. if(view!=currPaint){
  295. ImageButton imgView = (ImageButton)view;
  296. String color = view.getTag().toString();
  297. drawView.setColor(color);
  298. //updates the UI to reflect the new chosen paint and set the previous one back to normal
  299. imgView.setImageDrawable(getResources().getDrawable(R.layout.paint_pressed));
  300. currPaint.setImageDrawable(getResources().getDrawable(R.layout.paint));
  301. currPaint=(ImageButton)view;
  302. }
  303. }
  304.  
  305. //respond to clicks
  306. @Override
  307. public void onClick(View view){
  308. //draw button clicked
  309. if(view.getId()==R.id.draw_btn){
  310. final Dialog brushDialog = new Dialog(this);
  311. brushDialog.setTitle("Brush size:");
  312. }
  313. }
  314.  
  315. return super.onOptionsItemSelected(item);
  316. }
  317. }
  318. -------------------------------------------------------------------------------------------------------------------------------
  319. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  320. xmlns:tools="http://schemas.android.com/tools"
  321. android:layout_width="match_parent"
  322. android:layout_height="match_parent"
  323. android:background="#FFCCCCCC"
  324. android:orientation="vertical"
  325. tools:context=".MainActivity" >
  326.  
  327. <LinearLayout
  328. android:layout_width="wrap_content"
  329. android:layout_height="50dp"
  330. android:layout_gravity="center"
  331. android:orientation="horizontal" >
  332. <ImageButton
  333. android:id="@+id/new_btn"
  334. android:layout_width="wrap_content"
  335. android:layout_height="fill_parent"
  336. android:contentDescription="@string/start_new"
  337. android:src="@drawable/new_pic" />
  338. <ImageButton
  339. android:id="@+id/draw_btn"
  340. android:layout_width="wrap_content"
  341. android:layout_height="fill_parent"
  342. android:contentDescription="@string/brush"
  343. android:src="@drawable/brush" />
  344. <ImageButton
  345. android:id="@+id/erase_btn"
  346. android:layout_width="wrap_content"
  347. android:layout_height="fill_parent"
  348. android:contentDescription="@string/erase"
  349. android:src="@drawable/eraser" />
  350. <ImageButton
  351. android:id="@+id/save_btn"
  352. android:layout_width="wrap_content"
  353. android:layout_height="fill_parent"
  354. android:contentDescription="@string/save"
  355. android:src="@drawable/save" />
  356.  
  357. </LinearLayout>
  358.  
  359. <samkough.com.painter.CanvasView
  360. android:id="@+id/drawing"
  361. android:layout_width="fill_parent"
  362. android:layout_height="0dp"
  363. android:layout_marginBottom="3dp"
  364. android:layout_marginLeft="5dp"
  365. android:layout_marginRight="5dp"
  366. android:layout_marginTop="3dp"
  367. android:layout_weight="1"
  368. android:background="#FFFFFFFF" />
  369.  
  370. <!-- color palette -->
  371. <LinearLayout
  372. android:layout_width="wrap_content"
  373. android:layout_height="wrap_content"
  374. android:layout_gravity="center"
  375. android:orientation="vertical" >
  376.  
  377. <!-- Top Row -->
  378. <LinearLayout
  379. android:id="@+id/paint_colors"
  380. android:layout_width="wrap_content"
  381. android:layout_height="wrap_content"
  382. android:orientation="horizontal" >
  383.  
  384. <ImageButton
  385. android:layout_width="@dimen/large_brush"
  386. android:layout_height="@dimen/large_brush"
  387. android:layout_margin="2dp"
  388. android:background="#FF660000"
  389. android:contentDescription="@string/paint"
  390. android:onClick="paintClicked"
  391. android:src="@layout/paint"
  392. android:tag="#FF660000" />
  393.  
  394. <ImageButton
  395. android:layout_width="@dimen/large_brush"
  396. android:layout_height="@dimen/large_brush"
  397. android:layout_margin="2dp"
  398. android:background="#FFFF0000"
  399. android:contentDescription="@string/paint"
  400. android:onClick="paintClicked"
  401. android:src="@layout/paint"
  402. android:tag="#FFFF0000" />
  403.  
  404. <ImageButton
  405. android:layout_width="@dimen/large_brush"
  406. android:layout_height="@dimen/large_brush"
  407. android:layout_margin="2dp"
  408. android:background="#FFFF6600"
  409. android:contentDescription="@string/paint"
  410. android:onClick="paintClicked"
  411. android:src="@layout/paint"
  412. android:tag="#FFFF6600" />
  413.  
  414. <ImageButton
  415. android:layout_width="@dimen/large_brush"
  416. android:layout_height="@dimen/large_brush"
  417. android:layout_margin="2dp"
  418. android:background="#FFFFCC00"
  419. android:contentDescription="@string/paint"
  420. android:onClick="paintClicked"
  421. android:src="@layout/paint"
  422. android:tag="#FFFFCC00" />
  423.  
  424. <ImageButton
  425. android:layout_width="@dimen/large_brush"
  426. android:layout_height="@dimen/large_brush"
  427. android:layout_margin="2dp"
  428. android:background="#FF009900"
  429. android:contentDescription="@string/paint"
  430. android:onClick="paintClicked"
  431. android:src="@layout/paint"
  432. android:tag="#FF009900" />
  433.  
  434. <ImageButton
  435. android:layout_width="@dimen/large_brush"
  436. android:layout_height="@dimen/large_brush"
  437. android:layout_margin="2dp"
  438. android:background="#FF009999"
  439. android:contentDescription="@string/paint"
  440. android:onClick="paintClicked"
  441. android:src="@layout/paint"
  442. android:tag="#FF009999" />
  443. </LinearLayout>
  444.  
  445. <!-- Bottom Row -->
  446. <LinearLayout
  447. android:layout_width="wrap_content"
  448. android:layout_height="wrap_content"
  449. android:orientation="horizontal" >
  450.  
  451. <ImageButton
  452. android:layout_width="@dimen/large_brush"
  453. android:layout_height="@dimen/large_brush"
  454. android:layout_margin="2dp"
  455. android:background="#FF0000FF"
  456. android:contentDescription="@string/paint"
  457. android:onClick="paintClicked"
  458. android:src="@layout/paint"
  459. android:tag="#FF0000FF" />
  460.  
  461. <ImageButton
  462. android:layout_width="@dimen/large_brush"
  463. android:layout_height="@dimen/large_brush"
  464. android:layout_margin="2dp"
  465. android:background="#FF990099"
  466. android:contentDescription="@string/paint"
  467. android:onClick="paintClicked"
  468. android:src="@layout/paint"
  469. android:tag="#FF990099" />
  470.  
  471. <ImageButton
  472. android:layout_width="@dimen/large_brush"
  473. android:layout_height="@dimen/large_brush"
  474. android:layout_margin="2dp"
  475. android:background="#FFFF6666"
  476. android:contentDescription="@string/paint"
  477. android:onClick="paintClicked"
  478. android:src="@layout/paint"
  479. android:tag="#FFFF6666" />
  480.  
  481. <ImageButton
  482. android:layout_width="@dimen/large_brush"
  483. android:layout_height="@dimen/large_brush"
  484. android:layout_margin="2dp"
  485. android:background="#FFFFFFFF"
  486. android:contentDescription="@string/paint"
  487. android:onClick="paintClicked"
  488. android:src="@layout/paint"
  489. android:tag="#FFFFFFFF" />
  490.  
  491. <ImageButton
  492. android:layout_width="@dimen/large_brush"
  493. android:layout_height="@dimen/large_brush"
  494. android:layout_margin="2dp"
  495. android:background="#FF787878"
  496. android:contentDescription="@string/paint"
  497. android:onClick="paintClicked"
  498. android:src="@layout/paint"
  499. android:tag="#FF787878" />
  500.  
  501. <ImageButton
  502. android:layout_width="@dimen/large_brush"
  503. android:layout_height="@dimen/large_brush"
  504. android:layout_margin="2dp"
  505. android:background="#FF000000"
  506. android:contentDescription="@string/paint"
  507. android:onClick="paintClicked"
  508. android:src="@layout/paint"
  509. android:tag="#FF000000" />
  510. </LinearLayout>
  511.  
  512. </LinearLayout>
  513.  
  514. <!--<View
  515. android:layout_width="match_parent"
  516. android:layout_height="700px"
  517. class="samkough.com.painter.CanvasView"
  518. android:id="@+id/canvasView"
  519. android:background="#ffffff"
  520. android:layout_centerVertical="true"
  521. android:layout_alignParentEnd="true" />-->
  522.  
  523. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement