Android跟随手指运动的小球|Android跟随手指运动的小球 …

[java]view plain copy

  1. package yuhee.mars;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.view.View;
  7. public class DrawViewextends View{
  8. public float currentx = 40 ;
  9. publicfloat currenty = 50 ;
  10. public DrawView(Context context) {
  11. super(context);
  12. // TODO Auto-generated constructor stub
  13. }
  14. @Override
  15. protected void onDraw(Canvas canvas) {
  16. // TODO Auto-generated method stub
  17. super.onDraw(canvas);
  18. //创建画笔 ;
  19. Paint p = new Paint() ;
  20. p.setColor(Color.GREEN) ;
  21. //绘制一个小球 ;
  22. canvas.drawCircle(currentx, currenty, 15, p) ;
  23. }
  24. }
下面是该应用的Activity类:

[java]view plain copy
  1. package yuhee.mars;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnTouchListener;
  7. import android.widget.LinearLayout;
  8. public class Activity_06Activity extends Activity {
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. LinearLayout main = (LinearLayout)findViewById(R.id.root) ;
  14. final DrawView draw = new DrawView(this) ;
  15. draw.setMinimumWidth(300) ;
  16. draw.setMinimumHeight(500) ;
  17. draw.setOnTouchListener(new OnTouchListener()
  18. {
  19. @Override
  20. public boolean onTouch(View arg0, MotionEvent event) {
  21. // TODO Auto-generated method stub
  22. draw.currentx = event.getX() ;
  23. draw.currenty = event.getY() ;
  24. //通知Draw组件重绘 ;
  25. draw.invalidate() ;
  26. return true ;
  27. }
  28. }) ;
  29. main.addView(draw) ;
  30. }
  31. }
看下效果图:欢迎各位拍砖
【Android跟随手指运动的小球|Android跟随手指运动的小球 …】 Android跟随手指运动的小球|Android跟随手指运动的小球 …
文章图片
适合新手的很好的Demo" width="0" height="0">

    推荐阅读