Android Bitmap之擦掉图片(扒衣服)

来源:互联网 发布:人证比对软件 编辑:程序博客网 时间:2024/05/17 06:27
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:src="@drawable/after" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"         /></RelativeLayout>
public class MainActivity extends Activity {    private ImageView iv;    // 可以修改的位图    private Bitmap alertBitmap;    private Canvas canvas;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv = (ImageView) findViewById(R.id.iv);        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),                R.drawable.pre);        // 创建一个空白的原图的拷贝        alertBitmap = Bitmap.createBitmap(bitmap.getWidth(),                bitmap.getHeight(), bitmap.getConfig());        canvas = new Canvas(alertBitmap);        Paint paint = new Paint();        paint.setColor(Color.BLACK);        canvas.drawBitmap(bitmap, new Matrix(), paint);        iv.setImageBitmap(alertBitmap);        iv.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                switch (event.getAction()) {                case MotionEvent.ACTION_DOWN:// 手指按下屏幕                    System.out.println("action down");                    break;                case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动                    int x = (int) event.getX();// getX,相对于控件,getRawX,相对于屏幕                    int y = (int) event.getY();                    System.out.println("设置(" + x + "," + y + ")透明颜色");                    for (int i = -4; i < 5; i++) {                        for (int j = -4; j < 5; j++) {                            try {                                alertBitmap.setPixel(x + i, y + j,                                        Color.TRANSPARENT);                            } catch (Exception e) {                                // TODO: handle exception                            }                        }                    }                    iv.setImageBitmap(alertBitmap);                    break;                case MotionEvent.ACTION_UP:// 手指离开屏幕                    MediaPlayer.create(getApplicationContext(), R.raw.higirl)                            .start();                    break;                }                return true;// 代表事件被消费, 可以重复循环的处理事件            }        });    }}

参考:
Android图片处理

0 0
原创粉丝点击