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

  • Home
  • SEARCH
  • 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 8876803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:18:08+00:00 2026-06-14T19:18:08+00:00

I got an Activity that contains a CustomImage (extends View) on which i try

  • 0

I got an Activity that contains a CustomImage (extends View) on which i try to draw, i can see the lines but on my touchUp they disappear.
I set the paint like this:

paint = (CustomImage) findViewById(R.id.paint);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setXfermode(null);
mPaint.setAlpha(0xFF);
mPaint.setMaskFilter(null);
mPaint.setStrokeWidth(50);
paint.setPaint(mPaint);

This is my CustomImage class:

public class CustomImage extends View {

private Bitmap bitmap;

public Context context;

Paint paint = new Paint();

private Canvas canvas = new Canvas();

private Path mPath = new Path();

private float mX, mY;

private static final float TOUCH_TOLERANCE = 4;

private float screenDensity;

int height, width;

public CustomImage(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

public CustomImage(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public CustomImage(Context context) {
    super(context);
    init(context);
}

private void init(Context context) {
    this.context = context;
}

public void setImg(Context context, Canvas canvas, Bitmap bitmap, int width, int height) {
    this.bitmap = bitmap;
    canvas.drawBitmap(bitmap, 0, 0, null);
}

public void setPaint(Paint paint) {
    this.paint = paint;
}
public void getBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawPath(mPath, paint);

}

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);
    mPath.moveTo(mX, mY);
    canvas.drawPath(mPath, paint);
    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;
}
}

First i set an background image on the View. and then i should be able to draw over that picture. The app draws the lines but they disappear on touchup. How can i avoid that?

  • 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-14T19:18:09+00:00Added an answer on June 14, 2026 at 7:18 pm

    I made the bitmap mutable and then i applied Canvas canvas = new Canvas(bitmap);
    How i made the bitmap mutable:

       public void setBitmap(Bitmap bitmap, Bitmap.Config targetConfig) throws Exception {
        System.out.println("CustomImage.setBitmap()");
        File file = new File("/mnt/sdcard/sample/temp.txt");
        file.getParentFile().mkdirs();
        randomAccessFile = new RandomAccessFile(file, "rw");
        int bWidth = bitmap.getWidth();
        int bHeight = bitmap.getHeight();
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, bWidth*bHeight*4);
        bitmap.copyPixelsToBuffer(map);
        bitmap.recycle();
        this.bitmap = Bitmap.createBitmap(bWidth, bHeight, targetConfig);
        map.position(0);
        this.bitmap.copyPixelsFromBuffer(map);
        channel.close();
        randomAccessFile.close();
        //        this.bitmap = bitmap;
    
    
        // canvas = new Canvas();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an activity which has a gallery that is backed by an Adapter
I've got a custom view in which I need to draw two bitmaps, one
I got 4 activites that all include a xml-footer which contains 4 buttons (one
Got an activity that extends ListActivity . The list is backed up by a
I got an activity and a service that are binded. When the onServiceConnected is
I've got Activity A which fires up the Camera intent via: Intent intent =
I've got the following method... public class Image extends Activity { public void onCreate(Bundle
I am trying to implement a ListFragment with AsynchTaskLoader that contains datasource but I
i Have a Spinner on my activity that contains a list of String, it
I've got an android app, with a super-class that contains a layout that should

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.