Android撕美女衣服小案例

package com.itheima74.syf;

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 {

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

        final ImageView iv = (ImageView) findViewById(R.id.iv);

        // 创建原图
        Bitmap bitmap_src = BitmapFactory.decodeResource(getResources(), R.drawable.pre19);

        // 创建副本
        final Bitmap bitmap_copy = Bitmap.createBitmap(bitmap_src.getWidth(), bitmap_src.getHeight(),
                bitmap_src.getConfig());
        Canvas canvas = new Canvas(bitmap_copy);
        Paint paint = new Paint();
        canvas.drawBitmap(bitmap_src, new Matrix(), paint);

        // 将副本展示到iv        iv.setImageBitmap(bitmap_copy);

        // 设置iv的触摸事件,让所到之处变透明
        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    for (int i = -30; i <= 30; i++) {
                        for (int j = -30; j <= 30; j++) {
                            if (Math.sqrt(i * i + j * j) < 30) {
                                try {
                                    bitmap_copy.setPixel((int) event.getX() + i, (int) event.getY() + j,
                                            Color.TRANSPARENT);
                                } catch (Exception e) {
                                    // java.lang.IllegalArgumentException: x must be >= 0 不打印异常
                                }
                            }
                        }
                    }
                    // 更新UI
                    iv.setImageBitmap(bitmap_copy);
                }
                return true;
            }
        });
    }
}


          

没有更多推荐了,返回首页