【问题标题】:How can I get the coordinates and size of ImageView in LinearLayout如何在 LinearLayout 中获取 ImageView 的坐标和大小
【发布时间】:2012-11-18 03:57:30
【问题描述】:

当我点击按钮时,调用drawDigit(),puzzleview只是Activity中的一个LinearLayout

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++;
        }
    }

如何将布局中的ImageView 分配给图块,然后获取坐标和大小?

【问题讨论】:

标签: android android-layout android-canvas


【解决方案1】:

你可以试试这个:

//这是为了获取布局中固定的ImageView大小

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;
}
});

// 获取 ImageView 中图像的大小,并且大小会有所不同
**// 在您放置在 ImageView 中的图像上

ImageView imageObj = (ImageView)findViewById(R.id.image_view_id);
int imageWidth = imageObj.getWidth();
int imageHeight = imageObj.getHeight();

【讨论】:

  • 是的,但结果不对。不是0,第一个是120,第二个是60。第二种方法是0,坐标怎么计算?
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
相关资源
最近更新 更多