我试图从Android的中心点扩展位图以实现缩放效果,但没有成功。我的代码是:
float scaleWidth = ((float) width + (i * 5)) / width;
float scaleHeight = ((float) height + (i * 5)) / height;
Matrix matrix = new Matrix();
matrix.setScale(scaleWidth, scaleHeight, scaleWidth / 2, scaleHeight / 2);
Bitmap rescaledBitmap = Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
result.add(rescaledBitmap);
我通过将尺寸除以2来设置轴心点,但效果只是图像从0,0缩放为坐标而不是从中心。我想要的是图像是固定大小,但从其中心点放大(从而裁剪图像)。
我将使用属性动画师提供替代解决方案,因为我认为这是一个更清洁的解决方案。
SomeLayout.xml(这里的关键是ViewGroup与View的大小相同,因此它会根据您的要求进行剪辑(比如google maps放大))
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center">
<View
android:id="@+id/zoom"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/myCoolImage"
/>
</FrameLayout>
代码:(1,2,1将以1x然后2x开始,然后返回1x,它需要一个值列表)
final View view = findViewById(R.id.zoom);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.SCALE_X, 1, 2, 1)
.ofFloat(view, View.SCALE_Y, 1, 2, 1)
.setDuration(5000);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.start();
}
});
因此,对于此版本图像,如果您使用onClickListeners进行缩放+和缩放 - 视图,只要您知道要缩放的值,就可以基本模拟受控缩放。
同样如前所述,ViewGroup与内部视图的大小相同将强制动画剪辑到其父边界而不是完全可见。
参考文献:
createBitmap中的第二个和第三个参数采用左上角的x / y坐标。你发送0,0,所以如果我理解正确的话......
图像被正确缩放,但图像不居中,对吗?
要使其居中,您需要找到左上角的正确(x,y)点。这应该是原始宽度/高度的1/4。
所以...
Bitmap rescaledBitmap = Bitmap.createBitmap(src, (width/2), (height/2), width, height, matrix, true);
应该管用。
当然这会迟到,但对所有人照顾我:
double scaleFactor = 0.75; // Set this to the zoom factor
int widthOffset = (int) ((1 - scaleFactor)/2 * bmp.getWidth());
int heightOffset = (int) ((1 - scaleFactor)/2 * bmp.getHeight());
int numWidthPixels = bmp.getWidth() - 2 * widthOffset;
int numHeightPixels = bmp.getHeight() - 2 * heightOffset;
Bitmap rescaledBitmap = Bitmap.createBitmap(bmp, widthOffset, heightOffset, numWidthPixels, numHeightPixels, null, true);
此示例将放大位图的中心并且系数为25%。