思路:
类似前面的画画板
不过这里是通过将滑动的像素点设置成透明颜色
altBitMap.setPixel(x + i, y + j, Color.TRANSPARENT)
而且是双层for循环设置以这个点为中心的圆为透明颜色(一个像素一个像素的刮太慢了。。。)
而且在设置的时候注意边界问题。
代码:
package cn.zsp.guaguale;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Bitmap altBitMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.iv);
Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.guagua);
altBitMap = Bitmap.createBitmap(bitMap.getWidth(), bitMap.getHeight(), bitMap.getConfig());
Canvas canvas = new Canvas(altBitMap);
Paint paint = new Paint();
canvas.drawBitmap(bitMap, new Matrix(), paint);
imageView.setImageBitmap(altBitMap);
imageView.setOnTouchListener(new View.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();
Toast.makeText(MainActivity.this, "移动" + x + "," + y, Toast.LENGTH_SHORT).show();
for (int i = -20; i < 20; i++) {
for (int j = -20; j < 20; j++) {
if (Math.sqrt((i * i) + (j * j)) <= 20) {//设置成透明 以x,y 为半径的圆 遍历
//宽 高 不能越界
if (x + i >= 0 && x + i < altBitMap.getWidth() && y + j >= 0 && y + j < altBitMap.getHeight()) {
altBitMap.setPixel(x + i, y + j, Color.TRANSPARENT);
}
}
}
}
imageView.setImageBitmap(altBitMap);
break;
}
return true;
}
});
}
}