我试着从另一个论坛来解决这个问题.他们告诉ontouch()必须返回true;但它并不是更好.
我的课问题:
public class VIEwer extends VIEw implements OntouchListener{/** Constant tile wIDth. */public static final int TILEW = 32;/** Constant tile height. */public static final int TILEH = 70;/** Constant tile shift due to increased level. */public static final int TILESKEW = 7; /** Tile images. */private Bitmap[][] tileImages;/** Highlighted tile images. */private Bitmap[][] tileImagesHL;/** Board itself. */private Board b;private Tile[][][] tTile;private float screenWIDth = defines.getScreenWIDth();private float screenHeight = defines.getScreenHeight();private float offsetimg = defines.getoffsetimage();private float reScalePosX = defines.getReScalePosX();private float reScalePosY = defines.getReScalePosY();private boolean firstIter;/** Stores selected tiles */private ArrayList<Tile> selectedTiles;// = new ArrayList<Tile>();/** Stores highlighted tiles by Hint */private ArrayList<Tile> highlightedTiles;// = new ArrayList<Tile>();//////////////////////////////////////////////////private static RectF screen;private Images imgObj; public VIEwer(Context context) { super(context); // Todo auto-generated constructor stub imgObj = new Images(context); b = new MainActivity().board; defines.setScreenRender(1280,800); makeTileImages(Board.MAXGROUPS); }@OverrIDeprotected voID onDraw(Canvas canvas){ drawBackGround(imgObj.bg01,canvas); try{ tTile = b.getContent(); if(tTile==null || tileImages==null){ return; } drawTile(tTile,canvas); }catch (Exception e) { // Todo: handle exception }}@OverrIDepublic boolean ontouch(VIEw v,MotionEvent event) { // Todo auto-generated method stub Log.d("MydeBUG","YEHHHHHH"); Tile[][][] content = b.getContent(); for (int i = 0; i < content.length; i++) { for (int y = 0; y < content[i].length; y++) { for (int x = 0; x < content[i][y].length; x++) { processMouseClick(content,i,y,x,event); } } } return true;}public voID drawBackGround(Bitmap bitmapBG,Canvas canvas){ screen = new RectF(0.0f,0.0f,defines.getScreenWIDth(),defines.getScreenHeight()); canvas.drawBitmap(bitmapBG,null,screen,null);}private voID makeTileImages(int groups){ tileImages = loadTileset(groups,"unlit_"); tileImagesHL = loadTileset(groups,"lit_");}private Bitmap[][] loadTileset(int groups,String prefix){ Bitmap[][] tr = new Bitmap[groups][Board.GROUPSIZE]; try{ inputStream is = this.getResources().getAssets().open("Tiles/tiles.set"); BufferedReader f = new BufferedReader(new inputStreamReader(is)); String s = f.readline(); while (s!=null) { if (!s.startsWith("#")) { String[] tokens = s.split(" "); try{ int groupNum =Integer.parseInt(tokens[0]); for(int i =1; i<=(Board.GROUPSIZE);i++){ String pathPNG = "Tiles/"+prefix+tokens[i]+".png"; Bitmap img = imgObj.loadImage(pathPNG); img = imgObj.resizeBitmap(img,Tile.tileHeight,Tile.tileWIDth); //Bitmap img = loadImage(pathPNG); //Log.d("MydeBUG","PATH:"+pathPNG); tr[groupNum][i-1] = img; } }catch (Exception e) { // Todo: handle exception Log.d("MydeBUG","file error TIles/tile.set"); } } s = f.readline(); } }catch (Exception e) { // Todo: handle exception } return tr;}public voID drawTile(Tile[][][] content,Canvas canvas){ for (int i = 0; i < content.length; i++) { for (int y = 0; y < content[i].length; y++) { for (int x = 0; x < content[i][y].length; x++) { final Tile t = content[i][y][x]; if (t != null) { if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) { continue; } if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) { continue; } Bitmap image = tileImages[t.getValue()][t.getSubindex()]; if (b.free(t)) { image = tileImagesHL[t.getValue()][t.getSubindex()]; } /* Draw tile images to panel */ //canvas.drawBitmap(image,x*TILEW+TILEW/2+i*TILESKEW,(y+1)*TILEH/2-i*TILESKEW,null); canvas.drawBitmap(image,(x*TILEW+TILEW/2+i*TILESKEW)+200,((y+1)*TILEH/2-i*TILESKEW)+100,null); } } } }}/** A helper function for the VIEwer constructor which accompanIEs the mouseListener object on each tiles * @param content The current board's 3D array of positions * @param i The current tile's x position * @param y The current tile's y position * @param x The current tile's z position * @param me The mouseEvent for each tile */private voID processMouseClick(Tile[][][] content,int i,int y,int x,MotionEvent touchPos){ final Tile t = content[i][y][x]; if (t != null) { if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) { return; } if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) { return; } Bitmap image = tileImages[t.getValue()][t.getSubindex()]; if (b.free(t)) { image = tileImagesHL[t.getValue()][t.getSubindex()]; } // Rectangle representing space of tile final Rect rect = new Rect(x*TILEW+TILEW/2+i*TILESKEW,image.getWIDth(),image.getHeight()); //if ((rect.contains(me.getPoint())) && b.free(t) && t.isVisible()) { if ((rect.contains((int)touchPos.getX(),(int)touchPos.getY()) && b.free(t))){ t.wasClicked = true; firstIter = false; Log.d("MyDeBUG","Clicked ME"); //if (content[i][y][x].isVisible()){ //Add corresponding JLabel to panel // t.tLabel.setBounds(x*TILEW+TILEW/2+i*TILESKEW,image.getWIDth(null),image.getHeight(null)); // t.tLabel.setVisible(false); // t.tLabel.setborder(borderFactory.createlineborder(color.blue,2)); //} //valIDClick(t); } }}}解决方法 删除实现OntouchListener并覆盖该方法:
public boolean ontouch(MotionEvent event)
更新
当然是ontouchEvent,而不是ontouch
总结以上是内存溢出为你收集整理的android – Ontouch()不适用于自定义视图?全部内容,希望文章能够帮你解决android – Ontouch()不适用于自定义视图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)