2010-03-27 22 views
7

當我觸摸屏幕的那一點,會發光(只是閃光或閃閃發光)一段時間。怎麼做?任何示例或想法?我必須實施把按鈕放在上面。正是當我觸摸屏幕它會發光一些時間,然後按鈕將出現在我觸摸的點。在Android中觸摸屏幕時會發光嗎?

回答

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; 
    } 
} 
+0

如果我有一個ViewPager,它需要onTouchEvent? – Machado 2015-04-02 11:40:13