Androidx学习笔记(76)--- 撕衣服

来源:互联网 发布:如何化行最简形矩阵 编辑:程序博客网 时间:2024/04/20 16:29

撕衣服

  • 原理:把穿内衣和穿外衣的照片重叠显示,内衣照在下面,用户滑动屏幕时,触摸的是外衣照,把手指经过的像素都置为透明,内衣照就显示出来了

     iv.setOnTouchListener(new OnTouchListener() {    @Override    public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_MOVE:            int newX = (int) event.getX();            int newY = (int) event.getY();            //把指定的像素变成透明            copyBm.setPixel(newX, newY, Color.TRANSPARENT);            iv.setImageBitmap(copyBm);            break;        }        return true;    }});
  • 每次只设置一个像素点太慢,以触摸的像素为圆心,半径为5画圆,圆内的像素全部置为透明

    for (int i = -5; i < 6; i++) {    for (int j = -5; j < 6; j++) {        if(Math.sqrt(i * i + j * j) <= 5)            copyBm.setPixel(newX + i, newY + j, Color.TRANSPARENT);    }}



public class MainActivity extends Activity {
 
private Bitmap bmCopy;
private ImageView iv;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.awaiyi);
bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
Paint paint = new Paint();
Canvas canvas = new Canvas(bmCopy);
canvas.drawBitmap(bmSrc, new Matrix(), paint);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
for(int i = -5; i <= 5; i++){
for(int j = -5; j <= 5; j++){
//把用户划过的坐标置为透明色
//改变指定的像素颜色
if(Math.sqrt(i*i + j*j) <= 5){
if(x + i < bmCopy.getWidth() && y + j < bmCopy.getHeight() && x + i >= 0 && y + j >= 0){
bmCopy.setPixel(x + i, y + j, Color.TRANSPARENT);
iv.setImageBitmap(bmCopy);
}
}
}
}
break;
 
}
return true;
}
});
}
 
 
}
0 0
原创粉丝点击