Jouh
Jouh

Reputation: 74

Android Development - Buttons react slowly

Question is about android development, more exactly it is about buttons and cumstom views. I'm using four buttons in Linear Layouts and one custom view in which I draw images. When I use method to do this (I override onDraw() ) everything works just fine, except my buttons react quite slow on pressing them. Just removing the onDraw functions leaves them working quickly. So, my questions are: Why do those buttons work that slow? I just cant find out why! Do I have to use self created buttons in the custom view?

And how to solve this?

Thsi is the class I use the onDraw Method:

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;

public class test extends ImageView{  
Context mContext;
String[] medium;

final int pspawn[]={64,32};

public test(Context context, AttributeSet attrs) {         
    super(context, attrs);
    mContext = context;
}

private String getMapInfo(Integer counter){
    String[] mapArray = TextUtils.split(map, " ");
    return mapArray[counter];
}
public void onDraw(Canvas canvas){
    int x = 0;
    int y = 0;
    for(int i = 0; i<100; i = i+1)
    {
        String mapinfo = getMapInfo(i);
        if (mapinfo.equals("x"))
        {
            canvas.drawBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.t1), x, y, null);
        }
        x = x + 32;
        if (x == 320)
        {
            y = y + 32;
            x = 0;
        }
        canvas.drawBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.t3), pspawn[0], pspawn[1],null);
        invalidate();   
    }               
}
}

And this is my main class:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class desimain extends Activity{

private Thread worker;
private Runnable newMsg;
private OnClickListener getKeystroke;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getKeystroke = new OnClickListener(){
        public void onClick(View view) {
            switch(view.getId()){   
            case R.id.Up:
                worker = new Thread(newMsg);
                worker.start();
                break;   
            case R.id.Down:   
                Toast.makeText(getApplicationContext(), "Down", Toast.LENGTH_SHORT).show();     
                break;
            case R.id.Left:   
                Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();     
                break; 
            case R.id.Right:   
                Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();        
                break;
            }
        };
    };

    Button pressUp = (Button) findViewById (R.id.Up);
    pressUp.setOnClickListener(getKeystroke);
    Button pressDown = (Button) findViewById (R.id.Down);
    pressDown.setOnClickListener(getKeystroke);
    Button pressLeft = (Button) findViewById (R.id.Left);
    pressLeft.setOnClickListener(getKeystroke);
    Button pressRight = (Button) findViewById (R.id.Right);
    pressRight.setOnClickListener(getKeystroke);


    newMsg = new Runnable(){
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(), "Up", Toast.LENGTH_SHORT).show();       
                    } 
                }); 
        }   
    };
}
}

PS: I know this code isnt very beautiful, but at the moment i just try to figure out the basics I need...

Upvotes: 3

Views: 1775

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007124

Your buttons do not respond because you are taking up much too much time on the main application thread in your onDraw() method. Please cache your bitmaps rather than loading files from flash 200 times per draw.

Upvotes: 6

Related Questions