Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="samkough.com.painter" >
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:screenOrientation="portrait"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-permission android:name="android.permission.INTERNET" />
- </manifest>
- -------------------------------------------------------------------------------------------------------------------------------
- <resources>
- <string name="app_name">Painter</string>
- <string name="hello_world">Hello world!</string>
- <string name="action_settings">Settings</string>
- <string name="title_activity_about">AboutActivity</string>
- <string name="title_activity_gallery">GalleryActivity</string>
- <string name="start_new">New</string>
- <string name="brush">Brush</string>
- <string name="erase">Erase</string>
- <string name="save">Save</string>
- <string name="paint">Paint</string>
- </resources>
- -------------------------------------------------------------------------------------------------------------------------------
- <resources>
- <!-- Default screen margins, per the Android Design guidelines. -->
- <dimen name="activity_horizontal_margin">16dp</dimen>
- <dimen name="activity_vertical_margin">16dp</dimen>
- <!-- Brush sizes -->
- <dimen name="small_brush">10dp</dimen>
- <integer name="small_size">10</integer>
- <dimen name="medium_brush">20dp</dimen>
- <integer name="medium_size">20</integer>
- <dimen name="large_brush">30dp</dimen>
- <integer name="large_size">30</integer>
- </resources>
- -------------------------------------------------------------------------------------------------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <item>
- <shape android:shape="rectangle" >
- <stroke
- android:width="4dp"
- android:color="#FF999999" />
- <solid android:color="#00000000" />
- <padding
- android:bottom="0dp"
- android:left="0dp"
- android:right="0dp"
- android:top="0dp" />
- </shape>
- </item>
- <item>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
- <stroke
- android:width="4dp"
- android:color="#FF999999" />
- <solid android:color="#00000000" />
- <corners android:radius="10dp" />
- </shape>
- </item>
- </layer-list>
- </LinearLayout>
- -------------------------------------------------------------------------------------------------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <item>
- <shape android:shape="rectangle" >
- <stroke
- android:width="4dp"
- android:color="#FF333333" />
- <solid android:color="#00000000" />
- <padding
- android:bottom="0dp"
- android:left="0dp"
- android:right="0dp"
- android:top="0dp" />
- </shape>
- </item>
- <item>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
- <stroke
- android:width="4dp"
- android:color="#FF333333" />
- <solid android:color="#00000000" />
- <corners android:radius="10dp" />
- </shape>
- </item>
- </layer-list>
- </LinearLayout>
- -------------------------------------------------------------------------------------------------------------------------------
- package samkough.com.painter;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.view.MotionEvent;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- public class CanvasView extends View
- {
- //drawing path
- /*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*/
- private Path drawPath;
- //drawing and canvas paint
- /*Both the canvas and the drawing on top of it are represented by Paint objects*/
- private Paint drawPaint, canvasPaint;
- //initial color
- private int paintColor = 0xFF660000;
- //canvas
- private Canvas drawCanvas;
- //canvas bitmap
- private Bitmap canvasBitmap;
- public CanvasView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- setupDrawing();
- }
- //get drawing area setup for interaction
- private void setupDrawing()
- {
- drawPath = new Path();
- drawPaint = new Paint();
- // initial path color
- drawPaint.setColor(paintColor);
- // path properties
- drawPaint.setStrokeWidth(20);
- drawPaint.setStyle(Paint.Style.STROKE);
- // drawings appear smoother with these methods
- drawPaint.setAntiAlias(true);
- drawPaint.setStrokeJoin(Paint.Join.ROUND);
- drawPaint.setStrokeCap(Paint.Cap.ROUND);
- // we set dithering by passing a parameter to the constructor
- canvasPaint = new Paint(Paint.DITHER_FLAG);
- }
- // called when the custom View is assigned a size
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh)
- {
- super.onSizeChanged(w, h, oldw, oldh);
- canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- drawCanvas = new Canvas(canvasBitmap);
- }
- // To allow the class to function as a custom drawing View, we also need to override the onDraw method
- @Override
- protected void onDraw(Canvas canvas) {
- // draws the canvas and the drawing path
- canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
- canvas.drawPath(drawPath, drawPaint);
- }
- //detect user touch
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // x and y positions
- float touchX = event.getX();
- float touchY = event.getY();
- // this allows us to respond to the up, down, and up touch events
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- drawPath.moveTo(touchX, touchY);
- break;
- case MotionEvent.ACTION_MOVE:
- drawPath.lineTo(touchX, touchY);
- break;
- case MotionEvent.ACTION_UP:
- drawCanvas.drawPath(drawPath, drawPaint);
- drawPath.reset();
- break;
- default:
- return false;
- }
- //invalidates the View and returns a true value
- // Calling invalidate will cause the onDraw method to execute.
- invalidate();
- return true;
- }
- //set color
- public void setColor(String newColor){
- invalidate();
- paintColor = Color.parseColor(newColor);
- drawPaint.setColor(paintColor);
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- package samkough.com.painter;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.view.View;
- import java.util.UUID;
- import android.provider.MediaStore;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.view.View.OnClickListener;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener {
- private CanvasView drawView;
- private ImageButton currPaint, drawBtn;
- private float smallBrush, mediumBrush, largeBrush;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- drawView = (CanvasView) findViewById(R.id.drawing);
- LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
- currPaint = (ImageButton)paintLayout.getChildAt(0);
- // currPaint.setImageDrawable(getResources().getDrawable(R.layout.paint_pressed));
- currPaint.setImageDrawable(ContextCompat.getDrawable(this, R.layout.paint_pressed));
- smallBrush = getResources().getInteger(R.integer.small_size);
- mediumBrush = getResources().getInteger(R.integer.medium_size);
- largeBrush = getResources().getInteger(R.integer.large_size);
- drawBtn = (ImageButton)findViewById(R.id.draw_btn);
- drawBtn.setOnClickListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- //use chosen color
- public void paintClicked(View view)
- {
- //update color
- if(view!=currPaint){
- ImageButton imgView = (ImageButton)view;
- String color = view.getTag().toString();
- drawView.setColor(color);
- //updates the UI to reflect the new chosen paint and set the previous one back to normal
- imgView.setImageDrawable(getResources().getDrawable(R.layout.paint_pressed));
- currPaint.setImageDrawable(getResources().getDrawable(R.layout.paint));
- currPaint=(ImageButton)view;
- }
- }
- //respond to clicks
- @Override
- public void onClick(View view){
- //draw button clicked
- if(view.getId()==R.id.draw_btn){
- final Dialog brushDialog = new Dialog(this);
- brushDialog.setTitle("Brush size:");
- }
- }
- return super.onOptionsItemSelected(item);
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- <LinearLayout 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="#FFCCCCCC"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="50dp"
- android:layout_gravity="center"
- android:orientation="horizontal" >
- <ImageButton
- android:id="@+id/new_btn"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:contentDescription="@string/start_new"
- android:src="@drawable/new_pic" />
- <ImageButton
- android:id="@+id/draw_btn"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:contentDescription="@string/brush"
- android:src="@drawable/brush" />
- <ImageButton
- android:id="@+id/erase_btn"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:contentDescription="@string/erase"
- android:src="@drawable/eraser" />
- <ImageButton
- android:id="@+id/save_btn"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:contentDescription="@string/save"
- android:src="@drawable/save" />
- </LinearLayout>
- <samkough.com.painter.CanvasView
- android:id="@+id/drawing"
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_marginBottom="3dp"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:layout_marginTop="3dp"
- android:layout_weight="1"
- android:background="#FFFFFFFF" />
- <!-- color palette -->
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:orientation="vertical" >
- <!-- Top Row -->
- <LinearLayout
- android:id="@+id/paint_colors"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF660000"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF660000" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FFFF0000"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FFFF0000" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FFFF6600"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FFFF6600" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FFFFCC00"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FFFFCC00" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF009900"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF009900" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF009999"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF009999" />
- </LinearLayout>
- <!-- Bottom Row -->
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF0000FF"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF0000FF" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF990099"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF990099" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FFFF6666"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FFFF6666" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FFFFFFFF"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FFFFFFFF" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF787878"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF787878" />
- <ImageButton
- android:layout_width="@dimen/large_brush"
- android:layout_height="@dimen/large_brush"
- android:layout_margin="2dp"
- android:background="#FF000000"
- android:contentDescription="@string/paint"
- android:onClick="paintClicked"
- android:src="@layout/paint"
- android:tag="#FF000000" />
- </LinearLayout>
- </LinearLayout>
- <!--<View
- android:layout_width="match_parent"
- android:layout_height="700px"
- class="samkough.com.painter.CanvasView"
- android:id="@+id/canvasView"
- android:background="#ffffff"
- android:layout_centerVertical="true"
- android:layout_alignParentEnd="true" />-->
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement