怎么样,我在relativeLayout中有一个textview和一个imageview。 imageview从手机图库中获取位图,然后将其置于图像视图中,并在图像缩放到相应的屏幕时保持图像比例。我的问题是我的textview应该得到一个随机位置,该位置位于位图的顶部。我一直试图获得位图的正确宽度和高度用于某些数学运算,但我无法获得正确的宽度和高度。我的文字似乎总是超出位图。有人可以帮我这个吗?已经过了几天......
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
g.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
int dpHeight = (int) (displayMetrics.heightPixels / displayMetrics.density);
int dpWidth = (int) (displayMetrics.widthPixels / displayMetrics.density);
int widthMin = g.getWidth();
int widthMax = dpWidth;
int randomWidth = new Random().nextInt(widthMax - widthMin + 1) + widthMin;
int heightMin = g.getHeight();
int heightMax = dpHeight;
int randomHeight = new Random().nextInt(heightMax - heightMin + 1) + heightMin;
TextView test = (TextView) findViewById(R.id.textView11);
test.setText(String.valueOf(widthMax));
params.leftMargin = randomHeight;
params.topMargin = randomWidth - 40;