Android 自訂控制項開發入門(二),android控制項

來源:互聯網
上載者:User

Android 自訂控制項開發入門(二),android控制項

上一次我們講了一堆實現自訂控制項的理論基礎,列舉了View類一些可以重寫的方法,我們對這些方法的重寫是我們繼承View類來派生自訂控制項的關鍵


我通過一個最簡單的例子給大家展示了這一個過程,無論是多麼複雜的自訂控制項,思路總是這樣子的,但是因為我們僅僅重寫了onDraw方法使得大家覺得怪怪的,作為一個控制項,我們居然還要為了他的實現為其增加麻煩的監聽,這就不能叫做控制項了。

下面再給大家介紹一個經常重寫的方法法:publicboolean onTouchEvent (MotionEvent event)


通過這個方法,我們就把寫在Activity的監聽部分內建在控制項內部了,這才能叫做一個完整的控制項,其功能是建立一片地區,並其中包含一個可以根據手指觸摸而改變位置的小球。

下面我們來看一下這個觸摸事件方法:

 publicboolean onTouchEvent (MotionEvent event)


Added in API level 1


Implement this method to handle touch screen motionevents.

If this method is used to detect click actions, it isrecommended that the actions be performed by implementing and calling performClick(). This willensure consistent system behavior, including:

obeying click sound preferences

dispatching OnClickListener calls

handling ACTION_CLICK whenaccessibility features are enabled


Parameters

event

The motion event.


Returns

True if the event was handled, false otherwise.

 

這樣我們就可以把我們剛才在Activity的類中做的工作放到我們的自訂控制項中來實現

只要去掉剛才的setter 和 getter 然後重寫這個觸摸事件的方法就可以了:


public boolean onTouchEvent(MotionEvent motionevent){CircleX  = motionevent.getX();CircleY  = motionevent.getY();this.invalidate();return true;

這樣我們只需要再簡單的在xml中調用,一切都愉快的解決了!

這個例子我會和第一個一併放在一起的,就和我之前寫的適配器的教程一樣,源碼我會整理再一起再給大家。第二個程式我注釋就不那麼注意啦……不是我懶,最近比較忙(其實就是懶)


下面我貼一下代碼:


Activity的代碼:


package com.example.customcomponentsdemo.Activity;import com.example.customcomponentsdemo.R;import android.app.Activity;import android.content.Context;import android.os.Bundle;public class MoveBallActivity2 extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_moveball2);}}


簡介了好多有木有!


自訂View版本2的代碼:

package com.example.customcomponentsdemo.component;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 DrawView2 extends View{private Context context;private float CircleX = 100;private float CircleY = 100;private float CircleR = 10;public DrawView2(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;}@Overridepublic void onDraw(Canvas canves){Paint paint = new Paint();paint.setColor(Color.BLUE);canves.drawCircle(CircleX, CircleY, CircleR, paint);}@Overridepublic boolean onTouchEvent(MotionEvent motionevent){CircleX  = motionevent.getX();CircleY  = motionevent.getY();this.invalidate();return true;}}

這樣就簡潔了好多!


還有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:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="這是MoveBall的Demo 版本2"        android:textColor="@color/white" >    </TextView>    <com.example.customcomponentsdemo.component.DrawView2        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="10dp" >    </com.example.customcomponentsdemo.component.DrawView2>        </LinearLayout>


 這樣我們的第二個教程也就先到這裡了,這次的主題不是這個ontouch方法,而是要告訴大家,自訂控制項的核心是重寫這些方法,並添加所需要的邏輯,View的方法不多也不少,我就用這個例子給大家拋磚引玉一下,希望大家在自訂自己的控制項並選擇了繼承View這條路時,要花時間去瞭解和理解這些方法的重寫方法,這是十分重要的。 下次再給大家介紹一下如果自訂的View需要有自訂的屬性我們該如何處理,下一講也將會是這個系列完結篇了,因為自訂View之路還有很遠,我也沒有舉一些很難的例子,我認為基礎知識只有這些,學習了這些之後自訂控制項的基礎也就講完了,剩下的是大家在基礎之上發揮了!之後如果有比較好的例子我還會繼續補充的。


源碼我會在下次一併發給大家連結的,希望大家能學到一些東西~


另外我也是學生,如果有寫的不好或者有錯誤的地方還請大家多多指教,謝謝!


android開發 自訂控制項怎在xml檔案中配置 尤其是最上面的<LinearLayout >那部分

自已控制項就是你先寫好你的控制項代碼,假如你自訂了一個MyButton,然後這個MyButton類你放在com.app包裡面,那麼你在xml配置代碼如下

<com.app.MyButton
android:layout_heigjt........... 屬性/>

希望能幫到您
 
Android 自訂控制項開發,怎擷取子控制項的margin的值(進……)

先通過getChildAt(int index)擷取到子控制項,再通過lp = getLayoutParams()擷取到子控制項的參數,lp.leftMargin就是你想要的數值
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.