Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球
一個(gè)關(guān)于自定義控件的小Demo,隨著手指移動(dòng)的小球。
先看下效果圖:
實(shí)現(xiàn)代碼如下:
1.自定義控件類
package com.dc.customview.view; 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; import android.view.WindowManager; public class DrawCircle extends View { //圓的初始位置 private int x = 100; private int y = 100; Context context; /** * 有style資源文件時(shí)調(diào)用 * @param context * @param attrs * @param defStyle */ public DrawCircle(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } /** * xml創(chuàng)建時(shí)調(diào)用 * @param context * @param attrs */ public DrawCircle(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } /** * java代碼創(chuàng)建時(shí)調(diào)用 * @param context */ public DrawCircle(Context context) { super(context); this.context = context; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 畫(huà)筆 Paint paint = new Paint(); paint.setColor(Color.RED); //繪制圓 //cx :圓心的x坐標(biāo) //cy :圓心的y坐標(biāo) //radius :圓的半徑 //paint :畫(huà)筆 canvas.drawCircle(x, y, 20, paint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: // 獲取當(dāng)前觸摸點(diǎn)的x,y坐標(biāo) x = (int) event.getX(); y = (int) event.getY(); break; } //獲取屏幕寬高 WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int width = manager.getDefaultDisplay().getWidth(); int heigh = manager.getDefaultDisplay().getHeight(); //重新繪制圓 ,控制小球不會(huì)被移出屏幕 if(x>=20 && y>=20 && x<=width-20 && y<=heigh-90){ invalidate(); } // 自己處理觸摸事件 return true; } }
2.引用自定義控件
第一種:xml中引用
<RelativeLayout 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:id="@+id/rl"> <!-- 自定義控件的全類名 --> <com.dc.customview.view.DrawCircle android:id="@+id/circle" android:layout_width="wrap_content" android:layout_height="wrap_content" > </com.dc.customview.view.DrawCircle> </RelativeLayout>
第二種:代碼中引用
package com.dc.customview; import com.dc.customview.view.DrawCircle; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.RelativeLayout; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取容器 RelativeLayout container = (RelativeLayout) findViewById(R.id.rl); //創(chuàng)建自定義控件 DrawCircle circle = new DrawCircle(this); //添加到容器 container.addView(circle); } }
以上,將Demo運(yùn)行到模擬器或手機(jī)上,即可實(shí)現(xiàn)一個(gè)紅色的圓,隨著手指觸摸移動(dòng)而移動(dòng)的效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 獲取內(nèi)外SD卡路徑幾種方法總結(jié)
這篇文章主要介紹了Android 獲得內(nèi)外SD卡路徑幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-12-12Android 兩個(gè)Fragment之間傳遞數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了Android 兩個(gè)Fragment之間傳遞數(shù)據(jù)實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼,實(shí)現(xiàn)該功能,需要的朋友可以參考下2016-12-12Android ViewFlipper的詳解及實(shí)例
這篇文章主要介紹了Android ViewFlipper的詳解及實(shí)例的相關(guān)資料,通過(guò)本文希望能幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果
這篇文章主要為大家詳細(xì)介紹了Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05Android自動(dòng)攔截與接聽(tīng)功能APK黑白名單
大家好,本篇文章主要講的是Android自動(dòng)攔截與接聽(tīng)功能APK黑白名單,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2021-12-12Android SDK 百度地圖通過(guò)poi城市內(nèi)檢索簡(jiǎn)介接口的使用
這篇文章主要介紹了Android SDK 百度地圖通過(guò)poi城市內(nèi)檢索簡(jiǎn)介接口的使用的相關(guān)資料,需要的朋友可以參考下2016-02-02