我正在使用这个代码
onCreate
创建位图的活动方法
final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
String filePath = "image.png";
final Bitmap fullImage = getBitmapFromAsset(context.getApplicationContext(), filePath);
Matrix matrix = new Matrix();
RectF src = new RectF(0, 0, fullImage.getWidth(), fullImage.getHeight());
RectF dst = new RectF(0, 0, screenWidth/8f, screenWidth/8f);
matrix.setRectToRect(src, dst, ScaleToFit.CENTER);
mImage = Bitmap.createBitmap(fullImage, 0, 0, fullImage.getWidth(), fullImage.getHeight(), matrix, true);
位图的宽度应为父布局的1/8(这是一个宽度匹配的线性布局,它是第一个/根布局)。我会一直使用肖像模式(
android:screenOrientation="portrait"
我知道我可以等待布局膨胀,然后使用
getWidth()
和
getHeight()