1.繼承并在繪制前后添加 法
一句話:
繼承常用的控件后,在繪制之前或之后進(jìn)行邏輯添加。
關(guān)鍵代碼
@Override
public void onDraw(Canvas canvas){
//繪制前進(jìn)行自己繪制。
super.onDraw(canvas);
//繪制后進(jìn)行自己繪制。
}
例子:
- 繼承一個TextView,然后先繪制一個矩形,然后顯示字。
public class MyFirstView extends TextView{
private Paint mpain;
public MyFirstView(Context context) {
super(context);
}
public MyFirstView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyFirstView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onDraw(Canvas canvas){
mpain=new Paint();
mpain.setColor(Color.LTGRAY);
mpain.setStyle(Paint.Style.FILL);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mpain);
super.onDraw(canvas);
}
}
覆蓋onDraw方法,在super.onDraw(canvas)前繪制一個灰色矩形。
2.添加額外屬性組合控件 法
一句話:
自己定義在xml的屬性,然后覆蓋構(gòu)造函數(shù)獲取屬性并賦值。
要滿足兩點:
- 創(chuàng)建attrs.xml。
- 覆蓋一個構(gòu)造函數(shù)獲取attrs.xml里的屬性并賦值。
創(chuàng)建attrs.xml,并設(shè)定每個屬性名和數(shù)據(jù)類型。
這里設(shè)定兩個textview的四個屬性。
<resources>
<declare-styleable name="MySecondView">
<attr name="leftText" format="string"/>
<attr name="leftTextSize" format="dimension"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
<attr name="rightTextSize" format="dimension"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
</declare-styleable>
</resources>
覆蓋構(gòu)造函數(shù)獲取上述的屬性,并且將上述屬性的值放入具體的屬性。
public MySecondView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.MySecondView);
leftText=ta.getString(R.styleable.MySecondView_leftText);
leftTextSize=ta.getDimension(R.styleable.MySecondView_leftTextSize,10);
leftTextColor=ta.getColor(R.styleable.MySecondView_leftTextColor,0);
leftBackground=ta.getDrawable(R.styleable.MySecondView_leftBackground);
rightText=ta.getString(R.styleable.MySecondView_rightText);
rightTextSize=ta.getDimension(R.styleable.MySecondView_rightTextSize,10);
rightTextColor=ta.getColor(R.styleable.MySecondView_rightTextColor,0);
rightBackground=ta.getDrawable(R.styleable.MySecondView_rightBackground);
ta.recycle();
leftTextView =(TextView)new TextView(context);
leftTextView.setText(leftText);
leftTextView.setTextSize(leftTextSize);
leftTextView.setTextColor(leftTextColor);
leftTextView.setBackground(leftBackground);
rightTextView=(TextView)new TextView(context);
rightTextView.setText(rightText);
rightTextView.setTextSize(rightTextSize);
rightTextView.setTextColor(rightTextColor);
rightTextView.setBackground(rightBackground);
LeftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
RightParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
this.addView(leftTextView,LeftParams);
this.addView(rightTextView,RightParams);
}
然后在具體的布局xml中使用:
<com.example.myview.MyView.MySecondView
android:layout_width="match_parent"
android:layout_height="match_parent"
two:leftText="這是左邊"
two:leftTextColor="#0288D1"
two:leftTextSize="20sp"
two:rightText="這是右邊"
two:rightTextColor="#FF5722"
two:rightTextSize="20sp"/>
顯示如下:
3.徹底自己繪制 法
一句話:
繼承View后覆蓋onDraw()各憑本事0.0
自己繪制一個階梯狀的小正方形~
關(guān)鍵代碼:
@Override
public void onDraw(Canvas canvas) {
mpaint.setColor(0xff03A9F4);
mpaint.setStyle(Paint.Style.FILL);
for (int i = 0; i < 5; i++) {
canvas.drawRect(i * 100, i * 100, i * 100 + 100, i * 100 + 100, mpaint);
}
}
其實這個功能是最強(qiáng)的,所有的view都可以靠自己的想象力繪制出來,所以說,各!憑!本!事!哈哈。