Android實現(xiàn)拖動小球跟隨手指移動效果
更新時間:2020年08月24日 08:54:44 作者:G_T_K
這篇文章主要為大家詳細介紹了Android實現(xiàn)拖動小球跟隨手指移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Android拖動小球跟隨手指移動Demo,供大家參考,具體內(nèi)容如下
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程添加快捷方式(Short)到手機桌面的方法(含添加,刪除及查詢)
這篇文章主要介紹了Android編程添加快捷方式(Short)到手機桌面的方法,含有針對桌面快捷方式的添加,刪除及查詢的操作實現(xiàn)技巧,需要的朋友可以參考下2016-01-01Android 通過webservice上傳多張圖片到指定服務(wù)器詳解
這篇文章主要介紹了Android 通過webservice上傳多張圖片到指定服務(wù)器詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02Android中TelephonyManager類的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機硬件信息為例詳細分析了TelephonyManager類的使用技巧,需要的朋友可以參考下2015-09-09Android ListView里控件添加監(jiān)聽方法的實例詳解
這篇文章主要介紹了Android ListView里控件添加監(jiān)聽方法的實例詳解的相關(guān)資料,這里提供實例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08Android使用多線程進行網(wǎng)絡(luò)聊天室通信
這篇文章主要為大家詳細介紹了Android使用多線程進行網(wǎng)絡(luò)聊天室通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10