Android的 - 图像扭曲效应 [英] Android - Image Warp effect

查看:159
本文介绍了Android的 - 图像扭曲效应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想申请效果的照片经<​​/A>和的照片消泡剂应用。对于我使用 BitmapMesh 。问题在于,它没有保存扭曲图像。每当我抚摸形象,它刷新图像,不保存我的previously扭曲image.I要保存,每当用户执行经操作该图像。在这里,我张贴我的code。在这里,我使用BitmapMesh活动执行图像扭曲效果。

请帮我解决这个问题。 谢谢你。

code:

BitmapMesh活动:

 进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Matrix;
进口android.os.Bundle;
进口android.util.FloatMath;
进口android.view.MotionEvent;
进口android.view.View;

公共类BitmapMesh扩展GraphicsActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(新SampleView(本));
    }

    私有静态类SampleView扩展视图{
        私有静态最终诠释WIDTH = 20;
        私有静态最终诠释HEIGHT = 20;
        私有静态最终诠释COUNT =(宽+ 1)*(高+ 1);

        私人最终位图mBitmap;
        私人最终浮动[] mVerts =新的浮动[COUNT * 2];
        私人最终浮动[] mOrig =新的浮动[COUNT * 2];

        私人最终矩阵mMatrix =新的Matrix();
        私人最终矩阵MINVERSE =新的Matrix();

        私有静态无效setXY(浮动[]数组,INT指数,浮动的x,浮动Y){
            数组[索引* 2 + 0] = X;
            数组[索引* 2 + 1] = Y;
        }

        公共SampleView(上下文的背景下){
            超(上下文);
            setFocusable(真正的);

            mBitmap = BitmapFactory.de codeResource(getResources()
                    R.drawable.image1);

            浮瓦特= mBitmap.getWidth();
            浮动H = mBitmap.getHeight();
            //构造我们的目
            INT索引= 0;
            对于(INT Y = 0; Y&LT; =身高; Y ++){
                浮财年= H * Y /高度;
                为(中间体X = 0 X  - 其中=宽度; X ++){
                    浮FX = W * X /宽;
                    setXY(mVerts,指数,外汇,风云);
                    setXY(mOrig,指数,外汇,风云);
                    指数+ = 1;
                }
            }

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

        @覆盖
        保护无效的OnDraw(帆布油画){
            canvas.drawColor(0xFFCCCCCC);

            canvas.concat(mMatrix);
            canvas.drawBitmapMesh(mBitmap,宽度,高度,mVerts,0,NULL,0,
                    空值);
        }

        私人无效经线(浮点CX,浮CY){
            最终浮动K = 10000;
            浮动[] SRC = mOrig;
            浮动[] DST = mVerts;
            的for(int i = 0; I&LT; COUNT * 2; I + = 2){
                浮X = SRC [I + 0];
                浮动Y = SRC [I + 1];
                浮DX = CX  -  X;
                浮DY = CY  - ÿ;
                浮DD = DX * DX + DY * DY;
                浮D = FloatMath.sqrt(DD);
                浮拉= K /(DD + 0.000001f);

                拉/ =(D + 0.000001f);
                // android.util.Log.d(Skia的指数+ I +DIST =+ D +
                //拉=+拉);

                如果(拉&GT; = 1){
                    DST [I + 0] = CX;
                    DST [I + 1] = CY;
                } 其他 {
                    DST [1 + 0] = X + DX *拉;
                    DST [I + 1] = Y + DY *拉;
                }
            }

        }

        私人诠释mLastWarpX = -9999; //不匹配触摸坐标
        私人诠释mLastWarpY;

        @覆盖
        公共布尔的onTouchEvent(MotionEvent事件){
            浮动[] PT = {event.getX(),event.getY()};
            mInverse.mapPoints(PT);

            INT X =(int)的角[0];
            INT Y =(int)的角[1];
            如果(mLastWarpX!= X || mLastWarpY!= Y){
                mLastWarpX = X;
                mLastWarpY = Y;
                经纱(磅[0],角[1]);
                无效();
            }
            返回true;
        }
    }
}
 

图形活动:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.ViewGroup;

类GraphicsActivity延伸活动{
    //设置为true,测试图片
    私有静态最终布尔TEST_PICTURE = FALSE;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @覆盖
    公共无效的setContentView(查看视图){
        如果(TEST_PICTURE){
            ViewGroup中VG =新PictureLayout(本);
            vg.addView(视图);
            鉴于= VG;
        }

        super.setContentView(视图);
    }
}
 

PictureLayout.java

 进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Picture;
进口android.graphics.Rect;
进口android.graphics.drawable.Drawable;
进口android.util.AttributeSet;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.ViewParent;

