Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:22:53+00:00 2026-06-13T08:22:53+00:00

My code: public class MainActivity extends GraphicsActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

  • 0

My code:

public class MainActivity extends GraphicsActivity{    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));

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

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                       0.4f, 6, 3.5f);
        mPaint.setMaskFilter(mEmboss);

        //mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
    }

    private Paint       mPaint;
    private MaskFilter  mEmboss;

    public class MyView extends View {

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;

        public MyView(Context c) {
            super(c);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.t01);
            Display  display = getWindowManager().getDefaultDisplay();
            int height = display.getWidth();
            int width = display.getHeight();
            mBitmap = Bitmap.createScaledBitmap(bmp, height, width, true);
            //mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }

        @Override
        protected void onDraw(Canvas canvas) {

            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

            canvas.drawPath(mPath, mPaint);
        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up() {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        }
    }

}

My xml:

<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" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/t01"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

Now I want add the view in the activity to this xml and also I want to place this view over the layout such that the image must be seen and also the view. Actually I’m trying to draw inside the lines of the letter in the image. Something like tracing the letter.
And also I don’t want my canvas draw to exclude the outline of my Letter in the image. If it crosses, then I want to put some warning sound.
enter image description here
Please help me how to do this?
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:22:54+00:00Added an answer on June 13, 2026 at 8:22 am

    When I tried the answer given by MarvinLabs, I get Inflate Exception. So tried to write the code using java itself. My code that worked out for me:

    public class MainActivity extends GraphicsActivity{
    
        private Button clearButton;
        MyView signature;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            signature = new MyView(this);
    
            RelativeLayout myLayout = new RelativeLayout(this);
            myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    
            clearButton = new Button(this);
            clearButton.setText("Clear");
            clearButton.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // To clear the drawn lines with finger.
                    Intent intent = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                }
            });
    
            myLayout.addView(signature);
            myLayout.addView(clearButton);
    
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            clearButton.setLayoutParams(params);
    
            clearButton.bringToFront();
            this.setContentView(myLayout);
    
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xffffffff);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(12);
    
            mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                           0.4f, 6, 3.5f);
            mPaint.setMaskFilter(mEmboss);
    
        }
    
        private Paint       mPaint;
        private MaskFilter  mEmboss;
    
    
        public class MyView extends View {
    
            private static final float MINP = 0.25f;
            private static final float MAXP = 0.75f;
    
            private Bitmap  mBitmap;
            private Canvas  mCanvas;
            private Path    mPath;
            private Paint   mBitmapPaint;
    
            public MyView(Context c) {
                super(c);
    
                Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.t01);
                Display  display = getWindowManager().getDefaultDisplay();
                int height = display.getWidth();
                int width = display.getHeight();
                mBitmap = Bitmap.createScaledBitmap(bmp, height, width, true);
                mCanvas = new Canvas(mBitmap);
                mPath = new Path();
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            }
    
    
            @Override
            protected void onDraw(Canvas canvas) {
    
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    
                canvas.drawPath(mPath, mPaint);
            }
    
            private float mX, mY;
            private static final float TOUCH_TOLERANCE = 4;
    
            private void touch_start(float x, float y) {
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
            }
            private void touch_move(float x, float y) {
                float dx = Math.abs(x - mX);
                float dy = Math.abs(y - mY);
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                    mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                    mX = x;
                    mY = y;
                }
            }
            private void touch_up() {
                mPath.lineTo(mX, mY);
                // commit the path to our offscreen
                mCanvas.drawPath(mPath, mPaint);
                // kill this so we don't double draw
                mPath.reset();
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
    
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                        invalidate();
                        break;
                }
                return true;
            }
        }
    }
    

    Thanks for the support and help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public Class GUi(){ // More Code public void onClick(ClickEvent event) { LoginServer loginServer =new
I have the next code : public class AddPrinter extends ListActivity { @Override public
This is my code: public class MainActivity extends Activity { private ComponentName mService; private
findViewById returns null for EditText Java Code: public class MainActivity extends Activity { private
I have this code: MainActivity: public class MainActivity extends ActivityGroup In MainActivity, I use
Here is my code: public class MainActivity extends Activity { private static final int
Given this code: public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final
I have this code public class MainActivity extends Activity { private static final int
My title is pretty self explanatory. Here's my code: public class MainActivity extends Activity
Try this piece of code - public class WhitespaceTest { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.