这个xView中的ImageView:
<ImageView
android:id="@+id/stock_cover"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/place_holder16_9" />
如何像示例图像一样缩放此图像?
P.S。这张图片是我从网上下载的。
UPD 1.添加了ImageLoader代码。
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.place_holder16_9)
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoader.getInstance().displayImage(promo.getImageBigUrl(), mCoverImageView, options);
答案 0 :(得分:1)
很抱歉,但是当你使用它时,你无法做大:
android:layout_width="match_parent"
它已经意味着“尽可能多的宽度”。由于宽度具有最大值,因此图像高度也会占用最大值。
您可以使用以下方法进行检查:
android:scaleX="3"
android:scaleY="3"
它应该使它变大三倍。如果不起作用,请使用:
android:scaleX="0.2"
android:scaleY="0.2"
它应该缩小五次。我很确定它会起作用
希望有所帮助
答案 1 :(得分:1)
从代码中设置LayoutParams
。
ImageView image = new ImageView(this);
image.setImageResource(getResources().getIdentifier('file_name', "drawable", getPackageName()));
image.setAdjustViewBounds(true);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
parent.addView(image);
这是ImageView的一个已知问题,它不会放大小图片。
答案 2 :(得分:1)
试试这段代码:
private void scaleImage(ImageView view, int boundBoxInDp)
{
// Get the ImageView and its bitmap
Drawable drawing = view.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 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) boundBoxInDp) / width;
float yScale = ((float) boundBoxInDp) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// 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);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();
// 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);
}
private int dpToPx(int dp)
{
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
答案 3 :(得分:1)
您可以将Bitmap
设置为背景。
// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
mCoverImageView.setBackground(new BitmapDrawable(getResources(), bitmap));
}
});
答案 4 :(得分:0)
尝试在xml中使用ImageView的ScaleType属性。例如
android:scaleType="centerCrop".