项目想添加一个小功能,当用手点击屏幕的出现一个圆,之后颜色慢慢变淡然后消失
public class TestViewActivity extends Activity {
public static int alpha = 255;
private boolean upFlag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SimpleView(this));
}
private class SimpleView extends ImageView{
int point_x;
int point_y;
int radius = 0;
public SimpleView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
point_x = (int) event.getX();//获取点击位置
point_y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
upFlag = true;
}
invalidate();
return true;
};
protected void onDraw(android.graphics.Canvas canvas) {
alpha -= 3;
canvas.drawColor(Color.BLUE); //设置背景色
Paint paint = new Paint();
paint.setColor(Color.WHITE); //设置画笔颜色
paint.setAlpha(alpha); //设置透明度
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
if (upFlag) {
++radius;
if (radius > 26) {
upFlag = false;
radius = 0;
alpha = 255;
}
if (radius == 18) {
alpha = 100;
}
canvas.drawCircle(point_x, point_y, radius, paint); //画圆
invalidate();
}
}
}
}
当检测到抬起的时候调用onDraw方法。代码中使用的是继承了ImageView,在ImageView中实现onDraw方法,同时在ondraw中必要的位置调用invalidate去实现重画,这样就可以给人以动态的感觉了。圆的半径、画笔的颜色、透明度的改变都可以根据自己的感觉去调试。