问题描述
如何centerCrop喜欢同样的方法centerCrop做Android的XML的图像?我不想裁剪图像。我的心已经尝试下面的code,但它是裁剪图像的中心部分,它产生一个方形的形象,我想同样centerCrop功能使用在android.Can任何人的帮助画布?希望我的问题是清楚的给你。
位图位= BitmapFactory.de codeResource(RES(R.drawable.ice));
位图cropBmp;
如果(bitmap.getWidth()&GT = bitmap.getHeight()){ cropBmp = Bitmap.createBitmap(
位图,
bitmap.getWidth()/ 2 - bitmap.getHeight()/ 2,
0,
bitmap.getHeight(),
bitmap.getHeight()
); }其他{ cropBmp = Bitmap.createBitmap(
位图,
0,
bitmap.getHeight()/ 2 - bitmap.getWidth()/ 2,
bitmap.getWidth(),
bitmap.getWidth()
);
} dstRectForRender.set(50,20,500,360);
canvas2.drawBitmap(cropBmp,空,dstRectForRender,油漆);
如果你想要做同样的效果(完全相同的效果),可以收看的Android如何做到这一点的位置:https://github.com/android/platform_frameworks_base/blob/3f453164ec884d26a556477027b430cb22a9b7e3/core/java/android/widget/ImageView.java
code约CENTER_CROP的有趣的部分:
[...]
mDrawMatrix = mMatrix;浮规模;
浮DX = 0,DY = 0;如果(DWIDTH * vheight> vwidth *做切片){
规模=(浮点)vheight /(浮点)做切片;
DX =(vwidth - DWIDTH *等级)* 0.5F;
}其他{
规模=(浮点)vwidth /(浮点)DWIDTH;
DY =(vheight - 做切片*等级)* 0.5F;
}mDrawMatrix.setScale(秤,秤);
mDrawMatrix.postTranslate((int)的(DX + 0.5F),(INT)(DY + 0.5F));[...]
How to centerCrop an image like the same method "centerCrop" do in xml android ? I don't want to crop the center of the image .I have tried the below code but ,it was cropping the center portion of the image,and it produce a square shaped image,I want the same centerCrop function to use in a Canvas on android.Can anyone help?? Hope my question is clear to you.
Bitmap bitmap=BitmapFactory.decodeResource(res, (R.drawable.ice));
Bitmap cropBmp;
if (bitmap.getWidth() >= bitmap.getHeight()){
cropBmp = Bitmap.createBitmap(
bitmap,
bitmap.getWidth()/2 - bitmap.getHeight()/2,
0,
bitmap.getHeight(),
bitmap.getHeight()
);
}else{
cropBmp = Bitmap.createBitmap(
bitmap,
0,
bitmap.getHeight()/2 - bitmap.getWidth()/2,
bitmap.getWidth(),
bitmap.getWidth()
);
}
dstRectForRender.set(50,20 ,500 ,360 );
canvas2.drawBitmap(cropBmp, null, dstRectForRender, paint);
If you want to do same effect (exactly same effect), you can watch how Android do it here: https://github.com/android/platform_frameworks_base/blob/3f453164ec884d26a556477027b430cb22a9b7e3/core/java/android/widget/ImageView.java
Interesting part of code about CENTER_CROP:
[...]
mDrawMatrix = mMatrix;
float scale;
float dx = 0, dy = 0;
if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
}
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
[...]
这篇关于如何编程方式centerCrop图像通过保持宽高比像centerCrop做XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!