Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#09B8C1"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.app.MainActivity">
- <com.app.color.DrawingView
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:id="@+id/drawing"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:background="#FFFFFFFF"
- android:layout_above="@+id/settings"
- android:layout_alignParentTop="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="changeColor"
- android:text="@string/Settings"
- android:id="@+id/settings"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Clear"
- android:id="@+id/button5"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:onClick="clearScreen" />
- </RelativeLayout>
- package com.app.color;
- import android.content.Intent;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- DrawingView dv = new DrawingView();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void changeColor(View view)
- {
- Intent intent = new Intent(MainActivity.this, Colors.class);
- startActivity(intent);
- }
- public void clearScreen(View view)
- {
- }
- }
- package com.app.color;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- public class Colors extends AppCompatActivity {
- DrawingView dv = new DrawingView(,);
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_colors);
- }
- public void toWhite(View view)
- {
- dv.setColor("#ffffff");
- Intent intent = new Intent(Colors.this, MainActivity.class);
- startActivity(intent);
- }
- public void toYellow(View view)
- {
- dv.setColor("#ffff00");
- Intent intent = new Intent(Colors.this, MainActivity.class);
- startActivity(intent);
- }
- public void toBlue(View view)
- {
- dv.setColor("#0000ff");
- Intent intent = new Intent(Colors.this, MainActivity.class);
- startActivity(intent);
- }
- public void toRed(View view)
- {
- dv.setColor("#ff0000");
- Intent intent = new Intent(Colors.this, MainActivity.class);
- startActivity(intent);
- }
- public void toGreen(View view)
- {
- dv.setColor("#00ff00");
- Intent intent = new Intent(Colors.this, MainActivity.class);
- startActivity(intent);
- }
- public void toBlack(View view)
- {
- dv.setColor("#00000");
- Intent intent = new Intent(Colors.this, MainActivity.class);
- startActivity(intent);
- }
- }
- package com.app.color;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
- public class DrawingView extends View {
- private Paint paint = new Paint();
- private Path path = new Path();
- public DrawingView(Context context, AttributeSet attrs) {
- super(context, attrs);
- paint.setAntiAlias(true);
- paint.setStrokeWidth(5f);
- paint.setColor(Color.BLACK);
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeJoin(Paint.Join.ROUND);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawPath(path, paint);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // Get the coordinates of the touch event.
- float eventX = event.getX();
- float eventY = event.getY();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- // Set a new starting point
- path.moveTo(eventX, eventY);
- return true;
- case MotionEvent.ACTION_MOVE:
- // Connect the points
- path.lineTo(eventX, eventY);
- break;
- default:
- return false;
- }
- // Makes our view repaint and call onDraw
- invalidate();
- return true;
- }
- public void setColor(String newColor){
- invalidate();
- int paintColor = Color.parseColor(newColor);
- paint.setColor(paintColor);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.app.Colors">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Choose Color"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:layout_below="@+id/textView"
- android:onClick="toWhite"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:background="#ffffff"
- android:text="@string/white" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button2"
- android:layout_below="@+id/button"
- android:onClick="toBlue"
- android:layout_alignRight="@+id/button"
- android:layout_alignEnd="@+id/button"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:background="#153fc7"
- android:text="@string/blue" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/red"
- android:id="@+id/button3"
- android:layout_below="@+id/button2"
- android:onClick="toRed"
- android:layout_alignRight="@+id/button2"
- android:layout_alignEnd="@+id/button2"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:background="#f01010" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/yellow"
- android:id="@+id/button4"
- android:background="#f7e80f"
- android:layout_below="@+id/button3"
- android:onClick="toYellow"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_alignRight="@+id/button3"
- android:layout_alignEnd="@+id/button3" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/Green"
- android:id="@+id/button6"
- android:layout_below="@+id/button4"
- android:onClick="toGreen"
- android:layout_alignRight="@+id/button4"
- android:layout_alignEnd="@+id/button4"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:background="#00ff00" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/Black"
- android:id="@+id/button7"
- android:layout_below="@+id/button6"
- android:background="#000000"
- android:onClick="toBlack"
- android:textColor="#ffffff"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true" />
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement