Android 使用自定義View畫圓

2021-08-08 07:01:13 字數 1827 閱讀 1837

package com.gss.jrtt.mycircleview;

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.widget.toast;

/*** created by lenovo on 2017/9/15.

*/public class circleview extends view

//自定義veiw

在布局中使用,必須實現的乙個構造器

public circleview(context context, @nullable attributeset attrs )

@override

protected void ondraw(canvas canvas)

//要單點拖動,保證手指在圓上的時候才移動,我們需要判斷觸控的位置

@override

public boolean ontouchevent(motionevent event) else

break;

//移動的事件

case motionevent.action_move:

if (moveable)

break;

//抬起的事件

case motionevent.action_up:

break;

}return true;

}}最後直接在activity_main.xml實現布局就可以了

xml version=

"1.0"

encoding=

"utf-8"

?>

xmlns:

android

=""xmlns:

=""xmlns:

tools

=""android

:layout_width=

"match_parent"

android

:layout_height=

"match_parent"

tools

:context=

"com.gss.jrtt.mycircleview.mainactivity">

//你的包名加上繼承view的類名

android

:id=

"@+id/circle"

android

:layout_width=

"match_parent"

android

:layout_height=

"match_parent"

android

:layout_centerhorizontal=

"true"

>

com.gss.jrtt.mycircleview.circleview>

relativelayout>

執行起來看下圖,可以隨著手指移動

android自定義view之 組合view

最近工作比較輕鬆,沒有什麼事情幹,於是進入高產模式 呃。高產似xx 應該很多童鞋對自定義view這個東西比較牴觸,可能是聽網上說view比較難吧,其實自定義view並沒有很難 自定義view分為三種 1.自繪view 2.組合控制項view 3.重寫系統view 今天我們就來以乙個小例子講一下自定義...

Android實現隨機驗證碼 自定義View

一 問題描述 熟悉web開發中童鞋們都知道為了防止惡意破解 惡意提交 刷票等我們在提交表單資料時,都會使用隨機驗證碼功能。在android應用中我們同樣需要這一功能,該如何實現呢,下面我們就自定義乙個隨機驗證碼view控制項實現這一需求,並且具備通用性,需要的時候在介面中直接加入這個view元件即可...

Android開發之6步教你自定義view

如果你打算完全定製乙個view,那麼你需要實現view類 所有的android view都實現於這個類 並且實現確定尺寸的onmeasure 方法和確認繪圖的ondraw 方法。效果圖 第一步 繼承view,實現構造方法 public class smaileview extends view pu...