Android 图像背景根据屏幕大小而不使用九块补丁
我有一个图像背景,就像squareup图像的第一个图像: 我需要在每一个大小的屏幕上显示这个图像背景(填充父屏幕) 我该怎么办?由于图像的几何问题,我不能使用九块补丁。我需要制作所有尺寸的图像吗 谢谢 在代码中(而不是通过布局XML),您可以在将位图缩放到足够大以进行裁剪后,通过裁剪图像来创建具有正确纵横比(设备高度x宽度)的位图。您需要确保图像可以放大/缩小(最好是缩小),而不会丢失清晰度。您还需要确保使用不同的纵横比剪裁图像时不会丢失重要信息 生成位图后,将其作为ImageView的内容放置在显示器上 我发现最好将徽标的大小与基础图像分开,并将图像视图层叠在一起,以便文本保持清晰 我创建了ImageView类的一个子类来封装大小调整和裁剪。唯一的value方法是重写的onMeasure()方法:Android 图像背景根据屏幕大小而不使用九块补丁,android,image,background,nine-patch,Android,Image,Background,Nine Patch,我有一个图像背景,就像squareup图像的第一个图像: 我需要在每一个大小的屏幕上显示这个图像背景(填充父屏幕) 我该怎么办?由于图像的几何问题,我不能使用九块补丁。我需要制作所有尺寸的图像吗 谢谢 在代码中(而不是通过布局XML),您可以在将位图缩放到足够大以进行裁剪后,通过裁剪图像来创建具有正确纵横比(设备高度x宽度)的位图。您需要确保图像可以放大/缩小(最好是缩小),而不会丢失清晰度。您还需要确保使用不同的纵横比剪裁图像时不会丢失重要信息 生成位图后,将其作为ImageView的内容放置
/**
*根据需要重写onMeasure方法以调整位图的大小
*/
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
Drawable currentDrawable=this.getDrawable();
位图可绘制位图可绘制;
if(BitmapDrawable.class.isInstance(currentDrawable)){
//我们有一个位图要处理
BitmapDrawable=(BitmapDrawable)currentDrawable;
Bitmap currentBitmap=BitMapDrawable.getBitmap();
位图resizedBitmap=null;
如果(currentBitmap!=null){
int currentHeight=currentBitmap.getHeight();
int currentWidth=currentBitmap.getWidth();
int parentWidth=MeasureSpec.getSize(widthmasurespec);
int parentHeight=MeasureSpec.getSize(heightMeasureSpec);
if((currentHeight!=parentHeight)| |(currentWidth!=parentWidth)){
//位图需要调整大小和/或裁剪以适合
if((currentHeightparentHeight)和(&(currentWidth>parentWidth)){
//需要使启动屏幕位图变小
浮动高度因子=(浮动)父高度/(浮动)当前高度;
浮动宽度因子=(浮动)父宽度/(浮动)当前宽度;
浮点比例因子;
//选择最大的因素
if(浮动比较(高度因子、宽度因子)<0){
scaleFactor=宽度因子;
}否则{
比例因子=高度因子;
}
int dstWidth=(int)(currentWidth*scaleFactor);
int-dstwheight=(int)(当前高度*缩放因子);
if(dstWidth
@user420574-您需要像在公共类WidgetSelfSizingImageView扩展ImageView中那样扩展ImageView类{
/**
* Override the onMeasure method to resize the Bitmap as needed
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Drawable currentDrawable = this.getDrawable();
BitmapDrawable theBitmapDrawable;
if (BitmapDrawable.class.isInstance(currentDrawable)){
// We have a bitmap to work with
theBitmapDrawable = (BitmapDrawable) currentDrawable;
Bitmap currentBitmap = theBitmapDrawable.getBitmap();
Bitmap resizedBitmap = null;
if (currentBitmap != null) {
int currentHeight = currentBitmap.getHeight();
int currentWidth = currentBitmap.getWidth();
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
if ((currentHeight != parentHeight) || (currentWidth != parentWidth)) {
// The bitmap needs to be resized, and/or cropped to fit
if ((currentHeight < parentHeight) || (currentWidth < parentWidth)) {
// Need to make the bitmap larger
float heightFactor = (float) parentHeight / (float) currentHeight;
float widthFactor = (float) parentWidth / (float) currentWidth;
float scaleFactor;
// Choose the largest factor
if (Float.compare(heightFactor, widthFactor) < 0) {
scaleFactor = widthFactor;
} else {
scaleFactor = heightFactor;
}
int dstWidth = (int) (currentWidth * scaleFactor);
int dstHeight = (int) (currentHeight * scaleFactor);
if (dstWidth < parentWidth) dstWidth = parentWidth; // Deal with off by one rounding errors
if (dstHeight < parentHeight) dstHeight = parentHeight; // Deal with off by one rounding errors
resizedBitmap = Bitmap.createScaledBitmap(currentBitmap, dstWidth, dstHeight, true);
currentBitmap.recycle();
} else if ((currentHeight > parentHeight) && (currentWidth > parentWidth)){
// Need to make the splash screen bitmap smaller
float heightFactor = (float) parentHeight / (float) currentHeight;
float widthFactor = (float) parentWidth / (float) currentWidth;
float scaleFactor;
// Choose the largest factor
if (Float.compare(heightFactor, widthFactor) < 0) {
scaleFactor = widthFactor;
} else {
scaleFactor = heightFactor;
}
int dstWidth = (int) (currentWidth * scaleFactor);
int dstHeight = (int) (currentHeight * scaleFactor);
if (dstWidth < parentWidth) dstWidth = parentWidth; // Deal with off by one rounding errors
if (dstHeight < parentHeight) dstHeight = parentHeight; // Deal with off by one rounding errors
resizedBitmap = Bitmap.createScaledBitmap(currentBitmap, dstWidth, dstHeight, true);
currentBitmap.recycle();
} else {
// No need to resize the image - we'll just need to crop it
resizedBitmap = currentBitmap;
}
// Now crop the image so that it fits the aspect ratio of the screen
currentHeight = resizedBitmap.getHeight();
currentWidth = resizedBitmap.getWidth();
Bitmap newBitmap;
if ((currentHeight != parentHeight) || (currentWidth != parentWidth)) {
// Crop the image to fit exactly
int startX = (currentWidth - parentWidth)/2;
if (startX < 0) startX = 0; // Hmm!
int startY = (currentHeight - parentHeight)/2;
if (startY < 0) startY = 0; // Hmm! again
newBitmap = Bitmap.createBitmap(resizedBitmap, startX, startY, parentWidth, parentHeight);
resizedBitmap.recycle();
} else {
// The resized image is the exact right size
newBitmap = resizedBitmap;
}
this.setImageBitmap(newBitmap);
}
}
}
}