目的
掌握Android開發(fā)中最基礎(chǔ)的自定義屬性方法,自定義:PageController屬性经备,里面含有:mPadding屬性兼雄,resourceID屬性,numberOfPagese屬性
相關(guān)技術(shù)占业、及其使用
1绒怨、自定義屬性:
(1)、創(chuàng)建一個(gè)values資源文件
(2)谦疾、使用declare-styleable關(guān)鍵字修飾
(3)南蹂、name值為自己定義的類的類型
(4)、添加屬性 attr name 和對(duì)應(yīng)值的類型 format(如果有多個(gè)類型用 | 隔開) (自定義屬性在xml中使用)
自定義屬性:
<resources>
<!--聲明在那個(gè)控件上添加屬性-->
<declare-styleable name="PageController">
<attr name="mPadding" format="integer"/>
<attr name="resourceID" format="reference|color"/>
<attr name="numberOfPages" format="integer"/>
</declare-styleable>
</resources>
調(diào)用屬性:
<swu.ht.java.customattr.PageController
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:padding="20"
app:numberOfPages="5"
app:resourceID="@drawable/dot_shape"/>
2念恍、創(chuàng)建PageController類繼承于LinearLayout六剥,實(shí)現(xiàn)三個(gè)PageController方法
(1)創(chuàng)建私有變量:numberOfPage,resourceID,padding ,currentPage
public PageController(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(LinearLayout.HORIZONTAL);
setGravity(Gravity.CENTER);
/*
將xml里面自定義屬性的值取出來
*/
if(attrs != null){
/*從一個(gè)資源文件里面去將自定義的所有屬性取出來
1峰伙、AttributeSet xml里面配制的所有屬性
2仗考、自定義的屬性文件 R.styleable.name
*/
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.PageController);
/* 需要傳參數(shù)
1、自定義屬性名 自定義屬性name_屬性名
2词爬、默認(rèn)值(xml 文件里面沒有設(shè)置就默認(rèn))
*/
padding = typedArray.getInt(R.styleable.PageController_mPadding,0);
resourceID = typedArray.getResourceId(R.styleable.PageController_resourceID,0);
int page = typedArray.getInt(R.styleable.PageController_numberOfPages,0);
//顯示
setNumberOfPages(page);
}
}
(2)調(diào)用setNumberOfPage方法
public void setNumberOfPages(int numberOfPages) {
this.numberOfPages = numberOfPages;
//創(chuàng)建點(diǎn)
for(int i = 0; i < numberOfPages;i++){
//創(chuàng)建控件ImageView
ImageView dotView = new ImageView(getContext());
//設(shè)置顯示的內(nèi)容
dotView.setBackgroundResource(resourceID);
//設(shè)置約束
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
//除第一個(gè)點(diǎn)外秃嗜,依次給后面的點(diǎn)之間添加間距
if(i > 0 ){
params.leftMargin = padding;
}else {
//默認(rèn)選擇第一個(gè)點(diǎn)
dotView.setEnabled(false);
}
//添加到容器中
addView(dotView,params);
}
}
(3)調(diào)用onTouchEvent方法實(shí)現(xiàn)觸摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//取出當(dāng)前頁數(shù)
int current = currentPage;
//實(shí)現(xiàn)左右點(diǎn)擊切換
if(event.getX() > getWidth()*0.5){
//右邊
if(current == numberOfPages-1){
current = 0;
}else {
current++;
}
}else {
//左邊
if(current == 0){
current = numberOfPages-1;
}else {
current--;
}
}
setCurrentPage(current);
}
return true;
}
(4)調(diào)用SetCurrentPage還原上一次的默認(rèn)狀態(tài)
public void setCurrentPage(int currentPage) {
//將上一次的還原為默認(rèn)狀態(tài)
ImageView old = (ImageView)getChildAt(this.currentPage);
old.setEnabled(true);
//將當(dāng)前選中的設(shè)置為選中狀態(tài)
ImageView current = (ImageView)getChildAt(this.currentPage);
current.setEnabled(false);
this.currentPage = currentPage;
//將頁數(shù)改變的事件回調(diào)給監(jiān)聽者
if(mpageChangeListener != null){
mpageChangeListener.pageDidChanged(currentPage);
}
showAnimation(current);
}
(5)定義接口監(jiān)聽指示器改變事件,并實(shí)現(xiàn)拉伸動(dòng)畫
public interface PageChangeListener{
void pageDidChanged(int currentPage);
}
//mPageChangeListener
//設(shè)置監(jiān)聽對(duì)象
public void addPageChangeListener(PageChangeListener listener){
this.mpageChangeListener = listener;
}
/*
寬度拉伸的動(dòng)畫
關(guān)鍵幀動(dòng)畫 補(bǔ)間動(dòng)畫 屬性動(dòng)畫(ObjectAnimator)
*/
public void showAnimation(ImageView item){
ObjectAnimator scale = ObjectAnimator.ofFloat(item,"scaleX",1,1.5f,1);
scale.setDuration(400);
scale.start();
}
PS
自定義屬性主要就是在value里面定義屬性顿膨,然后在xml配置文件中進(jìn)行調(diào)用锅锨。