Android开发 - 跟随手指的小球实现
fmms
13年前
<p>今天要实现的是一个跟随手指的小球,说白了就是让小球按着手指滑动的轨迹运动,实现起来还是比较容易的。</p> <p>用到的类是drawView,我们先自定义一个DrawView组件。</p> <p>DrawView.java:</p> <pre class="brush:java; toolbar: true; auto-links: false;">package cn.csdn.android.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class DrawView extends View{ public float currentX = 300; public float currentY = 500; public DrawView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint p = new Paint(); p.setColor(Color.RED); canvas.drawCircle(currentX, currentY, 15, p); } }</pre> <p></p> <p>==================================</p> <p>在main.xml文件中给一个android:id="@+id/root";</p> <p>然后写java代码实现跟踪效果:</p> <pre class="brush:java; toolbar: true; auto-links: false;">CustomView.java package cn.csdn.android.view; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.LinearLayout; public class CustomViewActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout main = (LinearLayout) findViewById(R.id.root); final DrawView draw = new DrawView(this); draw.setMinimumWidth(300); draw.setMinimumHeight(500); draw.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { draw.currentX = event.getX(); draw.currentY = event.getY(); draw.invalidate(); return true; } }); main.addView(draw); } }</pre>转自: <a href="/misc/goto?guid=4959500610457750824" target="_blank">http://blog.csdn.net/yangbo_hbzjk/article/details/7193544</a> <p></p> <p></p>