gpt4 book ai didi

Android开发-按钮 react 慢

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:32 26 4
gpt4 key购买 nike

问题是关于 android 开发的,更确切地说是关于按钮和 cumstom View 。我在线性布局中使用了四个按钮,并在一个自定义 View 中绘制了图像。当我使用方法执行此操作时(我覆盖 onDraw() )一切正常,除了我的按钮在按下它们时 react 很慢。只需删除 onDraw 函数即可使它们快速运行。所以,我的问题是:为什么这些按钮工作得那么慢?我只是不知道为什么!我必须在自定义 View 中使用自己创建的按钮吗?

如何解决这个问题?

这是我使用 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开发-按钮 react 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5299997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com