Android拖动小球跟随手指移动 Android实现拖动小球跟随手指移动效果
G_T_K 人气:0想了解Android实现拖动小球跟随手指移动效果的相关内容吗,G_T_K在本文为您仔细讲解Android拖动小球跟随手指移动的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android拖动小球跟随手指移动,Android拖动小球移动,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>
加载全部内容