我有一个包含自定义ImageView的RelativeLayout,scaleType =“centerInside”,我在一个位图中加载(通常小于imageView).如何获取位图绘制位置的顶部/左侧位置?我需要能够将addView放在相对于位图的位置顶部.
RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.scroll_scaled, container, false);
ContentImageView image = (ContentImageView) view.findViewById(R.id.base_page);
Bitmap bm = mInterfaceActivity.getPageImage(mPageNumber);
image.setImageBitmap(bm);`
布局文件scrolled_scaled
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:scaleType="centerInside"
android:id="@+id/base_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff00"
android:contentDescription="@string/product_page"
android:src="@android:drawable/ic_menu_report_image" >
解决方法:
你需要使用Drawable的界限自己做数学运算.
ImageView test = (ImageView) findViewById(R.id.base_page);
Rect bounds = test.getDrawable().getBounds();
int x = (test.getWidth() - bounds.right) / 2;
int y = (test.getHeight() - bounds.bottom) / 2;
首先,我们计算View中未被图像使用的空间.然后,由于它居中,额外的空间在图像之前和之后均匀分布,因此它将该长度的一半绘制到视图中.
这些数字与View的位置有关,但如果需要,可以添加视图X和Y.
标签:android,bitmap,imageview,relativelayout
来源: https://codeday.me/bug/20190529/1181660.html