Android開發(fā)實(shí)現(xiàn)跟隨手指的小球效果示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)跟隨手指的小球效果。分享給大家供大家參考,具體如下:
配置DrawView類用于繪制小球
public class DrawView extends View {
public float currentX = 40;
public float currentY = 50;
//定義并創(chuàng)建畫筆
Paint p = new Paint();
public DrawView(Context context)
{
super(context);
}
public DrawView(Context context , AttributeSet set)
{
super(context,set);
}
@Override
public void onDrawForeground(Canvas canvas) {
super.onDrawForeground(canvas);
//設(shè)置畫筆顏色
p.setColor(Color.RED);
//繪制一個小球
canvas.drawCircle(currentX , currentY , 30 , p);
}
//為組建的觸碰實(shí)踐重寫處理方法
@Override
public boolean onTouchEvent(MotionEvent event) {
//修改currentX,currentY的兩個屬性
currentX = event.getX();
currentY = event.getY();
//通知當(dāng)前組建重繪自己
invalidate();
//放回true表明該處理方法已經(jīng)處理該事件
return true;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取Linearlayout布局容器
LinearLayout root = (LinearLayout) findViewById(R.id.root);
//創(chuàng)建DrawView組件
final DrawView draw = new DrawView(this);
//設(shè)定自定義組件的最小寬度、高度
draw.setMinimumWidth(300);
draw.setMinimumHeight(500);
root.addView(draw);
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.a30797.myapplication.DrawView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
**注:**由上面布局,已經(jīng)添加了自定義組件,因此Activity代碼可簡化為:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
示例:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android自定義圓形View實(shí)現(xiàn)小球跟隨手指移動效果
- Android實(shí)現(xiàn)拖動小球跟隨手指移動效果
- Android實(shí)現(xiàn)跟隨手指拖動并自動貼邊的View樣式(實(shí)例demo)
- Android中View跟隨手指移動效果
- Android自定義View圓形和拖動圓、跟隨手指拖動效果
- Android實(shí)現(xiàn)View拖拽跟隨手指移動效果
- Android中View跟隨手指滑動效果的實(shí)例代碼
- Android自定義View圓形和拖動圓跟隨手指拖動
- Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法
相關(guān)文章
Android Toast的幾種使用方式及注意事項(xiàng)
Toast是Android中常用的組件,下面介紹下Toast使用的幾種方式和注意事項(xiàng),本文給大家分享Toast的使用方式,感興趣的朋友一起看看吧2024-02-02
android針對json數(shù)據(jù)解析方法實(shí)例分析
這篇文章主要介紹了android針對json數(shù)據(jù)解析方法,以實(shí)例形式較為詳細(xì)的分析了Android操作json格式數(shù)據(jù)的各種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android編程實(shí)現(xiàn)長按Button按鈕連續(xù)響應(yīng)功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)長按Button按鈕連續(xù)響應(yīng)功能,涉及Android自定義按鈕及事件響應(yīng)操作相關(guān)技巧,需要的朋友可以參考下2017-01-01
Android實(shí)現(xiàn)TextView兩端對齊的方法
這篇文章主要介紹了Android實(shí)現(xiàn)TextView兩端對齊的方法,需要的朋友可以參考下2016-01-01
Android使用FontMetrics對象計(jì)算位置坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android使用FontMetrics對象計(jì)算位置坐標(biāo),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android AlertDialog對話框詳解及實(shí)例
這篇文章主要介紹了Android AlertDialog對話框詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android彈窗ListPopupWindow的簡單應(yīng)用詳解
這篇文章主要為大家詳細(xì)介紹了Android彈窗ListPopupWindow的簡單應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Android中解決RecyclerView各種點(diǎn)擊事件的方法
這篇文章主要介紹了Android中解決RecyclerView各種點(diǎn)擊事件的方法,完美解決RecyclerView點(diǎn)擊事件、長按事件、子項(xiàng)點(diǎn)擊事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時間功能示例
這篇文章主要介紹了Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時間功能,結(jié)合實(shí)例形式分析了Android布局、事件響應(yīng)、監(jiān)聽以及時間獲取相關(guān)操作技巧,需要的朋友可以參考下2019-04-04

