一個自定義View的實例

    本帖爲轉載帖,稍微作了修改。 java

    第一步驟:自定義屬性,在res/values中定義,文件名能夠自定義 android

<?xml version="1.0" encoding="utf-8"?>
<resources>  
    <declare-styleable name="MyView">  
        <attr name="textColor" format="color"/>  
        <attr name="textSize" format="dimension"/>  
    </declare-styleable>  
</resources>


    第二步:自定義View實現 canvas

package com.test.customview;

import android.view.View;
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;

/**
 * 這個是自定義的TextView. 至少須要重載構造方法和onDraw方法 對於自定義的View若是沒有本身獨特的屬性,能夠直接在xml文件中使用就能夠了
 * 若是含有本身獨特的屬性,那麼就須要在構造函數中獲取屬性文件attrs.xml中自定義屬性的名稱 並根據須要設定默認值,放在在xml文件中沒有定義。
 * 若是使用自定義屬性,那麼在應用xml文件中須要加上新的schemas,
 * 好比這裏是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
 * 其中xmlns後的「my」是自定義的屬性的前綴,res後的是咱們自定義View所在的包
 * 
 * @author Administrator
 * 
 */
public class MyView extends View {

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

	public MyView(Context context) {
		super(context);

	}

	// 這個構造方法是系統默認的從XML文件中加載控件是的回調方法
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
		// TypedArray是一個用來存放由context.obtainStyledAttributes得到的屬性的數組
		// 在使用完成後,必定要調用recycle方法
		// 屬性的名稱是styleable中的名稱+「_」+屬性名稱
		
		// 獲取自定義的屬性數組,R.styleable.MyView
		TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.MyView);
		int textColor = array
				.getColor(R.styleable.MyView_textColor, Color.BLACK); // 提供默認值,放置未指定
		float textSize = array.getDimension(R.styleable.MyView_textSize, 10);
		mPaint.setColor(textColor);
		mPaint.setTextSize(textSize);

		array.recycle(); // 必定要調用,不然此次的設定會對下次的使用形成影響
	}

	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// Canvas中含有不少畫圖的接口,利用這些接口,咱們能夠畫出咱們想要的圖形

		//設置畫布背景顏色 
		canvas.drawColor(Color.BLUE);
		canvas.drawText("我是被畫出來的", mPaint.getTextSize() + 10, mPaint.getTextSize() + 10, mPaint);
		// 設置畫筆的顏色
		mPaint.setColor(Color.RED);
		// 設置填充
		mPaint.setStyle(Style.FILL); 
		canvas.drawRect(10, 10, mPaint.getTextSize() + 10, mPaint.getTextSize() + 10, mPaint); // 繪製矩形
	}
}

    第三步:佈局文件: 數組

<?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.test.customview"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.test.customview.MyView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        my:textColor="@android:color/black"
        my:textSize="50dp" />

</LinearLayout>    


    這裏有兩個須要注意的地方,第一個是第三行,最後面一段要指定到自定義控件所在的包;第二個是在佈局控件時,要寫明全路徑com.test.customview.MyView app

    第四步:Activity ide

package com.test.customview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class CustomView extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_custom_view);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.custom_view, menu);
		return true;
	}

}
相關文章
相關標籤/搜索