代碼示例見 神秘的gitHub鏈接 (/view/widget包下均為自定義控制元件
View繪制程序
繼承自View類的控制元件在繪制中分為三個主要程序
Measure
在這一階段確定控制元件的大小,應對不同specModel采取不用策略
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
一般直接呼叫super.onMeasure(widthMeasureSpec,heightMeasureSpec)就可以了,如果不能達到預期效果,就需要先了解一下class#MeasureSpec
在onMeasure函式傳入的兩個int并不單純是view的width和height,只有30位用于記錄數值,高二位記錄specModel 大概,反正不全是用來計數的 ,specModel有如下三種,以height屬性為例:
- EXACTLY
在layout_height為 具體數值 or match_parent時生效 - AT_MOST
在layout_height為wrap_content時生效,表示view尺寸為不超過父視圖最大尺寸的任意大小 - UNSPECIFIED
不指定大小,父視圖不限制子視圖的大小
在確定view的height和width之后,呼叫setMeasuredDimension(width,height);為view設定大小,否則不會生效,
onLayout
確定view左上位置 具體作用不明,用到再更
onDraw
這一程序負責繪制你的CustomView,也是實作影片效果的關鍵部分,根據計時器數指計算引數,呼叫invalidate()不斷重繪控制元件實作影片,
示例:仿華為天氣:日出日落影片效果
三者呼叫關系如下圖

自定義屬性
宣告屬性
如果需要額外的資訊來配置自定義控制元件,可以在res/value資源檔案下新建Resource File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="自定義控制元件類名">
<attr name="自定義屬性名稱" format="屬性格式">
</declare-styleable>
</resources>
建構式
現在僅定義屬性我們還無法在控制元件加載時獲取,獲取還需要寫建構式,
關于建構式有如下三種多載函式
-
public CustomView(Context context)
在代碼中動態為頁面動態添加時呼叫構造器,需要配合setter配置屬性 -
public CustomView(Context context, @Nullable AttributeSet attrs)
從xml檔案中構造控制元件時呼叫的構造器,獲取屬性方法如下TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.yourCustomViewClassName); if (typedArray!=null){ int color = typedArray.getInt(R.styleable.declare_attr , defValue); // 注意String型別沒有默認值,如果xml檔案中沒有配置獲取為null String text = typedArray.getString(R.styleable.declare_attr); // 獲取dimension型別時,默認值單位為px float width = typedArray.getDimension(R.styleable.declare_attr, defValue); typedArray.recycle(); } -
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
從xml中構造控制元件且使用theme attr style時呼叫,大概用不到,用到再更
影片效果
這里僅對Android Anime效果做簡單概述
幀影片
實作較為簡單但是由于不會畫圖所以從來沒有試過
- 在res/drawable資源檔案下新建xml,root為 animation-list
- 將幀影片的每一幀按序添加,設定持續時間duration
- 更改需要影片效果的activity/fragment的background
- AnimationDrawable drawable =activity/fragment id.getBackground();drawable.start()開始影片;drawable.stop()停止影片
補間影片
補間影片有四種影片效果:alpha 透明度 / rotate 旋轉 / scale 縮放 / translate 平移
配置補間影片需要在res/anim資源檔案夾下新建Animation Resource File,配置屬性見開發檔案,下面以alpha示例
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.8"
android:toAlpha="1"
android:duration="3000"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
</alpha>
頁面布局
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="bg pic"/>
bg = findViewById(R.id.background);
Animation animation = AnimationUtils.loadAnimation(SplashActivity.this,R.anim.yourResoucreFileName);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//影片開始時回呼函式
}
@Override
public void onAnimationEnd(Animation animation) {
//影片結束后回呼函式
}
@Override
public void onAnimationRepeat(Animation animation) {
//影片重復時回呼函式
}
});
bg.startAnimation(animation);
屬性影片
示例:仿華為天氣:日出日落影片效果
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/276650.html
標籤:其他
