我的应用程序在 3x3 网格中有 9 个位图图像。所有这些都显示在屏幕上,并且根据用户与它们的交互方式(点击、双击、捏合、缩放、拖动),“操作”的图像需要执行不同的任务。
例如,如果我点击一张图片,它需要完全围绕其垂直访问旋转,然后停止。
目前我正在使用带有 onTouchEvent(..) 方法的 SurfaceView。我认为可以制作动画的方法是将图像的宽度从 1 缩放到 -1 再回到 1。我一直在尝试使用矩阵,但我无法让图像保持在它的中心位置。请帮助我解决此问题或提出替代/更好的方法来完成此操作。
我当前的代码只是一个测试。如果 b 为真,则该方法将尝试缩放位图。如果 b 为 false 则它将正常绘制位图。:
public void doDraw(Canvas canvas, boolean b)
{
Matrix m = new Matrix();
float scale = .5f;
m.setScale(scale, 1f);
m.postTranslate(mX,mY);
if(!b)
canvas.drawBitmap(mBitmap, mX, mY, null);
else
canvas.drawBitmap(mBitmap, m, null);
}
最佳答案
试试这个而不是 postTranslate
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
其中centerX,Y是位图的中心
更新
您可以在 android 上使用 graphics.camera 来变换矩阵。将相机访问到主视图(网格)并覆盖 getChildStaticTransformation。得到变换矩阵。 t.getMatrix()
并更改那个。您可以将它移动到任何您想要的位置因为这是来自主视图的矩阵但仅用于渲染该特定 subview 。
所以做类似的事情
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
Matrix matrix = t.getMatrix();
int centerX = (child.getWidth() / 2);
int centerY = (child.getHeight() / 2);
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
mCamera.save();
if (child == getChildAt(0)) {
mCamera.translate(pixels to the right,pixels to the bottom,
to the z axis);
mCamera.getMatrix(matrix);
mCamera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
return true;
}
另外不要忘记设置 setStaticTransformationsEnabled(true)
;某处到网格的构造函数
关于Android缩放位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7276574/