其他分享
首页 > 其他分享> > Android开发 – 按钮反应缓慢

Android开发 – 按钮反应缓慢

作者:互联网

问题是关于android开发,更确切地说是关于按钮和cumstom视图.
我正在使用线性布局中的四个按钮和一个我绘制图像的自定义视图.
当我使用方法执行此操作时(我重写onDraw())一切正常,除了按下按钮时我的按钮反应很慢.只需删除onDraw函数,它们就可以快速运行.
所以,我的问题是:
为什么这些按钮工作得那么慢?我只是找不到原因!
我是否必须在自定义视图中使用自己创建的按钮?

以及如何解决这个问题?

Thsi是我使用onDraw方法的类:

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();   
    }               
}
}

这是我的主要课程:

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:我知道这段代码不是很漂亮,但此刻我只想弄清楚我需要的基础知识……

解决方法:

您的按钮没有响应,因为您在onDraw()方法的主应用程序线程上花费了太多时间.请缓存您的位图,而不是每次绘制时从闪存加载文件200次.

标签:android,custom-view,performance,button
来源: https://codeday.me/bug/20190827/1736794.html