Android實(shí)現(xiàn)隨手指移動(dòng)小球
本文實(shí)例為大家分享了Android實(shí)現(xiàn)隨手指移動(dòng)小球的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)隨手指移動(dòng)小球,首先要使用paint畫筆在canvas畫布畫出一個(gè)圓,然后重寫OnTouchEvent(),進(jìn)行小球的坐標(biāo)的獲取和小球的重新繪畫。
package com.bwei.self_view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; /** * Created by ZhangTAO on 2017/11/2. */ public class MyView extends View{ // 初始化圓的位置 public int x = 200; public int y = 600; Context context; public MyView(Context context) { super(context,null); this.context = context; } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs,0); this.context = context; } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); this.context = context; } private void initView() { setBackgroundColor(Color.BLUE); } /** * @canvas 畫布 paint 畫筆 */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //建立一根畫筆 Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); paint.setFakeBoldText(true); //畫布 canvas.drawCircle(DipUtils.dip(getContext(),x), DipUtils.dip(getContext(),y), DipUtils.dip(getContext(),50),paint); } /** * 進(jìn)行設(shè)置touch移動(dòng) */ @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int action = event.getAction(); switch (action) { 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(); invalidate(); break; } // 獲取屏幕的寬高 WindowManager manger = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int width = manger.getDefaultDisplay().getWidth(); int height = manger.getDefaultDisplay().getHeight(); //重新繪制圓 ,控制小球不會(huì)被移除屏幕 if(x>=20 && y>=20 && x<=width-20 && y<height-20) { invalidate(); } //自己處理觸摸事件 return true; //修改當(dāng)前的坐標(biāo) // this.x =(int) event.getX(); // this.y =(int) event.getY(); //重繪小球 // this.invalidate(); // return true; } }
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
沉浸式狀態(tài)欄是指狀態(tài)欄與ActionBar顏色相匹配,隱藏導(dǎo)航欄,就是將導(dǎo)航欄隱藏,去掉下面的黑條。下面通過實(shí)例給大家詳解android沉浸式狀態(tài)欄與隱藏導(dǎo)航欄,感興趣的朋友一起看看2017-07-07Android筆記之:App模塊化及工程擴(kuò)展的應(yīng)用
這篇文章是android開發(fā)人員的必備知識(shí),是我特別為大家整理和總結(jié)的,不求完美,但是有用2013-04-04Android實(shí)現(xiàn)小米相機(jī)底部滑動(dòng)指示器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)小米相機(jī)底部滑動(dòng)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04通過Android trace文件分析死鎖ANR實(shí)例過程
遇到ANR(Application Not Responding)是比較常見的問題,產(chǎn)生ANR的原因有很多,比如CPU使用過高、事件沒有得到及時(shí)的響應(yīng)、死鎖等,下面將通過一次因?yàn)樗梨i導(dǎo)致的ANR問題,來說明如何通過trace文件分析ANR問題2013-06-06基于RxJava2實(shí)現(xiàn)的簡(jiǎn)單圖片爬蟲的方法
本篇文章主要介紹了基于RxJava2實(shí)現(xiàn)的簡(jiǎn)單圖片爬蟲的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Android?RecyclerChart其它圖表繪制示例詳解
這篇文章主要為大家介紹了Android?RecyclerChart其它圖表繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12詳解Android開發(fā)技巧之PagerAdapter實(shí)現(xiàn)類的封裝
這篇文章主要介紹了詳解Android開發(fā)技巧之PagerAdapter實(shí)現(xiàn)類的封裝,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Android實(shí)現(xiàn)截屏與截長(zhǎng)圖功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)截屏,以及Android實(shí)現(xiàn)截長(zhǎng)圖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android使用TransitionDrawable漸變切換多張圖片
這篇文章主要為大家詳細(xì)介紹了Android使用TransitionDrawable漸變切換多張圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android 判斷SIM卡屬于哪個(gè)移動(dòng)運(yùn)營(yíng)商的實(shí)現(xiàn)代碼
有時(shí)候我們需要在Android中獲取本機(jī)網(wǎng)絡(luò)提供商呢,這里簡(jiǎn)單分享下,方便需要的朋友2013-05-05