Android实现拖动小球跟随手指移动效果
作者:G_T_K
这篇文章主要为大家详细介绍了Android实现拖动小球跟随手指移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
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、布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/root" tools:context="com.syt.androidtest.androidtest1.MainActivity"> </LinearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。