2

When I click the button,call drawDigit(),puzzleview is only a LinearLayout in Activity

protected void drawDigit(Canvas canvas, int digit) {
        // TODO Auto-generated method stub
        if(num < diff){
            int x = tiles.get(num).getScrollX();  // get the X coordinate of ImageView,it's 0
            int y = tiles.get(num).getScrollY();  // get the Y coordinate of ImageView,it's 0
            float height = tiles.get(num).getHeight(); // height is 0
            float width = tiles.get(num).getWidth();   // width is 0
            background.setTextSize(height * 0.75f);
            background.setStyle(Style.FILL);
            background.setTextScaleX(width/height);
            background.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(digit + "", x, y, background);
            //num++;
        }
    }

How can I assign the ImageView in Layout to tiles,then get the coordinate and size?

Charles
  • 50,943
  • 13
  • 104
  • 142
Li Che
  • 727
  • 10
  • 24
  • http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-the-bitmap. Have a look at the answer in the link , Should help you. – Raghunandan Nov 13 '12 at 08:54
  • What's `num`? What's `diff`? What's `tiles`? – cjk Nov 13 '12 at 08:56

1 Answers1

1

You can try this :

//This is for Getting ImageView size that is fixed in layout

int imageHeight;
int imageWidth;

ImageView imageViewObj = (ImageView)findViewById(R.id.image_view_id);
ViewTreeObserver treeObsObj = imageViewObj.getViewTreeObserver();
treeObsObj.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

    public boolean onPreDraw() {
    imageWidth= imageViewObj.getMeasuredWidth();
    imageHeight = imageViewObj.getMeasuredHeight();
    return true;
}
});

// Getting the size of the Image inside the ImageView and The size will differ based
**// on the image you placed inside the ImageView

ImageView imageObj = (ImageView)findViewById(R.id.image_view_id);
int imageWidth = imageObj.getWidth();
int imageHeight = imageObj.getHeight();
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84
  • ya,but the result is not right. it is not 0, the first is 120, and the second is 60. the second method is 0. how to calculate the coordinate? – Li Che Nov 15 '12 at 02:47