2011-09-01 174 views
1

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

因此,例如,如果我点击一个图像,它需要完全旋转垂直访问,然后停止。

目前我正在使用SurfaceView和onTouchEvent(..)方法。我想通过这种方式我可以对此进行动画处理,方法是将图像的宽度从1缩放到-1回到1.我一直在尝试使用矩阵,但无法让图像保持居中。请帮我解决这个问题,或者建议一个替代/更好的方法来完成这个。

我现在的代码只是一个测试。如果b为真,则该方法将尝试缩放位图。如果b是假的,然后它会得出正常位图:

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); 
} 

回答

1

尝试,而不是这个postTranslate

matrix.preTranslate(-centerX, -centerY); 
matrix.postTranslate(centerX, centerY); 

其中的centerX,Y是位图的中心

UPDATE

你可以在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);网格的构造函数的某处

+0

我假设你的意思是这样的: 'float scale = -1f; \t m.preTranslate(-mX,-mY); \t m.setScale(scale,1f); \t m.postTranslate(mX,mY);' 但这并不奏效。我也尝试过订购量表,预翻译,翻译后。没有任何内容 – Sababado

+0

它使图像与左边缘对齐。我需要它被集中。 – Sababado

+0

发布更新了看一看 – weakwire