Android自定義組件跟隨自己手指主動(dòng)畫圓
本文實(shí)例為大家分享了Android實(shí)現(xiàn)跟隨手指畫圓的具體代碼,供大家參考,具體內(nèi)容如下
首先自己定義一個(gè)View子類:
package com.example.androidtest0.myView; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; 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 attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //設(shè)置畫筆的顏色 p.setColor(Color.RED); //繪制一個(gè)小球 canvas.drawCircle(currentX, currentY, 15, p); } /** * 為該組件的觸碰事件重寫事件處理方法 */ @Override public boolean onTouchEvent(MotionEvent event) { //改動(dòng)currentX、currentY兩個(gè)屬性 currentX = event.getX(); currentY = event.getY(); //通知當(dāng)前組件重繪自己 invalidate(); return true; } }
主界面XML:
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root" android:orientation="vertical" > </LinearLayout>
主activity:
package com.example.androidtest0; import com.example.androidtest0.myView.DrawView; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; public class CustomView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_layout); //獲取布局文件里L(fēng)inearLayout容器 LinearLayout root = (LinearLayout)findViewById(R.id.root); //創(chuàng)建DrawView組件 final DrawView drawView = new DrawView(this); //設(shè)置自己定義組件的最小寬度、高度 drawView.setMinimumWidth(10); drawView.setMinimumHeight(10); root.addView(drawView); } }
效果:
除此之外:
還能夠用XML的方式:也是首先建一個(gè)View的子類。和上面一樣。
然后主界面XML例如以下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root" android:orientation="vertical" > <com.example.androidtest0.myView.DrawView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
主activity文件例如以下:
package com.example.androidtest0; import com.example.androidtest0.myView.DrawView; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; public class CustomView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_layout); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)每天定時(shí)提醒功能
本文主要介紹了Android每天定時(shí)提醒功能、定時(shí)功能、鬧鐘的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-04-04Android開發(fā)筆記之:消息循環(huán)與Looper的詳解
本篇文章是對Android中消息循環(huán)與Looper的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Flutter網(wǎng)絡(luò)請求Dio庫的使用及封裝詳解
本文主要介紹了Flutter網(wǎng)絡(luò)請求Dio庫的使用及封裝詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Android通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放的實(shí)例代碼
這篇文章主要介紹了Android通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放的實(shí)例代碼,完成了點(diǎn)擊圖片就能瀏覽大圖的功能,并且在瀏覽大圖的時(shí)候還可以通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放。2018-05-05Android ListView滾動(dòng)到指定的位置
這篇文章主要給大家介紹了Android中的ListView如何滾動(dòng)到指定的位置,文章給出了兩種解決的方法,并給出詳細(xì)的示例代碼,相信會(huì)對大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們下面來一起看看吧。2016-10-10Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
本篇文章主要介紹了Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android開發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法示例
這篇文章主要介紹了Android開發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法,涉及Android針對button按鈕的事件響應(yīng)及TextView動(dòng)態(tài)添加相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Android實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06