android - 如何在LinearLayout中获取ImageView的坐标和大小

标签 android android-layout android-canvas

当我点击按钮时,调用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++;
        }
    }

如何将Layout中的ImageView分配给tile,然后获取坐标和大小?

最佳答案

你可以试试这个:

//这是为了获取布局中固定大小的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中Image的大小,并且大小会有所不同
**//在您放置在 ImageView 中的图像上

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

关于android - 如何在LinearLayout中获取ImageView的坐标和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13357804/

相关文章:

android - 如何用颜色填充 Canvas

android - 在 Gingerbread 上将 ActionBarDrawerToggle 与 AppCompat 和 ActionBarActivity 一起使用时出现 VerifyError

Android 在 BottomNavigation 上制作重叠的 FloatingActionButton

android - 错误获取资源编号的值时没有包标识符

android - 显示为垂直线性布局的 iosched DashboardLayout 元素

Android Paint对象内存效率

android - Bugsense for ACRA 的任何替代品

android - 平台和设置说明?

java - `Can' t 在继承的 Activity 中创建处理程序...Looper.prepare()`

android - 使用面部检测 Android 进行面部交换