`
wang_peng1
  • 浏览: 3904443 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android画图---图像的扭曲

阅读更多
这个实例 是图像的扭曲,代码比较复杂,很多不是很懂,只把大体意思几下:
canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                                  null, 0, null);
主要通过mesh创建一个图像,这个mesh呢是可以扭曲的,这里面一定要注意vmVerts,这个数组是宽高都加以后乘机的两倍 然后在加上 偏移数值 ,上面的偏移是0.
  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();
这是最基本的初始化,注意数组的变化。

下面是把成对的坐标放在一个数组中,其中奇数项存x,偶数项存y。
  private static void setXY(float[] array, int index, float x, float y) {
            array[index*2 + 0] = x;
            array[index*2 + 1] = y;
        }

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

             canvas.concat(mMatrix);
          //  canvas.concat(mInverse);
            canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                                  null, 0, null);
       
        }
其中canvas.concat(mMatrix);不是很懂貌似是用参数来取代默认的Matrix,就是说要对哪个进行绘画。


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

            mBitmap = BitmapFactory.decodeResource(getResources(),
                                                     R.drawable.beach);
           
            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(100, 10);
            mMatrix.invert(mInverse);
           // mInverse.setTranslate(100, 100);
         
        }
        上面就是一个view的构造函数, mMatrix.setTranslate(100, 10);
用来设置mMatrix在屏幕上的位置。for循环是把图像上的每个点都存储在数组中。
最后动过一个事件来改变数组中值产生图像的变化。
@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;
       
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics