android image调整尺寸_android – 将图像放入ImageView,保持宽高比,然后将ImageView调整为图像尺寸?...

(在对原始问题作出澄清后,答案大量修改)

澄清后:

这不能在xml中完成。无法缩放图像和ImageView,因此图像的一个维度将始终为250dp,而ImageView将具有与图像相同的维度。

这个代码缩放一个ImageView的Drawable可以保留在一个正方形像250dp x 250dp一个维度刚好250dp并保持宽高比。然后调整ImageView的大小以匹配缩放图像的尺寸。代码在活动中使用。我通过按钮点击处理程序测试。

请享用。 🙂

private void scaleImage(ImageView view) throws NoSuchElementException {

// Get bitmap from the the ImageView.

Bitmap bitmap = null;

try {

Drawable drawing = view.getDrawable();

bitmap = ((BitmapDrawable) drawing).getBitmap();

} catch (NullPointerException e) {

throw new NoSuchElementException("No drawable on given view");

} catch (ClassCastException e) {

// Check bitmap is Ion drawable

bitmap = Ion.with(view).getBitmap();

}

// Get current dimensions AND the desired bounding box

int width = 0;

try {

width = bitmap.getWidth();

} catch (NullPointerException e) {

throw new NoSuchElementException("Can't find bitmap on given view/drawable");

}

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);

}

ImageView的xml代码:

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"/>

更新2012年11月7日:按注释中的建议添加了空指针检查

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值