Android缩放位图图像

Android缩放位图图像,android,canvas,matrix,image-manipulation,scale,Android,Canvas,Matrix,Image Manipulation,Scale,我的应用程序在3x3网格中有9个位图图像。所有这些都显示在屏幕上,并且取决于用户与它们交互的方式(点击、双击、挤压、缩放、拖动),“动作启动”的图像需要执行不同的任务 例如,如果我点击一个图像,它需要完全围绕垂直通道旋转,然后停止 目前我正在使用SurfaceView和onTouchEvent(..)方法。我认为我可以通过将图像的宽度从1缩放到-1再缩放到1来设置动画。我一直在尝试使用矩阵,但我无法使图像保持在其中心位置。请帮助我解决这个问题,或者建议一种替代/更好的方法来完成这个问题 我当前的

我的应用程序在3x3网格中有9个位图图像。所有这些都显示在屏幕上,并且取决于用户与它们交互的方式(点击、双击、挤压、缩放、拖动),“动作启动”的图像需要执行不同的任务

例如,如果我点击一个图像,它需要完全围绕垂直通道旋转,然后停止

目前我正在使用SurfaceView和onTouchEvent(..)方法。我认为我可以通过将图像的宽度从1缩放到-1再缩放到1来设置动画。我一直在尝试使用矩阵,但我无法使图像保持在其中心位置。请帮助我解决这个问题,或者建议一种替代/更好的方法来完成这个问题

我当前的代码只是一个测试。如果b为true,则该方法将尝试缩放位图。如果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()
并更改该值。您可以将其移动到您想要的任何位置,因为这是主视图中的矩阵,但仅用于渲染该特定子对象

那就这样做吧

@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)
;到网格构造函数的某个地方

尝试此操作,而不是
postTranslate

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
其中,centerX,Y是位图的中心

更新

您可以在android上使用graphics.camera来转换矩阵。将相机访问到主视图(栅格)并覆盖getChildStaticTransformation。得到变换矩阵
t.getMatrix()
并更改该值。您可以将其移动到您想要的任何位置,因为这是主视图中的矩阵,但仅用于渲染该特定子对象

那就这样做吧

@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)
;网格构造函数的某处

我假设你的意思是这样的:
float scale=-1f;m.preTranslate(-mX,-mY);m.设置刻度(刻度,1f);m.postTranslate(mX,mY)但这不起作用。我也试过顺序表,翻译前,翻译后。Nothing保持图像与左边缘对齐。我需要它居中。问题是,我的图像不是
视图
(这看起来像是您将它们视为的),而是
位图
s。我现在在整个程序中只有
视图
SurfaceView
。除此之外,我还需要一种方法来缩放、旋转和拖动图像。如果我错了,请纠正我,但这似乎不是为这种操作做好准备的最佳方式。不,这适用于ImageView或一般的android ViewRight。。。所以我很感激你的回答,但考虑到我的具体情况,这并没有什么帮助。我假设你的意思是:
float scale=-1f;m.preTranslate(-mX,-mY);m.设置刻度(刻度,1f);m.postTranslate(mX,mY)但这不起作用。我也试过顺序表,翻译前,翻译后。Nothing保持图像与左边缘对齐。我需要它居中。问题是,我的图像不是
视图
(这看起来像是您将它们视为的),而是
位图
s。我现在在整个程序中只有
视图
SurfaceView
。除此之外,我还需要一种方法来缩放、旋转和拖动图像。如果我错了,请纠正我,但这似乎不是为这种操作做好准备的最佳方式。不,这适用于ImageView或一般的android ViewRight。。。所以我很感激你的回答,但考虑到我的具体情况,这并没有什么帮助。