標(biāo)簽(空格分隔): Android
子View的onMeasure不一定要重寫辛萍,只是如果不重寫就會(huì)造成wrap_coontent時(shí)會(huì)充滿父布局限寞。而且onLayout也不一定要重寫雕崩,因?yàn)橄到y(tǒng)調(diào)用繼承自View的onLayout;所以經(jīng)常要重寫的是onDraw迈勋,在里面的進(jìn)行自己的繪制
自定義View時(shí)一般都會(huì)繼承View的,所以一般會(huì)在onMeasure醋粟、onLayout靡菇、onDraw調(diào)用父類View相應(yīng)的的構(gòu)造方法,以方便系統(tǒng)幫我們實(shí)現(xiàn)一些必要的功能米愿,免得自己麻煩去自己實(shí)現(xiàn)
注意自定義屬性的定義格式厦凤、獲取格式
留意下面的代碼
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.DottedProgressBar,
0, 0);
TypedValue value = new TypedValue();
//獲取對(duì)應(yīng)的屬性值存放在value中
a.getValue(R.styleable.DottedProgressBar_activeDot, value);
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// It's a color
isActiveDrawable = false;//不用Drawable,用Color
//從value中獲取屬性值
mActiveDotColor = getResources().getColor(value.resourceId);
//后面的那個(gè)數(shù)字是獲取不到時(shí)的默認(rèn)值
mDotSize = a.getDimensionPixelSize(R.styleable.DottedProgressBar_dotSize, 5);
- 子View的構(gòu)造函數(shù)一般要這樣寫育苟,不然會(huì)報(bào)錯(cuò)较鼓,或許像下面ViewGroup那樣寫也行。其實(shí)只寫一個(gè)帶有兩個(gè)參數(shù)的構(gòu)造函數(shù)也行宙搬,但是一般是把三個(gè)都寫
public CircleView(Context context) {
this(context, null);//調(diào)用三個(gè)參數(shù)的構(gòu)造函數(shù)
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);//調(diào)用三個(gè)參數(shù)的構(gòu)造函數(shù)
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);//先調(diào)用父類的構(gòu)造函數(shù)
//以下是初始化工作笨腥,包括選擇好自定義的屬性、設(shè)置好畫筆等等
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
try {
mColor = a.getColor(R.styleable.CircleView_cv_color, DEFAULT_COLOR);
} finally {
a.recycle();
}
init();//設(shè)置畫筆
}
-ViewGroup一般要這樣寫勇垛,不然會(huì)報(bào)錯(cuò)脖母,或許像上面子View那樣寫也行。
//init是初始化工作的函數(shù)
//要在每個(gè)Viewgroup的構(gòu)造函數(shù)中都調(diào)用相應(yīng)的父類的構(gòu)造函數(shù)闲孤,并且都調(diào)用init
public RubberIndicator(Context context) {
super(context);
init(null, 0);
}
public RubberIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public RubberIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
//以下是用注解來針對(duì)一些特殊的版本
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RubberIndicator(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs, defStyleAttr);
}
- 關(guān)于Canvas的操作:
Canvas對(duì)象的獲取方式有兩種:一種我們通過重寫View.onDraw方法谆级,View中的Canvas對(duì)象會(huì)被當(dāng)做參數(shù)傳遞過來,我們操作這個(gè)Canvas讼积,效果會(huì)直接反應(yīng)在View中肥照。另一種就是當(dāng)你想創(chuàng)建一個(gè)Canvas對(duì)象時(shí)使用的方法:
請(qǐng)參考《群英傳》p38
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
canvas.drawColor(Color.GRAY);
//原先這個(gè)Bitmap b是沒有顏色的,但經(jīng)過Canvas c的上色后勤众,已經(jīng)變成灰色舆绎,如果之后把這個(gè)Bitmap b在View的onDraw傳給View的自帶Canvas,就可以在View中畫出變成灰色后的Bitmap
canvas.drawBitmap(bitmap, 0, 0, null); //繪制圖像 们颜,因?yàn)榇笮?吕朵,所以不改變bitmap的大小
- 關(guān)于Bitmap的操作
見[博客地址][1]
- 關(guān)于requestLayout 和 invalidate的區(qū)別
RequestLayout:當(dāng)view確定自身已經(jīng)不再適合現(xiàn)有的區(qū)域時(shí)猎醇,該view本身調(diào)用這個(gè)方法要求parent view重新調(diào)用他的onMeasure onLayout來對(duì)重新設(shè)置自己位置。特別的當(dāng)view的layoutparameter發(fā)生改變努溃,并且它的值還沒能應(yīng)用到view上硫嘶,這時(shí)候適合調(diào)用這個(gè)方法。也就是當(dāng)通過getLayoutParrms().width = XXX的時(shí)候梧税,我們需要重新調(diào)用RequestLayout
invalidate:View類調(diào)用迫使view重畫沦疾。
![此處輸入圖片的描述][2]
在很多情況下,requestLayout是不需要被調(diào)用的第队。例如哮塞,我們把一個(gè)AbsoluteLayout里面的childView挪動(dòng)一下位置。我們僅僅需要調(diào)用的可能就是重新布局當(dāng)前AbsoluteLayout斥铺,然后調(diào)用invalidate方法進(jìn)行重繪彻桃。而不是從當(dāng)前View向上的整個(gè)View樹形結(jié)構(gòu)都要重新layout,onLayout晾蜘,measure邻眷,onMeasure一次。
這個(gè)時(shí)候剔交,怎么辦肆饶?
一種方法是,直接調(diào)用onLayout岖常。然后調(diào)用invalidate進(jìn)行重繪驯镊。很明顯可以提升繪制效率。由于父View的layout實(shí)現(xiàn)中對(duì)會(huì)通知布局的listener竭鞍。但是由于無法得到listener板惑,因此調(diào)用onlayout的時(shí)候無法對(duì)其進(jìn)行通知,這也是這種實(shí)現(xiàn)的缺陷偎快。
- 關(guān)于ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this, "progress",1, 500);見[博客][3]冯乘、[博客][4]
原理大致是
用ObjectAnimator.ofFloat(mView, "progress", 0, 1).setDuration(2000).start();來改變自定義控件progress的值,然后不斷刷新繪制圖(調(diào)用canvas方法)晒夹,要使用ObjectAnimator必須要在自定義方法加上對(duì)progress的setProgress()方法裆馒,原因是ObjectAnimator用java反射來改變progress的值。
- android中獲取屏幕相關(guān)信息
// 屏幕寬度(px)
int widthPx = this.getResources()
.getDisplayMetrics().widthPixels;
// 屏幕高度(px)
int heightPx = this.getResources()
.getDisplayMetrics().heightPixels;
// 屏幕密度(dpi):指每英寸中的像素?cái)?shù)
float densityDpi = this.getResources()
.getDisplayMetrics().densityDpi;
//屏幕密度:指每平方英寸中的像素?cái)?shù),在DisplayMetrics類中丐怯,該密度值為dpi/160 喷好,所以density = densityDpi /160
//density是當(dāng)前設(shè)備和默認(rèn)dpi = 160的比值,比如读跷,對(duì)于dpi = 320的設(shè)備梗搅,densityDpi = 320,density = 2。
float density = this.getResources()
.getDisplayMetrics().density;
// 屏幕寬度(dip)
int widthDip = pxToDip(this, widthPx);
// 屏幕高度(dip)
int heightDip = pxToDip(this, heightPx);
/**
* px值向dip值轉(zhuǎn)換
*
* @param context
* @param pxValue
* @return
*/
private int pxToDip(Context context, float pxValue) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);//多了一個(gè)0.5,這個(gè)是為了四舍五入而已
}
/**
* dip值向px值轉(zhuǎn)換
*
* @param context
* @param dipValue
* @return
*/
public int dipToPx(Context context, float dipValue) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);//多了一個(gè)0.5无切,這個(gè)是為了四舍五入而已
}
- 為什么一般都是將距離長(zhǎng)度的單位從DP轉(zhuǎn)化為PX
舉個(gè)簡(jiǎn)單的例子蟀俊,如果掃描矩形框的長(zhǎng)度為300px,在density為320和480的手機(jī)屏幕上顯示效果是完全不一樣的订雾,因此單位要使用dip。但是在使用canvas繪制東西時(shí)矛洞,所依照的坐標(biāo)系洼哎、Rect等單位都是px,所以其尺寸要以dip為單位沼本,而坐標(biāo)要以px為單位噩峦。
---
- 如果自定義View時(shí),在這個(gè)view里面new出來的handler是View的抽兆,不是從Activity主線程傳進(jìn)來的识补,這樣也可以更新界面嗎?
答案是:可以辫红。因?yàn)閂iew默認(rèn)帶一個(gè)Handler凭涂,屬于mainLooper的,所以這個(gè)new出來的handler是可以更新界面的
---
[1]: http://www.cnblogs.com/feisky/archive/2010/01/10/1643460.html
[2]: http://o6uwc0k25.bkt.clouddn.com/1.jpg
[3]: http://blog.sina.com.cn/s/blog_812973c30102w933.html
[4]: http://www.it610.com/article/2112665.htm