什麼是自定義view?
android開發中所有的控件幾乎都是view,然而實際開發中系統的view遠遠不夠使用,還用到自定義的view。
自定義view分爲三種:
1:自繪控件,之前不存在的控件,有自定義view生成的新控件。
2:繼承控件,在繼承系統的控件後,增加新的功能。
3:組合控件,將系統原有的幾種控件整合到一塊。
如何開發自定義view?
無論哪種自定義view的開發無疑離不開以下的某種方法:
Ⅰ、在OnMeasure()方法中,測量自定義控件的大小,使自定義控件能夠自適應佈局各種各樣的需求。
2、在OnDraw()方法中,利用Canvas與Pain兩個重要的對象來繪製要顯示的內容。
3、在OnLayout()方法中來確定控件顯示位置,主要有子控件情況下
4、在OnTouchEvent()方法處理控件的觸摸事件。
除此之外自定義view還需要在xml文件中定義style樣式來影響效果。
其中以下三個方法也比較重要:
- requestLayout View重新調用一次layout過程。
- invalidate View重新調用一次draw過程
- forceLayout 標識View在下一次重繪,需要重新調用layout過程。
這一篇我講下自繪view,例如做一個跑馬燈效果的view
效果圖如下:
跑馬燈效果圖
首先自定義一個myView繼承view,不需要實現其他三種方法,只需要實現onDraw方法,在onDraw中利用Canvas與Pain,設置一個文字並設置文字的顏色和大小,並設置文字的座標,這些值通過TypedArray對象獲取,記得使用完這個對象後要recycle。邏輯方面就是開啓一個線程,不斷以疊加橫座標的值重繪view來實現跑馬燈效果。
public class MyView extends View {
MyThread thread;
int rx=0;
private boolean isXScroll;
private String textString="";
private int textSize=0;
private int textColor;
Paint paint = new Paint();
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.process);
isXScroll = ta.getBoolean(R.styleable.process_isXScroll,false);
textString = ta.getString(R.styleable.process_text);
textSize = ta.getInt(R.styleable.process_textSize,10);
textColor=ta.getColor(R.styleable.process_textColor,32332);
ta.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(thread==null){
thread = new MyThread();
thread.start();
}else{
paint.setTextSize(textSize);
paint.setColor(textColor);
canvas.drawText(textString,rx,100,paint);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
isCircle = false;
}
private boolean isCircle=true;
class MyThread extends Thread{
@Override
public void run() {
while (isCircle){
if(isXScroll){
rx+=3;
if(rx>getWidth()){
rx= (int) (0-paint.measureText(textString));
}
}
postInvalidate();//和invalidate區別就是一個可以在主線程中直接調用
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
在values下面新建一個styles.xml中定義屬性,不同的屬性對應不同的format,有很多屬性類型,不在這裏一一介紹,這個demo以文字的內容、顏色、大小可以定義成style變量,在佈局文件中利用命名空間引入,方便開放開發者使用這些變量改變跑馬燈效果。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<declare-styleable name="process">
<attr name="isXScroll" format = "boolean"></attr>
<attr name="text" format = "string"></attr>
<attr name="textSize" format = "integer"></attr>
<attr name="textColor" format = "color"></attr>
</declare-styleable>
</resources>
佈局文件就可以使用這個自定義view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zzti.fengyongge.myview.MainActivity"
>
<com.zzti.fengyongge.myview.view.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res/com.zzti.fengyongge.myview"
app:isXScroll= "true"
app:text="Android開發的那些事"
app:textSize="50"
app:textColor="#385B00"
>
</com.zzti.fengyongge.myview.view.MyView>
</RelativeLayout>