Android跟随手指运动的小球 …

[java]  view plain copy
  1. package yuhee.mars;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.view.View;  
  8.   
  9.   
  10. public class DrawView  extends View{  
  11.           
  12.           
  13.           
  14.   
  15.         public float currentx = 40 ;  
  16.         public  float currenty = 50 ;  
  17.           
  18.         public DrawView(Context context) {  
  19.                 super(context);  
  20.                 // TODO Auto-generated constructor stub  
  21.         }  
  22.         @Override  
  23.         protected void onDraw(Canvas canvas) {  
  24.                 // TODO Auto-generated method stub  
  25.                 super.onDraw(canvas);  
  26.                 //创建画笔 ;  
  27.                 Paint p = new Paint() ;  
  28.                 p.setColor(Color.GREEN) ;  
  29.                 //绘制一个小球 ;  
  30.                 canvas.drawCircle(currentx, currenty, 15, p) ;  
  31.     }  
  32.         }  

下面是该应用的Activity类:

 

[java]  view plain copy
  1. package yuhee.mars;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.widget.LinearLayout;  
  9.   
  10. public class Activity_06Activity extends Activity {  
  11.       
  12.     @Override  
  13.             public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         LinearLayout main = (LinearLayout)findViewById(R.id.root) ;  
  17.         final DrawView draw = new DrawView(this) ;  
  18.         draw.setMinimumWidth(300) ;  
  19.         draw.setMinimumHeight(500) ;  
  20.         draw.setOnTouchListener(new OnTouchListener()  
  21.         {  
  22.   
  23.                         @Override  
  24.                         public boolean onTouch(View arg0, MotionEvent event) {  
  25.                                 // TODO Auto-generated method stub  
  26.                                 draw.currentx = event.getX() ;  
  27.                                 draw.currenty = event.getY() ;  
  28.                                 //通知Draw组件重绘 ;  
  29.                                 draw.invalidate() ;  
  30.                                 return true ;  
  31.                                   
  32.                         }  
  33.                   
  34.         }) ;  
  35.         main.addView(draw) ;  
  36.         }  
  37. }  
看下效果图:欢迎各位拍砖

 

你可能感兴趣的:(Android跟随手指运动的小球 …)