Android自定義組件跟隨自己手指主動畫圓
更新時間:2017年07月24日 14:55:08 作者:yangykaifa
這篇文章主要為大家詳細介紹了Android自定義組件跟隨自己手指主動畫圓,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(xiàn)跟隨手指畫圓的具體代碼,供大家參考,具體內(nèi)容如下
首先自己定義一個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); //設置畫筆的顏色 p.setColor(Color.RED); //繪制一個小球 canvas.drawCircle(currentX, currentY, 15, p); } /** * 為該組件的觸碰事件重寫事件處理方法 */ @Override public boolean onTouchEvent(MotionEvent event) { //改動currentX、currentY兩個屬性 currentX = event.getX(); currentY = event.getY(); //通知當前組件重繪自己 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); //獲取布局文件里LinearLayout容器 LinearLayout root = (LinearLayout)findViewById(R.id.root); //創(chuàng)建DrawView組件 final DrawView drawView = new DrawView(this); //設置自己定義組件的最小寬度、高度 drawView.setMinimumWidth(10); drawView.setMinimumHeight(10); root.addView(drawView); } }
效果:
除此之外:
還能夠用XML的方式:也是首先建一個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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)筆記之:消息循環(huán)與Looper的詳解
本篇文章是對Android中消息循環(huán)與Looper的應用進行了詳細的分析介紹,需要的朋友參考下2013-05-05Android使用自定義ImageView實現(xiàn)圓形圖片效果
本篇文章主要介紹了Android使用自定義ImageView實現(xiàn)圓形圖片效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例
這篇文章主要介紹了Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法,涉及Android針對button按鈕的事件響應及TextView動態(tài)添加相關操作技巧,需要的朋友可以參考下2017-11-11