将传入图像调整为Android布局

将传入图像调整为Android布局,android,android-layout,android-imageview,android-image,Android,Android Layout,Android Imageview,Android Image,如何将来自http请求的任何大小的图像放入大小为100x100的ImageView中 android:layout_width="100dp" android:layout_height="100dp" 我希望避免失去准确性,我希望看到完全相同的图像,但尺寸为100x100。 我试过使用Bitmap.createScaledBitmap(位图,100,100,true);但失败了 做这件事的正确步骤是什么?看看这个: “这不能仅在xml中完成。不能同时缩放图像和ImageVie

如何将来自http请求的任何大小的图像放入大小为100x100的ImageView中

    android:layout_width="100dp"
    android:layout_height="100dp"
我希望避免失去准确性,我希望看到完全相同的图像,但尺寸为100x100。 我试过使用Bitmap.createScaledBitmap(位图,100,100,true);但失败了

做这件事的正确步骤是什么?

看看这个:
“这不能仅在xml中完成。不能同时缩放图像和ImageView,使图像的一维始终为100dp,并且ImageView的维度与图像相同

此代码可缩放ImageView的可绘制部分,使其保持在100dp x 100dp的正方形中,一维正好为100dp,并保持纵横比。然后调整ImageView的大小以匹配缩放图像的尺寸。此代码用于一个活动。我通过按钮单击处理程序对其进行了测试

private void scaleImage()
{
// Get the ImageView and its bitmap
ImageView view = (ImageView) findViewById(R.id.image_box);
Drawable drawing = view.getDrawable();
if (drawing == null) {
    return; // Checking for null & return, as suggested in comments
}
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

// Get current dimensions AND the desired bounding box
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int bounding = dpToPx(250);
Log.i("Test", "original width = " + Integer.toString(width));
Log.i("Test", "original height = " + Integer.toString(height));
Log.i("Test", "bounding = " + Integer.toString(bounding));

// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.  
float xScale = ((float) bounding) / width;
float yScale = ((float) bounding) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Log.i("Test", "xScale = " + Float.toString(xScale));
Log.i("Test", "yScale = " + Float.toString(yScale));
Log.i("Test", "scale = " + Float.toString(scale));

// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);

// Create a new bitmap and convert it to a format understood by the ImageView 
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
width = scaledBitmap.getWidth(); // re-use
height = scaledBitmap.getHeight(); // re-use
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Log.i("Test", "scaled width = " + Integer.toString(width));
Log.i("Test", "scaled height = " + Integer.toString(height));

// Apply the scaled bitmap
view.setImageDrawable(result);

// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
params.width = width;
params.height = height;
view.setLayoutParams(params);

Log.i("Test", "done");
}

private int dpToPx(int dp)
{
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
private void scaleImage()
{
//获取ImageView及其位图
ImageView视图=(ImageView)findViewById(R.id.image\u框);
Drawable drawing=view.getDrawable();
如果(图形==null){
return;//按照注释中的建议,检查null&return
}
位图位图=((BitmapDrawable)绘图).getBitmap();
//获取当前尺寸和所需的边界框
int width=bitmap.getWidth();
int height=bitmap.getHeight();
int-bounding=dpToPx(250);
Log.i(“测试”、“原始宽度=“+Integer.toString(宽度));
Log.i(“测试”、“原始高度=“+Integer.toString(高度));
Log.i(“Test”、“bounding=“+Integer.toString(bounding));
//确定要缩放的大小:需要较少缩放的维度为
//更靠近它的侧面。这样图像总是停留在你的内部
//边界框,且任意x/y轴都与之接触。
浮动xScale=((浮动)边界)/宽度;
浮动Y比例=((浮动)边界)/高度;
浮动比例=(xScale查看此链接:

试一试 android:scaleType=“fitXY”
对于较大的图像

你不能通过编程设置图像吗???@Black Devil是的,但我该如何一步一步地设置,这是我的问题。我用在stackoverflow找到的解决方案更新了答案。希望有帮助;)*
<ImageView a:id="@+id/image_box"
a:background="#ff0000"
a:src="@drawable/star"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_marginTop="20dp"
a:layout_gravity="center_horizontal"/>"