2013-04-03 107 views
18

我想翹曲相似圖片:Android:如何變形圖像?

Example image

新增2013年8月4日:我用這個代碼,但它不能正常工作:

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

+0

您是否看到了? https://github.com/huntergdavis/Easy_Image_Morph希望這可以幫助你。 – Shadow

+1

分享你的研究可以幫助每個人。告訴我們你試過了什麼,以及它爲什麼不符合你的需求。這表明你已經花時間去嘗試幫助自己,它使我們避免重申明顯的答案,最重要的是它可以幫助你得到更具體和相關的答案! –

回答

4

有一個更容易比編碼你自己的方式。見TransitionDrawable

LayerDrawables的一個擴展,旨在交叉衰落 所述第一和第二層之間。要開始轉換,請致電 startTransition(int)。要僅顯示第一層,請致電 resetTransition()。
它可以在XML文件中用 <轉換爲>元素來定義。在過渡的每個繪製對象是在 嵌套<項目>

你可以找到行的例子很多定義,here是:

<?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); 
    } 
    } 
}); 
+0

如果您有任何示例或代碼,請將其粘貼... – Roadies

+0

謝謝但是,此答案不符合我的要求我想扭曲圖像對象不要更改圖像使用觸摸... – Roadies

+0

@Roadies預計因爲你也做了一些工作,例子是一個按鈕,但是如果你閱讀了我鏈接到的文檔,你就會明白這個類是針對drawables的。你必須在你的drawables上編寫你自己的代碼。 – ilomambo

0

這可以通過OpenCV來實現,所以試試this