问题描述
我有一个包含一个自定义的ImageView的scaleType =centerInside,我在一个位图(通常比小的ImageView)加载RelativeLayout的。我怎样才能得到其中的位图绘制的顶部/左侧位置?我需要能够addView的顶部相对于位图。
一位置 RelativeLayout的观点=(RelativeLayout的)inflater.inflate(R.layout.scroll_scaled,集装箱,FALSE);
ContentImageView图像=(ContentImageView)view.findViewById(R.id.base_page);
位图BM = mInterfaceActivity.getPageImage(mPageNumber);
image.setImageBitmap(BM);`
scrolled_scaled布局文件
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< ImageView的
机器人:scaleType =centerInside
机器人:ID =@ + ID / base_page
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#FF00FF00
机器人:contentDescription =@字符串/ product_page
机器人:SRC =@机器人:可绘制/ ic_menu_report_image>
< / ImageView的>
< / RelativeLayout的>
您需要使用的。
ImageView的测试=(ImageView的)findViewById(R.id.base_page);
矩形边界= test.getDrawable()的getBounds()。
INT X =(test.getWidth() - bounds.right)/ 2;
INT Y =(test.getHeight() - bounds.bottom)/ 2;
首先,我们计算出是不是空间由图像使用。然后,因为它集中了额外的空间前,后的图像所以它是画一半长度的进查看
。
这些数字相对于视图的位置,但你可以添加意见X和Y,如果你需要你。
I have an RelativeLayout that contains a custom ImageView, the scaleType="centerInside", I load in a bitmap (usually smaller than the imageView). How can I get the top/left position of where the bitmap was drawn? I need to be able addView's on top a positions relative to the bitmap.
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);`
The layout file scrolled_scaled
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
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" >
</ImageView>
</RelativeLayout>
You'll need to do the math yourself using the bounds of the 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;
First we calculate the space in the View that is not being used by the image. Then since it is centered the extra space is evenly distributed before and after the image so it is draw half of that length into the View
.
These numbers are relative to the location of the View but you can add the views X and Y if you need you.
这篇关于查找位图的位置在android系统的ImageView当容器视图缩放centerInside的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!