Question

I have made a simple android drawing application,In that i have taken different buttons for different colors,Now i want is that when i click on red button my pen color should be chaned to red.same as for diffrent colors..My code is as below,Please suggest me friends..

SingleTouch.java

package com.example.singletouch;

import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

public class SingleTouchView extends View {
    public static int width;
    public int height;
    public Bitmap mBitmap;
    public Canvas mCanvas;
    public Path mPath;
    public Paint mBitmapPaint;
    Context context;
     public Paint mPaint;
    public Paint circlePaint;
    public Path circlePath;

    public enum DrawingPens {
        PEN_1(6), PEN_2(4), PEN_3(2), PEN_4(1);

     public Paint mPaint;

        private DrawingPens(final int width) {
            mPaint = new Paint();

            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(width);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
        }

        Paint getPaint() {
            return mPaint;
        }
    }
    public enum DrawingColors{
        Black(Color.parseColor("#000000")),Blue(Color.parseColor("#0000FF")),Cofee(Color.parseColor("#D2691E")),Cyan(Color.parseColor("#00FFFF"))
        ,Fuchiya(Color.parseColor("#FF00FF")),Gray(Color.parseColor("#808080")),Green(Color.parseColor("#00FF00")),Indigo(Color.parseColor("#4B0082")),
        Khaki(Color.parseColor("#F0E68C")),Lavendar(Color.parseColor("#E6E6FA")),Magenta(Color.parseColor("#FF00FF")),Mango(Color.parseColor("#FF8C00"))
        ,Maroon(Color.parseColor("#800000")),Orange(Color.parseColor("#FFA500")),Pink(Color.parseColor("#FFC0CB")),Pista(Color.parseColor("#9ACD32")),
        Purple(Color.parseColor("#800080")),Red(Color.parseColor("#FF0000")),Tan(Color.parseColor("#0000A0")),Yellow(Color.parseColor("#FFD801"));
         public Paint mPaint;

            private DrawingColors(final int color) {
                mPaint = new Paint();

                mPaint.setAntiAlias(true);
                mPaint.setStrokeWidth(width);
                mPaint.setColor(color);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeJoin(Paint.Join.ROUND);
            }

            Paint getPaint() {
                return mPaint;
            }

    }

    public SingleTouchView(final Context context) {
        super(context);

        init(context);
    }

    public SingleTouchView(final Context context, final AttributeSet attrs) {
        super(context, attrs);

        init(context);
         mBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFFFF0000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(12);
    }

    public SingleTouchView(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);

        init(context);
    }

    private ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();

    private Path mCurrentPath;
    private Path mCurrentPath1;

    private void init(final Context context) {

        setPen(DrawingPens.PEN_1);


    }


    @Override
    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        for (Map.Entry<Path, DrawingPens> entry : mPaths) {
            canvas.drawPath(entry.getKey(), entry.getValue().getPaint());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        float eventX = me.getX();
        float eventY = me.getY();

        switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mCurrentPath.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            mCurrentPath.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            break;
        }

        invalidate();

        return true;
    }

    public void setPen(final DrawingPens pen) {

        mCurrentPath = new Path();
        mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingPens>(
                mCurrentPath, pen));
    }
    public void eraser() {
        // TODO Auto-generated method stub
          mPaint = new Paint();

           /* Toast.makeText(getContext(), "eraser", Toast.LENGTH_LONG).show();
            mPaint.setXfermode(null);
            mPaint.setAlpha(0x00FFFFFF);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));*/
       // invalidate();
    }
    public void setColor(final DrawingColors color){
        mCurrentPath = new Path();
        mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingColors>(
                mCurrentPath, color));

    }

}

