撕衣服小游戏原理

/*撕衣服原理:
底层放无衣服人物,上层放穿衣服人物
当触摸划过使上层划过像素变透明*/

    public class MainActivity extends Activity {

    private ImageView iv;
    private Bitmap srcBM;
    private Bitmap copyBM;
    private Canvas canvas;
    private Paint paint;
    private Matrix matrix;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);

        srcBM = BitmapFactory.decodeResource(getResources(), R.drawable.pre);
        copyBM = Bitmap.createBitmap(srcBM.getWidth(),srcBM.getHeight(), srcBM.getConfig());

        canvas = new Canvas(copyBM);
        paint = new Paint();
        matrix = new Matrix();
        canvas.drawBitmap(srcBM, matrix, paint);

        iv.setImageBitmap(copyBM);
        iv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                case MotionEvent.ACTION_MOVE:
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    for(int i = -15;i < 15;i++){
                        for(int j = -10;j < 10;j++){
                            copyBM.setPixel(x+i, y+j, Color.TRANSPARENT);
                        }
                    }
                    iv.setImageBitmap(copyBM);                  
                    break;
                }
                return true;
            }
        });
    }
}
展开阅读全文

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