Android拖动小球跟随手指移动Demo,供大家参考,具体内容如下
1、使用的知识点有自定义View,利用Canvas画球;
2、使用触摸时间来操作;
效果图:
代码如下:
1、自定义view;
public class DrawView extends View { public float currentX = 50; public float currentY = 50; public DrawView(Context context) { super(context); } public void onDraw(Canvas canvas){ super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawCircle(currentX,currentY,10,paint); } }
2、显示;
public class MainActivity extends Activity { public LinearLayout linearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout = (LinearLayout) findViewById(R.id.root); final DrawView drawView = new DrawView(this); drawView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { drawView.currentX = event.getX(); drawView.currentY = event.getY(); //通过draw组件重绘 drawView.invalidate(); return true; } }); linearLayout.addView(drawView); } }
3、布局
<&#63;xml version="1.0" encoding="utf-8"&#63;>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。