Android ImageView缩放不';行不通
我用谷歌搜索了这个问题,但没有找到合适的答案 这是我的Android ImageView缩放不';行不通,android,android-imageview,Android,Android Imageview,我用谷歌搜索了这个问题,但没有找到合适的答案 这是我的ImageView: 这是xml格式的ImageView: <ImageView android:id="@+id/stock_cover" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true"
ImageView
:
这是xml格式的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" />
尝试在xml中为ImageView使用ScaleType属性。比如说
android:scaleType="centerCrop".
很抱歉,使用此选项时无法将其放大:
android:layout_width="match_parent"
它已经意味着“尽可能地选择宽度”。由于宽度具有最大值,因此图像高度也具有最大值
您可以使用以下方法进行检查:
android:scaleX="3"
android:scaleY="3"
它应该使它大三倍。如果不起作用,请使用:
android:scaleX="0.2"
android:scaleY="0.2"
它应该把它缩小五倍。我很肯定它会起作用的
希望它能帮助你从代码中设置布局参数
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的一个已知问题,它不会放大小图像。请尝试以下代码:
private void scaleImage(图像视图,int boundBoxInDp)
{
//获取ImageView及其位图
Drawable drawing=view.getDrawable();
位图位图=((BitmapDrawable)绘图).getBitmap();
//获取当前维度
int width=bitmap.getWidth();
int height=bitmap.getHeight();
//确定要缩放的大小:需要较少缩放的维度为
//更靠近它的侧面。这样图像总是停留在你的内部
//边界框,且任意x/y轴都与之接触。
float xScale=((float)boundBoxInDp)/宽度;
float yScale=((float)boundBoxInDp)/高度;
浮动比例=(xScale您可以将位图设置为背景
// 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));
}
});
您需要动画还是什么?那么什么是左图像和右图像?有一个imageView和一个imageView请查看此链接。我尝试了此链接-centerCrop、fitXY、matrix和其他-但这无助于我发布完整的xml?
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);
}
// 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));
}
});