Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获取LinearLayout中ImageView的坐标和大小_Android_Android Layout_Android Canvas - Fatal编程技术网

Android 如何获取LinearLayout中ImageView的坐标和大小

Android 如何获取LinearLayout中ImageView的坐标和大小,android,android-layout,android-canvas,Android,Android Layout,Android Canvas,当我单击按钮call drawDigit()时,puzzleview只是活动中的线性布局 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 Image

当我单击按钮call drawDigit()时,puzzleview只是活动中的线性布局

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++;
        }
    }
受保护的void drawDigit(画布,整数位){
//TODO自动生成的方法存根
if(num
如何将布局中的
ImageView
分配给平铺,然后获取坐标和大小?

您可以尝试以下方法:

//用于获取布局中固定的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();

. 看看链接中的答案,应该对你有帮助。什么是
num
?什么是
diff
?什么是
tiles
?是的,但结果不对。它不是0,第一个是120,第二个是60。第二种方法是0。如何计算坐标?