android 缩放视图,当容器视图缩放为centerInside时,在android imageview中找到位图的位置...
我有一個包含自定義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
總結
以上是生活随笔為你收集整理的android 缩放视图,当容器视图缩放为centerInside时,在android imageview中找到位图的位置...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QString
- 下一篇: 机器学习--CART分类回归树