【问题标题】:Android Scale a bitmap imageAndroid 缩放位图图像
【发布时间】:2011-09-01 21:09:29
【问题描述】:

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

例如,如果我点击一张图片,它需要围绕它的垂直访问完全旋转,然后停止。

目前我正在使用 SurfaceView 和 onTouchEvent(..) 方法。我认为我可以制作动画的方法是将图像的宽度从 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);
}

【问题讨论】:

    标签: android canvas matrix image-manipulation scale


    【解决方案1】:

    试试这个而不是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.setScale(scale, 1f); m.postTranslate(mX,mY); 但这不起作用。我还尝试了订单规模,预翻译,后翻译。什么都没有
    • 它使图像沿左边缘排列。我需要它居中。
    • 这个问题是我的图像不是View 的(这就是你对待它们的样子)而是Bitmaps。我现在在整个程序中只有ViewSurfaceView。最重要的是,我需要有一种方法来缩放、旋转和拖动图像。如果我错了,请纠正我,但这似乎不是为这种操纵做好准备的最佳方式。
    • 这不适用于 ImageViews 或一般的 android View
    • 对...所以我很欣赏这个答案,但考虑到我的具体情况,它没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    相关资源
    最近更新 更多