[英]Runtime error “x + width must be <= bitmap.width()” on some devices
我正在創建一個程序,該程序實際上是根據Rect和字形創建菜單的。 由於某種原因,此代碼可在模擬器和我的朋友手機上運行,但不適用於我的代碼。 我開始相信我的手機可能太舊了(屬於彗星系列)。 但是,像任何優秀的手機應用程序開發人員一樣,我希望我的應用程序可以在任何類型的android手機上運行,因此我正在非常徹底地研究此問題。 我收到一條錯誤消息:
由以下原因引起:java.lang.IllegalArgumentException:x + width必須為<= bitmap.width()
我唯一使用位圖的地方是在此處發布的字形類中:
package text;
import java.util.HashMap;
import java.util.Map;
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Glyphs {
private Bitmap bitmap;
private Map<Character, Bitmap> glyphs = new HashMap<Character, Bitmap>(62);
private int width;
private int height;
private char[] charactersL = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private char[] charactersU = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private char[] numbers = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public Glyphs(Bitmap bitmap){
super();
this.bitmap = bitmap;
this.width = 12;
this.height = 18;
for(int i = 0; i < 26; i++){
glyphs.put(charactersL[i], Bitmap.createBitmap(bitmap, 0 + (i * width), 0, width, height));
}
for(int i = 0; i < 26; i++){
glyphs.put(charactersU[i], Bitmap.createBitmap(bitmap, 0 + (i * width), 24, width, height));
}
for(int i = 0; i < 10; i++){
glyphs.put(numbers[i], Bitmap.createBitmap(bitmap, 0 + (i * width), 48, width, height));
}
}
public Bitmap getBitmap(){
return bitmap;
}
public void drawString(Canvas canvas, String text, int x, int y){
if(canvas == null){
//return a tag
}
for(int i = 0; i < text.length(); i++){
Character ch = text.charAt(i);
if(glyphs.get(ch) != null){
canvas.drawBitmap(glyphs.get(ch), x + (i * width), y, null);
}
}
}
}
但是,此程序在其他手機上也可以正常工作。 我不確定為什么它會在我的舊POS手機上崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.