7

当我触摸屏幕上的任何位置时,该点会发光(不过像闪光或闪闪发光)一段时间。怎么做?任何例子或想法?我必须实施以在其上放置按钮。确切地说,当我触摸屏幕时,它会发光一段时间,然后按钮将出现在我触摸的点上。

4

1 回答 1

11

您将不得不创建一个自定义视图并覆盖 ontouchevent 和绘图。这是一个非常简单的例子。如果您使用包名,即 com.test.CustomView,您可以在 xml 布局中引用自定义视图。

 public class CustomView extends ImageView{
    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomView(Context context) {
        super(context);
    }
    boolean drawGlow = false;
    //this is the pixel coordinates of the screen
    float glowX = 0;
    float glowY = 0;
    //this is the radius of the circle we are drawing
    float radius = 20;
    //this is the paint object which specifies the color and alpha level 
    //of the circle we draw
    Paint paint = new Paint();
    {
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setAlpha(50);
    };

    @Override
    public void draw(Canvas canvas){
        super.draw(canvas);
        if(drawGlow)
            canvas.drawCircle(glowX, glowY, radius, paint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            drawGlow = true;
        }else if(event.getAction() == MotionEvent.ACTION_UP)
            drawGlow = false;

        glowX = event.getX();
        glowY = event.getY();
        this.invalidate();
        return true;
    }
}   
于 2010-03-27T17:49:43.657 回答