Android 如何自定義一個簡單的組件和自定義的點擊事件(中級)

Android 如何自定義一個簡單的組件和自定義的點擊事件(中級)

這裏自定義組件的代碼是最初學習時下載的代碼片段 忘了出處 

 直接上代碼 注意看代碼中的註釋  

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

               xmlns:my="http://schemas.android.com/apk/res/com.skyoceanone.zidingyiView"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    />

    <com.skyoceanone.zidingyiView.zidingyi

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    my:textColor="#ff00ff"

    my:textSize="10sp"

    android:id="@+id/zidingyi">

    </com.skyoceanone.zidingyiView.zidingyi>

</LinearLayout>

values中定義 attr.xml 來定義你的組件的屬性 個人認爲這個xml就是起到一箇中介作用 連接 Layout和 代碼的一個橋樑

<?xml version="1.0" encoding="utf-8"?>

<resources>

     <declare-styleable name="MyView">  

        <attr format="color" name="textColor" />

        <attr name="textSize" format="dimension"/>  

    </declare-styleable>

</resources>

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Paint.Style;

import android.util.AttributeSet;

import android.view.KeyEvent;

import android.view.View;

/**

 * 1 這個是自定義的TextView.

 * 2至少需要重載構造方法和onDraw方法  

 * 3對於自定義的View如果沒有自己獨特的屬性,可以直接在xml文件中使用就可以了 

 * 4如果含有自己獨特的屬性,那麼就需要在構造函數中獲取屬性文件attrs.xml中自定義屬性的名稱 

 * 5並根據需要設定默認值,放在在xml文件中沒有定義。

 * 6如果使用自定義屬性,那麼在應用xml文件中需要加上新的schemas

 *7比如這裏是xmlns:my="http://schemas.android.com/apk/res/com.skyoceanone.zidingyiView"

 * 8其中xmlns後的“my”是自定義的屬性的前綴,res後的是我們自定義 View所在的包 

 */

public class zidingyi extends View{

Paint mPaint;  // 畫筆,包含了畫幾何圖形、文本等的樣式和顏色信息

private OnZidingyiListener onZidingyiListener;

public zidingyi(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public zidingyi(Context context,AttributeSet attrs) {

super(context,attrs);

mPaint=new Paint();

// TypedArray是一個用來存放由context.obtainStyledAttributes獲得的屬性的數組

// 在使用完成後,一定要調用recycle方法

// 屬性的名稱是styleable中的名稱+“_”+屬性名稱

TypedArray mTypedArray=context.obtainStyledAttributes(attrs, R.styleable.MyView);

 // 提供默認值,放置未指定

int textcolor=mTypedArray.getColor(R.styleable.MyView_textColor, 0XFF00FF00);

float textsize=mTypedArray.getDimension(R.styleable.MyView_textSize, 100);

mPaint.setColor(textcolor);

mPaint.setTextSize(textsize);

mTypedArray.recycle();// 一定要調用,否則這次的設定會對下次的使用造成影響

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// Canvas中含有很多畫圖的接口,利用這些接口,我們可以畫出我們想要的圖形

// mPaint = new Paint();

// mPaint.setColor(Color.RED);

mPaint.setStyle(Style.FILL);// 設置填充

canvas.drawRect(100, 100, 200, 200, mPaint);// 繪製矩形   還有很多形狀 可以看方法

mPaint.setColor(Color.BLUE);

canvas.drawText("oy my good", 100, 100, mPaint);

// 下面兩個註釋的方法 也是經常用到的  

// invalidate();  如果你有其他需要畫得 調用這個方法 會激發 onDRAW

// postInvalidate(); 在其他非主線程中使用 可以防止 ANR

}

//下面兩個方法是自定義點擊事件

@Override  //複寫這個方法來實現判斷是哪個按鈕做出的事件

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (KeyEvent.KEYCODE_ENTER == keyCode)

{

onZidingyiListener.onZDYClick(zidingyi.this, keyCode);

}

return super.onKeyDown(keyCode, event);

}

public void setOnZidingyiListener(OnZidingyiListener onZidingyiListener) {

this.onZidingyiListener = onZidingyiListener;

}

}

下面我們開始 自定義點擊事件 

先看實現自定義組件顯示和點擊事件的 Main方法類代碼

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

public class mainactivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

zidingyi zidingyi = (com.tarena.zidingyiView.zidingyi) findViewById(R.id.zidingyi);

//調用自定義組件中 自定義的點擊事件 方法   (看上面帶桔色的方法)

//這個方法需要傳入一個我們定義的接口        (下面紅色的接口類)

//接口中的抽象方法 需要我們傳入一個自定義組件 和識別點擊事件的一個int

zidingyi.setOnZidingyiListener(new OnZidingyiListener() {

@Override

public void onZDYClick(com.tarena.zidingyiView.zidingyi zidingyi,

int keyCode) {

//寫 需要的代碼

}

});

}

}

 寫一個接口類 模仿View的點擊事件的接口 

import android.content.DialogInterface;

public interface OnZidingyiListener {

    public void onZDYClick(zidingyi zidingyi, int which);

}

出來的效果

大家看這個效果和我上面的代碼設置的 左上座標都是從100開始的 爲什麼我們文字沒有在畫的方框裏呢 這個問題我想了半天 百思不得其解 結果我仔細看了下 恍然大悟 哈哈 這裏先不給大家答案 如果大家也遇到這個問題 到時候記得問我把

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章