Android开发进阶——自定义View的使用及其原理探索
- 2019 年 10 月 3 日
- 笔记
???Android????????????UI???????????????????????????????????????????????????????????????????????????????????????????????
?????????Android??????Android??????????????ViewGroup?View??ViewGroup???????View,??????????????????????????????????????????????????Activity????findViewById()????????????????????????ID???????????????ViewParent?????????????????????????Activity???????setContentView()?????????Activity???????Window?????Android????PhoneWindow,????DecorView????????View,??????????window??DecorView???????????TitleView,???ContentView?ContentView???ID?content?Framelayout,???????????????TitleView??????topbar???????activity???????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????
????????Textview???????????????????TextView,????????onDraw()?onMeasure()?onTouchEvent()???????onDraw()?????????onMeasure()???????onTouchEvent()?????????????????????????TextView?????????????onDraw()??????????
Paint paint1=new Paint(); //???? paint1.setColor(Color.YELLOW); paint1.setStyle(Paint.Style.FILL);
??????????????????????????Textview?????????????????onDraw????????????????????????????
@Override protected void onDraw(Canvas canvas) { canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint1);//???? canvas.save()? super.onDraw(canvas); canvas.restore(); }
??
????canvas????????????canvas????????????????
?????????????????????????????????????view???????????????????????CustomTextview???TextView,?????
<com.example.myapplication.View.CustomTextView android:layout_width="wrap_content" android:layout_height="match_parent"></com.example.myapplication.View.Buttonbtn>
???????????????
?????????????????????????????????????????????????????Imageview?Textview????????
<ImageView android:id="@+id/iv" android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:text="??" android:textSize="13sp" />
?????????????LinearLayout?????????????????????
public void init(Context context) { //?????????????? setOrientation(VERTICAL); //??????????? LayoutParams mLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); setLayoutParams(mLayoutParams); setGravity(Gravity.CENTER); setPadding(4, 4, 4, 4); //??????? View mButtonbtnView = LayoutInflater.from(context).inflate(layout.botton_btn_view, this, true); mImageView = mButtonbtnView.findViewById(id.iv); mTextView = mButtonbtnView.findViewById(id.tv); }
?????????????????????????????????????????
<com.example.myapplication.View.Buttonbtn android:layout_width="wrap_content" android:layout_height="match_parent"></com.example.myapplication.View.Buttonbtn>
????View????????
???????????????????????????????????????????????????????View??????????????????????View????????????onDraw???onMeasure???onTouchEvent????????????????
??onDraw???????canvas????????????????????????
??onMeasure??
?????????onMeasure??????View?????????????????????View??????????onMeasure?????????????????????????
??EXACTLY:?????????view???????????
??AT_MOST:????????????“wrap_content”????????????????????
??UNSPECIFIED:??????????????
??????????????????????????????????????view?onMeasure??????????MeasureSpec???????????MeasureSpec??????????2???????????30????????
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); if (widthMode == MeasureSpec.EXACTLY) { } else if (widthMode == MeasureSpec.AT_MOST) { } else if (widthMode == MeasureSpec.UNSPECIFIED) { } }
???????????????????????????????????????????????????????????
???????ViewGroup??????????ViewGroup????“wrap_content”???????????View?????View????????????????????????RelativeLayout?LinearLayout????ViewGroup????????????????????????
??onTouchEvent??
??onTouchEvent????????????????Android??????????????View???????????????????????????onTouchEvent?????????????????MotionEvent????????????????????????????????????????getAction()??????????????????????????Android???????????Android????????????????????????x?????????y??????????????????????getX???getY()???????????????????????
public boolean onTouchEvent(MotionEvent event) { float x; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { x=event.getX(); } break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; }
?????????????????????????????????????????????????????????????????????????
????????
?????????????????????ViewGroup??????ViewGroup??View,??????????????????View?ViewGroup??????????ViewGroupA,????????ViewGroupB???ViewGroupB??????????View??????ViewGroupA???????????????????
????dispatchTouchEvent()
????onInterceptTouchEvent()
????onTouchEvent()
??????View?????????????
????dispatchTouchEvent()
????onTouchEvent()
???????????ViewGroup??View??onInterceptTouchEvent()????????????????????????Log??????View????????????????
??????????ViewGroupA??dispatchTouchEvent()?onInterceptTouchEvent()?
????????ViewGroupB??dispatchTouchEvent()?onInterceptTouchEvent()?
??????View?dispatchTouchEvent()???
??????????????????????????????
????View?onTouchEvent()?
????ViewGroupB?onTouchEvent()?
????ViewGroupA?onTouchEvent()?
??
????????????????????ViewGroup???????????????????????View????????????????onInterceptTouchEvent()??????????????????????true?????????????????onTouchEvent()??false?????????????????????????????????
??????????????Android?????????????????????????View????????????????????????