2018-03-19 图片处理(八)----撕衣服

笔记如下



Video_2018-03-16_102658.gif



layout文件中:




    
    



MainActivity中

package com.chen.tornclothes;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

    private ImageView iv_after;
    private ImageView iv_pre;

    private Bitmap copyBitmap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_after = (ImageView) findViewById(R.id.iv_after);
        iv_pre = (ImageView) findViewById(R.id.iv_pre);

        iv_after.setImageResource(R.mipmap.after);
        iv_pre.setImageResource(R.mipmap.pre);

        //监听上面图片的touch方法

        iv_pre.setOnTouchListener(this);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pre);

        copyBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
        Canvas canvas = new Canvas(copyBitmap);
        Paint paint = new Paint();
        Matrix matrix = new Matrix();
        canvas.drawBitmap(bitmap,matrix,paint);

    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        //滑动的过程中,取到原图的拷贝,然后修改拷贝的图片复制给ImageView
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                //按下去的操作:颜色的去除
                int downX = (int) (event.getX() + 0.5f);
                int downY = (int) (event.getY() + 0.5f);
                clearColor(downX,downY);
                break;
            case MotionEvent.ACTION_MOVE:
                //移动,颜色去除
                //按下去的操作:颜色的去除
                int moveX = (int) (event.getX() + 0.5f);
                int moveY = (int) (event.getY() + 0.5f);
                clearColor(moveX,moveY);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }


        return true;
    }

    private void clearColor(int x,int y){
        //颜色的去除
        //System.out.println("x : "+x +"  y : " +y);

        for(int i=-7;i<=7;i++){
            for (int j=-7;j<=7;j++){
                try{
                    if (i * i + j * j <= 49) {
                        copyBitmap.setPixel(x+i,y+j, Color.TRANSPARENT);
                    }
                }catch(Exception e){

                }
            }
        }
        iv_pre.setImageBitmap(copyBitmap);
        //Toast.makeText(this, "颜色去除了.....", Toast.LENGTH_SHORT).show();
    }

}

你可能感兴趣的:(2018-03-19 图片处理(八)----撕衣服)