Android:如何扭曲图像?

20
我希望能像这样扭曲图片:

示例图片

添加于 2013 年 08 月 04 日:我使用了以下代码,但它无法正常工作:
    private static final int WIDTH = 20;
    private static final int HEIGHT = 20;
    private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

    private final Bitmap mBitmap;
    private final float[] mVerts = new float[COUNT*2];
    private final float[] mOrig = new float[COUNT*2];

    private final Matrix mMatrix = new Matrix();
    private final Matrix mInverse = new Matrix();

    private static void setXY(float[] array, int index, float x, float y) {
        array[index*2 + 0] = x;
        array[index*2 + 1] = y;
    }

    public SampleView(Context context) {
        super(context);
        setFocusable(true);

        mBitmap = BitmapFactory.decodeResource(getResources(),
                                                 R.drawable.ic_launcher);

        float w = mBitmap.getWidth();
        float h = mBitmap.getHeight();
        // construct our mesh
        int index = 0;
        for (int y = 0; y <= HEIGHT; y++) {
            float fy = h * y / HEIGHT;
            for (int x = 0; x <= WIDTH; x++) {
                float fx = w * x / WIDTH;                    
                setXY(mVerts, index, fx, fy);
                setXY(mOrig, index, fx, fy);
                index += 1;
            }
        }

        mMatrix.setTranslate(10, 10);
        mMatrix.invert(mInverse);
    }

    @Override protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFCCCCCC);

        canvas.concat(mMatrix);
        canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                              null, 0, null);
    }

    private void warp(float cx, float cy) {
        final float K = 10000;
        float[] src = mOrig;
        float[] dst = mVerts;
        for (int i = 0; i < COUNT*2; i += 2) {
            float x = src[i+0];
            float y = src[i+1];
            float dx = cx - x;
            float dy = cy - y;
            float dd = dx*dx + dy*dy;
            float d = FloatMath.sqrt(dd);
            float pull = K / (dd + 0.000001f);

            pull /= (d + 0.000001f);
         //   android.util.Log.d("skia", "index " + i + " dist=" + d + " pull=" + pull);

            if (pull >= 1) {
                dst[i+0] = cx;
                dst[i+1] = cy;
            } else {
                dst[i+0] = x + dx * pull;
                dst[i+1] = y + dy * pull;
            }
        }
    }

    private int mLastWarpX = -9999; // don't match a touch coordinate
    private int mLastWarpY;

    @Override public boolean onTouchEvent(MotionEvent event) {
        float[] pt = { event.getX(), event.getY() };
        mInverse.mapPoints(pt);

        int x = (int)pt[0];
        int y = (int)pt[1];
        if (mLastWarpX != x || mLastWarpY != y) {
            mLastWarpX = x;
            mLastWarpY = y;
            warp(pt[0], pt[1]);
            invalidate();
        }
        return true;
    }

1
尝试使用这个库:http://code.google.com/p/javamorph/ - Harshid
1
分享您的研究可以帮助每个人。告诉我们您尝试过什么以及为什么不符合您的需求。这表明您已经花时间尝试自己解决问题,它可以避免我们重复显而易见的答案,最重要的是,它可以帮助您获得更具体和相关的答案! - NullPoiиteя
2
顺便提一下,这是扭曲而不是变形。 - Daniel Mošmondor
看一下这个链接 http://www.imagemagick.org/Usage/distorts/#shepards 可能会对你有所帮助。 - Chintan Rathod
你现在有任何想法或代码吗?同样的问题,+1。 - Mayur R. Amipara
显示剩余6条评论
2个回答

3

有比编写自己的代码更简单的方法。请参考TransitionDrawable

LayerDrawables的扩展,旨在淡入淡出第一层和第二层。要启动过渡,请调用startTransition(int)。要仅显示第一层,请调用resetTransition()。
它可以在XML文件中使用<transition>元素定义。过渡中的每个Drawable都在嵌套的<item>中定义

您可以在线找到许多示例,这里是其中之一:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/first_image" />
  <item android:drawable="@drawable/second_image" />
</transition>

和代码

final ImageView image = (ImageView) findViewById(R.id.image);
final ToggleButton button = (ToggleButton) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(final View v) {
    TransitionDrawable drawable = (TransitionDrawable) image.getDrawable();
    if (button.isChecked()) {
      drawable.startTransition(500);
    } else {
      drawable.reverseTransition(500);
    }
  }
}); 

谢谢,但是这个答案不符合我的要求,我想要包裹图像对象而不是使用触摸来改变图像... - Roadies
@Roadies 你也需要做一些工作,这个例子是针对按钮的,但如果你阅读我链接的文档,你会理解这个类是针对一般的可绘制对象。你需要围绕你的可绘制对象编写自己的代码。 - ilomambo
@Roadies 既然你想要的不是变形,而是扭曲,我觉得这个方法对你没有帮助。你应该编辑你的问题和标题。 - ilomambo

-1

这可以通过 OpenCV 实现,请尝试 this


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接