package com.itheima.girl;
import android.app.Activity;
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.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private Bitmap bitmap;
private Bitmap cr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
// 要把pre图片的副本加到 界面上。
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pre19);
cr = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
Canvas c=new Canvas(cr);
Paint p =new Paint();
p.setColor(Color.BLACK);
c.drawBitmap(bitmap, new Matrix(), p);
iv.setImageBitmap(cr);
// 给imageview设置一个触摸监听器。
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 手指第一次触摸到屏幕
System.out.println("摸到");
break;
case MotionEvent.ACTION_MOVE:// 手指位置改变了。
System.out.println("乱动" + event.getX() + "~" + event.getY());
int changeX = (int) event.getX();
int changeY = (int) event.getY();
for (int i = -5; i < 6; i++) {
for (int j = -5; j < 6; j++) {
if (Math.sqrt(i * i + j * j) <= 5) {
cr.setPixel(changeX + i, changeY + j,
Color.TRANSPARENT);
}
}
}
iv.setImageBitmap(cr);
break;
case MotionEvent.ACTION_UP:// 手指离开屏幕
System.out.println("松手");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return true;// True 监听器消费掉了事件, false 事件没有被消费(没有处理完).
}
});
}
}