main.java

 package com.example.singletouch;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
    ImageView pen, color;
    SingleTouchView mDrawView;
    RelativeLayout layout, layout1;
    ImageView remove;
    ImageView eraser;
    LinearLayout pens;
    HorizontalScrollView myplate;

    private Path mPath;

    public Canvas mCanvas;
    LinearLayout pen1, pen2, pen3, pen4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawView = (SingleTouchView) findViewById(R.id.myview);
        layout1 = (RelativeLayout) findViewById(R.id.layout1);
        layout = (RelativeLayout) findViewById(R.id.layout);
        pen = (ImageView) findViewById(R.id.pen);
        pens = (LinearLayout) findViewById(R.id.linear);
        pens.setVisibility(View.GONE);
        pen1 = (LinearLayout) findViewById(R.id.pen1);
        pen2 = (LinearLayout) findViewById(R.id.pen2);
        pen3 = (LinearLayout) findViewById(R.id.pen3);
        pen4 = (LinearLayout) findViewById(R.id.pen4);
        color = (ImageView) findViewById(R.id.color);
        myplate = (HorizontalScrollView) findViewById(R.id.myplate);

        eraser = (ImageView) findViewById(R.id.eraser);
        remove = (ImageView) findViewById(R.id.remove);
        /*
         * pen1.setOnClickListener(this); pen2.setOnClickListener(this);
         * pen3.setOnClickListener(this); pen4.setOnClickListener(this);
         */

        pen.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // layout.addView(mDrawView);
                pens.setVisibility(View.VISIBLE);

            }
        });
        pens.setVisibility(View.GONE);
        pen1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
                pens.setVisibility(View.GONE);

            }
        });
        pen2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
                pens.setVisibility(View.GONE);

            }
        });
        pen3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pens.setVisibility(View.GONE);
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);

            }
        });
        pen4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pens.setVisibility(View.GONE);
                mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);

            }
        });

        remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                layout.removeView(mDrawView);
                mDrawView = new SingleTouchView(MainActivity.this);
                layout.addView(mDrawView);
            }
        });

        eraser.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {



            }
        });

        color.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

    }

}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/linearLayout1" >

        <com.example.singletouch.SingleTouchView
            android:id="@+id/myview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/pen" />
    </RelativeLayout>

    <HorizontalScrollView
        android:id="@+id/myplate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:visibility="gone" >

        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <!-- > -->

            <Button
                android:id="@+id/red"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/red"
                android:padding="15dp" />

            <Button
                android:id="@+id/green"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/green"
                android:padding="15dp" />

            <Button
                android:id="@+id/blue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/blue"
                android:padding="15dp" />

            <Button
                android:id="@+id/cyan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/cyan"
                android:padding="15dp" />

            <Button
                android:id="@+id/yellow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/yellow"
                android:padding="15dp" />

            <Button
                android:id="@+id/orange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/orange"
                android:padding="15dp" />

            <Button
                android:id="@+id/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/black"
                android:padding="15dp" />

            <Button
                android:id="@+id/cofee"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/cofee"
                android:padding="15dp" />

            <Button
                android:id="@+id/fuchiya"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/fuchiya"
                android:padding="15dp" />

            <Button
                android:id="@+id/gray"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/gray"
                android:padding="15dp" />

            <Button
                android:id="@+id/indigo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/indigo"
                android:padding="15dp" />

            <Button
                android:id="@+id/khaki"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/khaki"
                android:padding="15dp" />

            <Button
                android:id="@+id/lavendar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/lavendar"
                android:padding="15dp" />

            <Button
                android:id="@+id/magenta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/magenta"
                android:padding="15dp" />

            <Button
                android:id="@+id/mango"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/mango"
                android:padding="15dp" />

            <Button
                android:id="@+id/maroon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/maroon"
                android:padding="15dp" />

            <Button
                android:id="@+id/pista"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/pista"
                android:padding="15dp" />

            <Button
                android:id="@+id/pink"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/pink"
                android:padding="15dp" />

            <Button
                android:id="@+id/purple"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/purple"
                android:padding="15dp" />
        </LinearLayout>
    </HorizontalScrollView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/menubar"
        android:padding="2dp"
        android:weightSum="4" >

        <ImageView
            android:id="@+id/pen"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:gravity="center"
            android:src="@drawable/pen" />

        <ImageView
            android:id="@+id/eraser"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:src="@drawable/eraser" />

        <ImageView
            android:id="@+id/color"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:src="@drawable/color" />

        <ImageView
            android:id="@+id/remove"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:src="@drawable/remove" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_alignParentLeft="true"
        android:background="#eeeeee"
        android:orientation="horizontal"
        android:visibility="gone"
        android:weightSum="4" >

        <LinearLayout
            android:id="@+id/pen1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/pen1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/pen2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:src="@drawable/pen2" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/pen3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/pen3" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/pen4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/pen4" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
Was it helpful?

Solution

If i understand your question you can extend FrameLayout and add to it the Button/ImageView with the pencil draw transparent.

With this you only should to change the FrameLayout background and can make whatever color button with the same View.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top