【Android 应用开发】自定义View 和 ViewGroup
一. 自定義View介紹
自定義View時, 繼承View基類, 并實現其中的一些方法.
(1) ~ (2) 方法與構造相關
(3) ~ (5) 方法與組件大小位置相關
(6) ~ (9) 方法與觸摸按鍵相關
(10) ~ (12) 方法與窗口 焦點相關
(1) 構造方法
該構造方法在創建View實例, 或者從XML布局中加載并構建界面的時候調用.
(2)加載回調方法
protected void onFinishInflate()回調方法, 從XML布局中加載該重寫的View組件的時候, 就會回調這個方法;
(3)測量方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)這個方法用來檢測View組件以及該View組件包含的子組件的大小
(4)定位組件方法
protected void onLayout(boolean changed, int left, int top, int right,int bottom)被重寫的View組件分配在其中的子組件的 位置 和 大小的時候, 回調這個方法;
(5)大小改變方法
protected void onSizeChanged(int w, int h, int oldw, int oldh)當組件大小被改變的時候回調該方法;
(6)按鍵方法
public boolean onKeyDown(int keyCode, KeyEvent event)當某個鍵被按下時觸發該方法;
(7)松開鍵方法
public boolean onKeyUp(int keyCode, KeyEvent event)當某個鍵松開的時候調用該方法;
(8)軌跡球事件方法
public boolean onTrackballEvent(MotionEvent event)發生軌跡球事件時觸發該方法;
(9)觸摸方法
public boolean onTouchEvent(MotionEvent event)當發生觸摸時間時觸發該方法;
(10)焦點改變方法
public void onWindowFocusChanged(boolean hasWindowFocus)當組件得到, 失去焦點的時候回調的方法;
(11)組件進入窗口方法
protected void onAttachedToWindow()當把組件放入窗口的時候, 回調這個方法
(12)組件分離窗口方法
protected void onAttachedToWindow()當把組件從某個窗口分離觸發的方法
(13)窗口可見性改變方法
protected void onWindowVisibilityChanged(int visibility)當包含該組件的窗口發生改變的時候觸發的方法
二. 實現一個跟隨手指的小球View
1. 自定義View
自定義一個View組件鋪滿全屏, 在繪制該View組件的時候, 在onDraw()方法中根據一個xy坐標繪制一個小球;
這個xy坐標在觸摸回調方法onTouchEvent()方法中動態改變, 當檢測到觸摸位置發生改變, 那么就重新給xy坐標賦值, 并且調用invalidate()方法重繪該組件, invalidate()方法執行后, 會回調onDraw()方法;
public class FollowBallView extends View {public float currentX = 40;public float currentY = 50;Paint paint = new Paint();public FollowBallView(Context context) {super(context);}public FollowBallView(Context context, AttributeSet set) {super(context, set);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setColor(Color.RED);canvas.drawCircle(currentX, currentY, 15, paint);}@Overridepublic boolean onTouchEvent(MotionEvent event) {currentX = event.getX();currentY = event.getY();//重繪invalidate();return true;} }
2. xml文件
在這個xml文件中, 引入自定義的布局, 使用完整的類名包名可以引入該自定義View組件;
引入組件后, 充滿整個布局;
<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" ><shuliang.han.followball.FollowBallViewandroid:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>
3. Activity中顯示該組件
public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);} }
4. 效果圖
三. ViewGroup簡介
1.View和ViewGroup關系
UI組件在Android中的位置?: Android中的UI組件大部分都放在android.widget 或者 android.view包中; ?View組件 和 ViewGroup組件都在android.view包中;
View 和 ViewGroup關系?:?
繼承關系 : ViewGroup是View的子類, 因此ViewGroup有View的一切屬性, 可以當做View來使用, ViewGroup主要是當做容器使用; View是小控件widget和容器組件ViewGroup的父類, ViewGroup是布局如LinearLayout的基類;
包含關系 : ViewGroup中可以包含View和ViewGroup, ViewGroup可以無限嵌套;
View和ViewGroup關系圖解?:?
抽象類?: ViewGroup是一個抽象類, 作為容器的類通常是ViewGroup的子類; ViewGroup子類對象有 : LinearLayout, FrameLayout等布局;
2. ViewGroup中的內部類
兩個內部類?:?
ViewGroup通過Viewgroup.LayoutParams和ViewGroup.MarginLayoutParams來控制容器中的組件;
ViewGroup.LayoutParams支持的XML屬性?:?
android:layout_height屬性(指定布局高度) 和 android:layout_width屬性(指定布局寬度), 這兩個屬性的值可以為 fill_parent, match_parent, wrap_content, 其中充滿布局空間推薦使用match_parent;
組件的寬高 與 布局的寬高?: Android的組件的高度和寬度不是其實際的寬度和高度, 組件實際的高度和寬度同樣受布局的寬高影響, 例如設置組件的寬度為100px, 如果設置布局寬度(layout_width)為match_parent, 那么組件的實際寬度就會被拉寬到整個布局, 如果設置布局的寬度為wrap_content, 那么組件的實際寬度就是100px;
ViewGroup.MarginLayoutParams支持的XML屬性?:?
android:layout_marginBottom : 指定該子組件到父容器下邊界的距離, 同理android:layout_marginTop, android:layout_marginLeft, android:layout_marginRight,?
3. View介紹
View可以有兩種方式創建 : 一種是靠XML文件創建, 一種是靠代碼創建;
這里只是簡單的介紹下, View詳細的屬性會單獨有一篇博客進行總結;
View創建關注的方面?:?
(1)設置屬性
長寬等屬性可以在XML中設置, 也可以在代碼中設置;?
(2)請求焦點
可以通過函數實現焦點轉變, 可以根據不同的焦點設置背景;?
焦點類別 : 可獲取的焦點, 不可獲取的焦點, 可以獲取但是正在觸摸狀態下的焦點;
(3)設置事件監聽
View在本身發生變化的時候, 會將信息廣播出去, 這邊變化例如 : 點擊, 焦點改變等;
一個事件的廣播到來, 該事件就會傳遞到相應的View中對應的監聽器里, 相當于回調View中的監聽器方法;
(4)設置顯示與隱藏
View可以在XML或者代碼中設置是否顯示或者隱藏. 同樣也可以設置滾動條等設置;
總結
以上是生活随笔為你收集整理的【Android 应用开发】自定义View 和 ViewGroup的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 应用开发】Blueto
- 下一篇: 【Android 应用开发】Androi