3
問題是關於android開發的,更確切地說是關於按鈕和cumstom的看法。 我在線性佈局和一個自定義視圖中使用四個按鈕來繪製圖像。 當我使用方法來做到這一點(我重寫onDraw()),一切正常,除非我的按鈕在按下它們時反應很慢。只需刪除onDraw函數就可以讓他們快速工作。 所以,我的問題是: 爲什麼那些按鈕工作緩慢?我只是找不出原因! 我是否必須在自定義視圖中使用自創的按鈕?Android開發 - 按鈕反應遲緩
如何解決這個問題?
Thsi是I類使用的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:我知道這碼心不是很漂亮,但此刻我只是試圖找出我需要的基本知識...
好吧,我用'Bitmap wall = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.t1);'和第二個圖像的等價物實現了我的圖像。然後在我的循環中,我使用'canvas.drawBitmap(wall,x,y,null);'來繪製。但是,無論如何,這並沒有多大幫助。它走得更快,是的,但還不夠。我的小費錯了嗎?還是其他的?我將這些更改添加到onDraw方法中。 – Jouh 2011-03-14 15:05:06
@Jou:那是顯而易見的優化。除此之外,使用Traceview來確定你的'onDraw()'方法的速度慢。 – CommonsWare 2011-03-14 16:05:53
非常感謝,關於使用traceview的提示非常好。我發現callng getMapInfo爲循環造成了麻煩。我想它每次循環都會創建一個mapArray []的實例。通過將整個字符串傳遞給onDraw方法來解決此問題:)) – Jouh 2011-03-15 07:55:39