user1222353
user1222353

Reputation: 55

Android ImageView slow, is there a easy way to speed it up?

The following code draws a graph with differrent colors for each point (color selection is in (...) which is umimportant here).
I have this attached to an onTouchListener which changes the Vector which hold the variables x,y,z. One "onTouch" changes points then updates the view with updateView(). The size of the vector wich holds x is about 300 elements. The array which holds y is about 200. Which makes it 200*300 loops inside the updateView() method for every onTouch event. I tried to implement a wait method before the next onTouch is triggered, but its still to slow.

Any ideas ?

public updateView(){

        bitmap = Bitmap.createBitmap((int) width, height - nextToCurve.getHeight(),
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        imageView.setImageBitmap(bitmap);
        canvas.drawColor(Color.BLACK);
        (...)
        //x
        for(int i5=start;i5<stop;i++){
        //y
        for(int i=blockSizeSmall-minBlockSize;i>minBlockSize;i--){
                   //change color for z
                   (...)
                   canvas.drawLine(oldX, oldY, temp3, temp4, paint);
                   canvas.drawPoint(x,y, paint);
        }
        imageView.postInvalidate(); 
}

Upvotes: 1

Views: 4901

Answers (1)

BitBank
BitBank

Reputation: 8715

There appear to be 3 things in your code causing performance problems:

1) Your onTouch listener receives a lot of events for a single touch. Be careful to filter the events such that only visible movement/touch/untouch takes action. Movements of 0.2 pixels are typical and will waste lots of time.

2) Creating a bitmap in your update routine will waste lots of time. Also, bitmaps are not automatically released when they go out of scope, so you will quickly run out of memory. You need to explicitly recycle them. Create the (single) bitmap outside of your updateView method and re-use it.

3) Setting a new bitmap into an ImageView could also be a source of slowdown since you're updating the image frequently. Once you set a bitmap into an ImageView, you can update the bitmap and it will update the ImageView.

Upvotes: 1

Related Questions