Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#09B8C1"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="com.app.MainActivity">
  12.  
  13. <com.app.color.DrawingView
  14. android:layout_width="match_parent"
  15. android:layout_height="200dp"
  16. android:id="@+id/drawing"
  17. android:layout_marginLeft="5dp"
  18. android:layout_marginRight="5dp"
  19. android:background="#FFFFFFFF"
  20. android:layout_above="@+id/settings"
  21. android:layout_alignParentTop="true" />
  22.  
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:onClick="changeColor"
  27. android:text="@string/Settings"
  28. android:id="@+id/settings"
  29. android:layout_alignParentBottom="true"
  30. android:layout_alignParentRight="true"
  31. android:layout_alignParentEnd="true" />
  32.  
  33. <Button
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="Clear"
  37. android:id="@+id/button5"
  38. android:layout_alignParentBottom="true"
  39. android:layout_alignParentLeft="true"
  40. android:layout_alignParentStart="true"
  41. android:onClick="clearScreen" />
  42. </RelativeLayout>
  43.  
  44. package com.app.color;
  45.  
  46. import android.content.Intent;
  47. import android.graphics.Canvas;
  48. import android.graphics.Color;
  49. import android.support.v7.app.AppCompatActivity;
  50. import android.os.Bundle;
  51. import android.view.View;
  52.  
  53. public class MainActivity extends AppCompatActivity {
  54.  
  55. DrawingView dv = new DrawingView();
  56.  
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.activity_main);
  61. }
  62.  
  63. public void changeColor(View view)
  64. {
  65. Intent intent = new Intent(MainActivity.this, Colors.class);
  66. startActivity(intent);
  67. }
  68.  
  69. public void clearScreen(View view)
  70. {
  71.  
  72. }
  73.  
  74. }
  75.  
  76. package com.app.color;
  77.  
  78. import android.content.Intent;
  79. import android.support.v7.app.AppCompatActivity;
  80. import android.os.Bundle;
  81. import android.view.View;
  82.  
  83. public class Colors extends AppCompatActivity {
  84.  
  85. DrawingView dv = new DrawingView(,);
  86.  
  87. @Override
  88. protected void onCreate(Bundle savedInstanceState) {
  89. super.onCreate(savedInstanceState);
  90. setContentView(R.layout.activity_colors);
  91. }
  92.  
  93. public void toWhite(View view)
  94. {
  95. dv.setColor("#ffffff");
  96. Intent intent = new Intent(Colors.this, MainActivity.class);
  97. startActivity(intent);
  98. }
  99.  
  100. public void toYellow(View view)
  101. {
  102. dv.setColor("#ffff00");
  103. Intent intent = new Intent(Colors.this, MainActivity.class);
  104. startActivity(intent);
  105. }
  106.  
  107. public void toBlue(View view)
  108. {
  109. dv.setColor("#0000ff");
  110. Intent intent = new Intent(Colors.this, MainActivity.class);
  111. startActivity(intent);
  112. }
  113.  
  114. public void toRed(View view)
  115. {
  116. dv.setColor("#ff0000");
  117. Intent intent = new Intent(Colors.this, MainActivity.class);
  118. startActivity(intent);
  119. }
  120.  
  121. public void toGreen(View view)
  122. {
  123. dv.setColor("#00ff00");
  124. Intent intent = new Intent(Colors.this, MainActivity.class);
  125. startActivity(intent);
  126. }
  127.  
  128. public void toBlack(View view)
  129. {
  130. dv.setColor("#00000");
  131. Intent intent = new Intent(Colors.this, MainActivity.class);
  132. startActivity(intent);
  133. }
  134. }
  135.  
  136. package com.app.color;
  137.  
  138. import android.content.Context;
  139. import android.graphics.Canvas;
  140. import android.graphics.Color;
  141. import android.graphics.Paint;
  142. import android.graphics.Path;
  143. import android.util.AttributeSet;
  144. import android.view.MotionEvent;
  145. import android.view.View;
  146.  
  147. public class DrawingView extends View {
  148. private Paint paint = new Paint();
  149. private Path path = new Path();
  150.  
  151. public DrawingView(Context context, AttributeSet attrs) {
  152. super(context, attrs);
  153.  
  154. paint.setAntiAlias(true);
  155. paint.setStrokeWidth(5f);
  156. paint.setColor(Color.BLACK);
  157. paint.setStyle(Paint.Style.STROKE);
  158. paint.setStrokeJoin(Paint.Join.ROUND);
  159. }
  160.  
  161. @Override
  162. protected void onDraw(Canvas canvas) {
  163. canvas.drawPath(path, paint);
  164. }
  165.  
  166. @Override
  167. public boolean onTouchEvent(MotionEvent event) {
  168. // Get the coordinates of the touch event.
  169. float eventX = event.getX();
  170. float eventY = event.getY();
  171.  
  172. switch (event.getAction()) {
  173. case MotionEvent.ACTION_DOWN:
  174. // Set a new starting point
  175. path.moveTo(eventX, eventY);
  176. return true;
  177. case MotionEvent.ACTION_MOVE:
  178. // Connect the points
  179. path.lineTo(eventX, eventY);
  180. break;
  181. default:
  182. return false;
  183. }
  184.  
  185. // Makes our view repaint and call onDraw
  186. invalidate();
  187. return true;
  188. }
  189.  
  190. public void setColor(String newColor){
  191. invalidate();
  192. int paintColor = Color.parseColor(newColor);
  193. paint.setColor(paintColor);
  194. }
  195. }
  196.  
  197. <?xml version="1.0" encoding="utf-8"?>
  198. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  199. xmlns:tools="http://schemas.android.com/tools"
  200. android:layout_width="match_parent"
  201. android:layout_height="match_parent"
  202. android:paddingBottom="@dimen/activity_vertical_margin"
  203. android:paddingLeft="@dimen/activity_horizontal_margin"
  204. android:paddingRight="@dimen/activity_horizontal_margin"
  205. android:paddingTop="@dimen/activity_vertical_margin"
  206. tools:context="com.app.Colors">
  207.  
  208. <TextView
  209. android:layout_width="wrap_content"
  210. android:layout_height="wrap_content"
  211. android:textAppearance="?android:attr/textAppearanceLarge"
  212. android:text="Choose Color"
  213. android:id="@+id/textView"
  214. android:layout_alignParentTop="true"
  215. android:layout_centerHorizontal="true" />
  216.  
  217. <Button
  218. android:layout_width="wrap_content"
  219. android:layout_height="wrap_content"
  220. android:id="@+id/button"
  221. android:layout_below="@+id/textView"
  222. android:onClick="toWhite"
  223. android:layout_alignParentLeft="true"
  224. android:layout_alignParentStart="true"
  225. android:layout_alignParentRight="true"
  226. android:layout_alignParentEnd="true"
  227. android:background="#ffffff"
  228. android:text="@string/white" />
  229.  
  230. <Button
  231. android:layout_width="wrap_content"
  232. android:layout_height="wrap_content"
  233. android:id="@+id/button2"
  234. android:layout_below="@+id/button"
  235. android:onClick="toBlue"
  236. android:layout_alignRight="@+id/button"
  237. android:layout_alignEnd="@+id/button"
  238. android:layout_alignParentLeft="true"
  239. android:layout_alignParentStart="true"
  240. android:background="#153fc7"
  241. android:text="@string/blue" />
  242.  
  243. <Button
  244. android:layout_width="wrap_content"
  245. android:layout_height="wrap_content"
  246. android:text="@string/red"
  247. android:id="@+id/button3"
  248. android:layout_below="@+id/button2"
  249. android:onClick="toRed"
  250. android:layout_alignRight="@+id/button2"
  251. android:layout_alignEnd="@+id/button2"
  252. android:layout_alignParentLeft="true"
  253. android:layout_alignParentStart="true"
  254. android:background="#f01010" />
  255.  
  256. <Button
  257. android:layout_width="wrap_content"
  258. android:layout_height="wrap_content"
  259. android:text="@string/yellow"
  260. android:id="@+id/button4"
  261. android:background="#f7e80f"
  262. android:layout_below="@+id/button3"
  263. android:onClick="toYellow"
  264. android:layout_alignParentLeft="true"
  265. android:layout_alignParentStart="true"
  266. android:layout_alignRight="@+id/button3"
  267. android:layout_alignEnd="@+id/button3" />
  268.  
  269. <Button
  270. android:layout_width="wrap_content"
  271. android:layout_height="wrap_content"
  272. android:text="@string/Green"
  273. android:id="@+id/button6"
  274. android:layout_below="@+id/button4"
  275. android:onClick="toGreen"
  276. android:layout_alignRight="@+id/button4"
  277. android:layout_alignEnd="@+id/button4"
  278. android:layout_alignParentLeft="true"
  279. android:layout_alignParentStart="true"
  280. android:background="#00ff00" />
  281.  
  282. <Button
  283. android:layout_width="wrap_content"
  284. android:layout_height="wrap_content"
  285. android:text="@string/Black"
  286. android:id="@+id/button7"
  287. android:layout_below="@+id/button6"
  288. android:background="#000000"
  289. android:onClick="toBlack"
  290. android:textColor="#ffffff"
  291. android:layout_alignParentRight="true"
  292. android:layout_alignParentEnd="true"
  293. android:layout_alignParentLeft="true"
  294. android:layout_alignParentStart="true" />
  295.  
  296. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement