gpt4 book ai didi

android - 适合屏幕的 Android Imageview 矩阵中心

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:03 25 4
gpt4 key购买 nike

你好,我在这个主题上使用适合屏幕的功能 Android image view matrix scale + translate

float imageWidth = imageDetail.getDrawable().getIntrinsicWidth();
float imageHeight = imageDetail.getDrawable().getIntrinsicHeight();
RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
RectF viewRect = new RectF(0, 0, imageDetail.getWidth(),
imageDetail.getHeight());
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
imageDetail.setImageMatrix(matrix);
imageDetail.invalidate();

它有效但不是第一次。当我启动应用程序时,它不起作用。图像不显示。当我点击时它起作用了。我做错了什么?感谢您的任何建议

最佳答案

您好,您应该将代码放在 onWindowFocusChanged 方法中。

@Override
public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

float imageWidth = image.getDrawable().getIntrinsicWidth();
float imageHeight = image.getDrawable().getIntrinsicHeight();
RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
RectF viewRect = new RectF(0, 0, image.getWidth(),
image.getHeight());
Matrix matrix = new Matrix();
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
image.setImageMatrix(matrix);
image.invalidate();
}

关于android - 适合屏幕的 Android Imageview 矩阵中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24381775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com