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 7824925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:45:52+00:00 2026-06-02T08:45:52+00:00

I have a custom View on which I draw an ArrayList of Coordinates (which

  • 0

I have a custom View on which I draw an ArrayList of Coordinates (which is a custom class just containing an x and y position). As more and more Coordinates are added to the ArrayList, the drawing noticeably slows down. I was wondering if there’s a more efficient way to draw this ArrayList, or alternatively, if I can just add the one Coordinate that was added (because the ArrayList only changes one Coordinate between calls to invalidate).

Here are the relevant pieces of code:

public class CustomDraw extends View {
// member variables

public void updateLine() {
    // grab new coordinates for each measure

    if(measure1.isEmpty()) {
        measure1.add(new Coordinate(0, 0));
    } else {
        Coordinate last_coord = measure1.get(measure1.size() - 1);

        // calculations for south, north, east, and west

        if(south && east) {
            measure1.add(new Coordinate(last_coord.x + 3, last_coord.y + 3));
        } else if(south && west) {
            measure1.add(new Coordinate(last_coord.x - 3, last_coord.y + 3));
        } else if(north && east) {
            measure1.add(new Coordinate(last_coord.x + 3, last_coord.y - 3));
        } else if(north && west) {
            measure1.add(new Coordinate(last_coord.x - 3, last_coord.y - 3));
        }
    }

    if(draw) {
        dh.sleep(10);
    }
}


@Override
public void onDraw(Canvas c) {
    super.onDraw(c);
    Paint p = new Paint();
    p.setStyle(Paint.Style.FILL);

    p.setColor(Color.WHITE);
    c.drawPaint(p);
    p.setColor(Color.BLACK);

    switch(mSelected) {
    case Constants.MEASURE_1:
        for(int i = 0; i < measure1.size(); i++) {
            Coordinate coord = measure1.get(i);
            Log.d("MAAV", "drawing coord.x, coord.y: " + (coord.x) + ", " + (coord.y));
            c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
        }
        break;
    }

}

class DrawHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        CustomDraw.this.updateLine();
        CustomDraw.this.invalidate();
    }

    public void sleep(long delayMillis) {
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
}
}

Thanks for any help!

  • 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-02T08:45:54+00:00Added an answer on June 2, 2026 at 8:45 am

    You are declaring coord each iteration of the loop. You don’t need to do this and allocations of memory for objects can be expensive. Move that outside the loop and simply reuse the object. Also try commenting out the log calls or only log on each 10th item.

    Coordinate coord;
    for(int i = 0; i < measure1.size(); i++) {
       coord = measure1.get(i) 
       if (i%10==0)
           Log.d("MAAV", "drawing coord.x, coord.y: " + (coord.x) + ", " + (coord.y));
       c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
    }
    

    If these updates don’t improve performance enough consider using OpenGL ES to do the drawing.

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

Sidebar

Related Questions

In my custom View which I have added to my viewControllers's view, I need
I have a custom View which I draw on bitmaps in the onDraw function.
I have a custom view onto which I draw some graphics from the drawrect
I have a custom view which I override the onDraw method to draw bitmaps
Please help me to turn off user interaction. I have a custom view which
I have a view with two subviews: A UIImageView A small Custom View which
I have a custom view with several NSTextField controls for which I want to
I have a GridView in which each row has a custom view. The grid
I have a custom component which consists of 2 text view and 4 toggle
I have implemented a custom list view which looks like the twitter timeline. adapter

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.