类PictureLayout扩展的ViewGroup {
    私人最终图片mPicture =新画面();

    公共PictureLayout(上下文的背景下){
        超(上下文);
    }

    公共PictureLayout(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    @覆盖
    公共无效addView(查看孩子){
        如果(getChildCount()→1){
            抛出新IllegalStateException异常(
                    PictureLayout只能承载一个直接子);
        }

        super.addView(子);
    }

    @覆盖
    公共无效addView(查看孩子,INT指数){
        如果(getChildCount()→1){
            抛出新IllegalStateException异常(
                    PictureLayout只能承载一个直接子);
        }

        super.addView(儿童,索引);
    }

    @覆盖
    公共无效addView(查看孩子的LayoutParams PARAMS){
        如果(getChildCount()→1){
            抛出新IllegalStateException异常(
                    PictureLayout只能承载一个直接子);
        }

        super.addView(孩子,则params);
    }

    @覆盖
    公共无效addView(查看孩子,INT指数的LayoutParams PARAMS){
        如果(getChildCount()→1){
            抛出新IllegalStateException异常(
                    PictureLayout只能承载一个直接子);
        }

        super.addView(儿童,指数,则params);
    }

    @覆盖
    保护的LayoutParams generateDefaultLayoutParams(){
        返回新的LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
        最终诠释计数= getChildCount();

        INT了maxHeight = 0;
        INT了maxWidth = 0;

        的for(int i = 0; I&LT;计数;我++){
            最后查看孩子= getChildAt(我);
            如果(child.getVisibility()!= GONE){
                measureChild(孩子,widthMeasureSpec,heightMeasureSpec);
            }
        }

        了maxWidth + = getPaddingLeft()+ getPaddingRight();
        了maxHeight + = getPaddingTop()+ getPaddingBottom();

        可绘制绘制=的getBackground();
        如果(绘制!= NULL){
            了maxHeight = Math.max(了maxHeight,drawable.getMinimumHeight());
            了maxWidth = Math.max(了maxWidth,drawable.getMinimumWidth());
        }

        setMeasuredDimension(resolveSize(了maxWidth,widthMeasureSpec)
                resolveSize(了maxHeight,heightMeasureSpec));
    }

    私人无效drawPict(画布油画,诠释的x,INT Y,INT W,INT小时,浮SX,
            浮动SY){
        canvas.save();
        canvas.translate(X,Y);
        canvas.cli $ P $厘(0,0,W,H);
        canvas.scale(0.5F,0.5F);
        canvas.scale(SX,SY,W,H);
        canvas.drawPicture(mPicture);
        canvas.restore();
    }

    @覆盖
    保护无效dispatchDraw(帆布油画){
        super.dispatchDraw(mPicture.beginRecording(的getWidth(),的getHeight()));
        mPicture.endRecording();

        INT X =的getWidth()/ 2;
        INT Y =的getHeight()/ 2;

        如果(假){
            canvas.drawPicture(mPicture);
        } 其他 {
            drawPict(帆布,0,0,X,Y,1,1);
            drawPict(画布中,x,0,X,Y,-1,1);
            drawPict(帆布,0,Y,X,Y,1,-1);
            drawPict(帆布,X,Y,X,Y,-1,-1);
        }
    }

    @覆盖
    公共ViewParent invalidateChildInParent(INT []的位置,矩形脏){
        位置[0] = getLeft();
        位置[1] =共达();
        dirty.set(0,0,的getWidth(),的getHeight());
        返回的getParent();
    }

    @覆盖
    保护无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在{
        最终诠释计数= super.getChildCount();

        的for(int i = 0; I&LT;计数;我++){
            最后查看孩子= getChildAt(我);
            如果(child.getVisibility()!= GONE){
                最终诠释childLeft = getPaddingLeft();
                最终诠释childTop = getPaddingTop();
                child.layout(childLeft,childTop,
                        childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());

            }
        }
    }
}
 

在这片code解决方案

  //变化不大
  浮动[] DST; //全球

公共SampleView(上下文的背景下){
        超(上下文);
        setFocusable(真正的);

        mBitmap = BitmapFactory.de codeResource(getResources()
                R.drawable.image1);

        浮瓦特= mBitmap.getWidth();
        浮动H = mBitmap.getHeight();
        //构造我们的目
        INT索引= 0;
        对于(INT Y = 0; Y&LT; =身高; Y ++){
            浮财年= H * Y /高度;
            为(中间体X = 0 X  - 其中=宽度; X ++){
                浮FX = W * X /宽;
                setXY(mVerts,指数,外汇,风云);
                setXY(mOrig,指数,外汇,风云);
                指数+ = 1;

                DST = mVerts; //指定DST这里只是一次

            }
        }

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

    @覆盖
    保护无效的OnDraw(帆布油画){
        canvas.drawColor(0xFFCCCCCC);

        canvas.concat(mMatrix);
        canvas.drawBitmapMesh(mBitmap,宽度,高度,mVerts,0,NULL,0,
                空值);
    }

    私人无效经线(浮点CX,浮CY){
        最终浮动K = 10000;
        浮动[] SRC = DST; //现在你所申请的最后影响的像素包装的影响

        的for(int i = 0; I&LT; COUNT * 2; I + = 2){
            浮X = SRC [I + 0];
            浮动Y = SRC [I + 1];
            浮DX = CX  -  X;
            浮DY = CY  - ÿ;
            浮DD = DX * DX + DY * DY;
            浮D = FloatMath.sqrt(DD);
            浮拉= K /(DD + 0.000001f);

            拉/ =(D + 0.000001f);
            // android.util.Log.d(Skia的指数+ I +DIST =+ D +
            //拉=+拉);

            如果(拉&GT; = 1){
                DST [I + 0] = CX;
                DST [I + 1] = CY;
            } 其他 {
                DST [1 + 0] = X + DX *拉;
                DST [I + 1] = Y + DY *拉;
            }
        }

    }
 

In my android application, i want to apply image warp effect provided in Photo Warp and Photo Deformer application. For that i used BitmapMesh. The problem is that, it's not saving warped image. Whenever i touch image, it refresh the image and doesn't save my previously warped image.I want to save that image whenever user perform warp operation. Here i am posting my code. Here i am using "BitmapMesh" activity to perform warp effect on image.

Please help me to solve this problem. Thanks.

Code:

BitmapMesh Activity:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;

public class BitmapMesh extends GraphicsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private static class SampleView extends View {
        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.image1);

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

Graphics Activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

class GraphicsActivity extends Activity {
    // set to true to test Picture
    private static final boolean TEST_PICTURE = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(View view) {
        if (TEST_PICTURE) {
            ViewGroup vg = new PictureLayout(this);
            vg.addView(view);
            view = vg;
        }

        super.setContentView(view);
    }
}

PictureLayout.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

class PictureLayout extends ViewGroup {
    private final Picture mPicture = new Picture();

    public PictureLayout(Context context) {
        super(context);
    }

    public PictureLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addView(View child) {
        if (getChildCount() > 1) {
            throw new IllegalStateException(
                    "PictureLayout can host only one direct child");
        }

        super.addView(child);
    }

    @Override
    public void addView(View child, int index) {
        if (getChildCount() > 1) {
            throw new IllegalStateException(
                    "PictureLayout can host only one direct child");
        }

        super.addView(child, index);
    }

    @Override
    public void addView(View child, LayoutParams params) {
        if (getChildCount() > 1) {
            throw new IllegalStateException(
                    "PictureLayout can host only one direct child");
        }

        super.addView(child, params);
    }

    @Override
    public void addView(View child, int index, LayoutParams params) {
        if (getChildCount() > 1) {
            throw new IllegalStateException(
                    "PictureLayout can host only one direct child");
        }

        super.addView(child, index, params);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int count = getChildCount();

        int maxHeight = 0;
        int maxWidth = 0;

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }

        maxWidth += getPaddingLeft() + getPaddingRight();
        maxHeight += getPaddingTop() + getPaddingBottom();

        Drawable drawable = getBackground();
        if (drawable != null) {
            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
        }

        setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
                resolveSize(maxHeight, heightMeasureSpec));
    }

    private void drawPict(Canvas canvas, int x, int y, int w, int h, float sx,
            float sy) {
        canvas.save();
        canvas.translate(x, y);
        canvas.clipRect(0, 0, w, h);
        canvas.scale(0.5f, 0.5f);
        canvas.scale(sx, sy, w, h);
        canvas.drawPicture(mPicture);
        canvas.restore();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight()));
        mPicture.endRecording();

        int x = getWidth() / 2;
        int y = getHeight() / 2;

        if (false) {
            canvas.drawPicture(mPicture);
        } else {
            drawPict(canvas, 0, 0, x, y, 1, 1);
            drawPict(canvas, x, 0, x, y, -1, 1);
            drawPict(canvas, 0, y, x, y, 1, -1);
            drawPict(canvas, x, y, x, y, -1, -1);
        }
    }

    @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        location[0] = getLeft();
        location[1] = getTop();
        dirty.set(0, 0, getWidth(), getHeight());
        return getParent();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = super.getChildCount();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final int childLeft = getPaddingLeft();
                final int childTop = getPaddingTop();
                child.layout(childLeft, childTop,
                        childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());

            }
        }
    }
}

解决方案

//Little changes in this piece of code
  float[] dst; //Global

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

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

        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;

                dst=mVerts;//Assign dst here just once

            }
        }

        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 = dst; //now you are applying wrap effect on the last effected pixels

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

    }

这篇关于Android的 - 图像扭曲效应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