1
我正在研究图像缩放。一切都很好。我试图在布局的中心显示矩阵图像。但我无法做到。Android:无法将矩阵imageview放置在屏幕中央?
我的XML是:
<FrameLayout
android:id="@+id/root1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/root"
>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="matrix"
android:src="@drawable/ic_launcher" >
</ImageView>
</FrameLayout>
而且我用从堆栈溢出,这个解决方案,但我没有得到中心放置的图像。图像对齐屏幕的左上角。请帮帮我。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
screenWidth = rel.getWidth();
screenHeight = rel.getHeight();
Log.e("",
"Image Width : " + image.getWidth() + " > "
+ image.getHeight());
Log.e("", "screen Width : " + screenWidth + " > " + screenHeight);
Matrix matrix = image.getImageMatrix();
RectF drawableRect = new RectF(0, 0, image.getWidth(),
image.getHeight());
RectF viewRect = new RectF(0, 0, screenWidth, screenHeight);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
image.setImageMatrix(matrix);
image.invalidate();
}
任何原因,你不使用'RelativeLayout'? –
我也使用了相对布局,但没有结果。@ XaverKapeller –
问题不在于ImageView的位置,而在于它的大小。你已经将其高度和宽度设置为'match_parent',这使得'ImageView'与包含它的布局一样大。改为使用'wrap_content'。 –