通过动态设置高度来裁剪图像
Posted
技术标签:
【中文标题】通过动态设置高度来裁剪图像【英文标题】:Cropping image by setting height dynamically 【发布时间】:2017-12-04 06:34:34 【问题描述】:猜一个人的照片。 (头在上,腿在下):P
我正在使用 layoutparam 动态设置高度。
当我将高度设置为 300 时,
它将高度从 HEAD 设置为 300 个单位到图像的腿。
我想要将高度从 LEG 设置为前 300 个单位。
imageView.setHeight()
【问题讨论】:
您需要使用指定的 (X,Y) 裁剪位图。见This。 是否可以将高度从 0(底部)设置为 100(顶部)。就像从 0 高度到 100% 高度显示图像视图一样。有可能吗? 高度将是相同的(300 或任何你想要的)。坐标会变化(X,Y)。 不,我不想改变坐标,因为我说过我想通过将高度 0 设置为 100% 来从底部到顶部开始显示 imageview。感谢您的快速回复。 如果符合您的要求,您可以使用 Reveal Animation。 【参考方案1】:在你的 java 类中使用这个方法:
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(150);// set height
Logger.i("Test", "original width = " + Integer.toString(width));
Logger.i("Test", "original height = " + Integer.toString(height));
Logger.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;
Logger.i("Test", "xScale = " + Float.toString(xScale));
Logger.i("Test", "yScale = " + Float.toString(yScale));
Logger.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);
Logger.i("Test", "scaled width = " + Integer.toString(width));
Logger.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);
Logger.i("Test", "done");
private int dpToPx(int dp)
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float) dp * density);
现在,如何调用这个方法:
Bitmap bitmap;
try
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
imageView.setImageBitmap(bitmap);
scaleImage(imageView);// call your imageview
catch (IOException e)
e.printStackTrace();
【讨论】:
以上是关于通过动态设置高度来裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章