android - 自定義View中代碼流程問題
問題描述
具體代碼在最下方給出
點(diǎn)擊 CHECK 后 就是圖二的效果 但是不太明白他這個(gè)繪制過程 所以用Log.e調(diào)試 還是沒看懂點(diǎn)擊包含這個(gè)View的activity時(shí) log打印為
不明白為什么是兩次onDraw的打印
然后點(diǎn)擊CHECK Log.e打印為(只發(fā)了一部分截圖)
不明白為什么 又打印了兩次onDraw然后進(jìn)行sendEmptyMessageDelayed的遞歸操作(不知道有沒有說錯(cuò))在invalidate前后分別設(shè)置log然后每次在handleMessage中判斷的時(shí)候 為什么 又要打印onDraw(還是兩次)
E/AAA: onDrawE/AAA: onDrawE/AAA: invalidate 1 + 0E/AAA: invalidate 1 + 1E/AAA: Count=1
public class CheckView extends View { private static final int ANIM_NULL = 0; //動(dòng)畫狀態(tài)-沒有 private static final int ANIM_CHECK = 1;//動(dòng)畫狀態(tài)-開啟 private static final int ANIM_UNCHECK = 2; //動(dòng)畫狀態(tài)-結(jié)束 private Context mContext; // 上下文 private int mWidth, mHeight;// 寬高 private Handler mHandler; // handler private Paint mPaint; private Bitmap okBitmap; private int animCurrentPage = -1; // 當(dāng)前頁(yè)碼 private int animMaxPage = 13; // 總頁(yè)數(shù) private int animDuration = 500; // 動(dòng)畫時(shí)長(zhǎng) private int animState = ANIM_NULL; // 動(dòng)畫狀態(tài) private boolean isCheck = false;// 是否只選中狀態(tài) public CheckView(Context context, AttributeSet attrs) {super(context, attrs);Log.e('AAA','構(gòu)造方法CheckView + 0');init(context);Log.e('AAA','構(gòu)造方法CheckView + 1'); } /** * 初始化 * @param context */ private void init(Context context) {mContext = context;mPaint = new Paint();mPaint.setColor(0xffFF5317);//設(shè)置畫筆顏色mPaint.setStyle(Paint.Style.FILL);//設(shè)置畫筆風(fēng)格mPaint.setAntiAlias(true);//抗鋸齒okBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.checkes);//從資源文件獲取BitmapmHandler = new Handler() { @Override public void handleMessage(Message msg) {super.handleMessage(msg);if (animCurrentPage < animMaxPage && animCurrentPage >= 0) {// 點(diǎn)擊之前-1 <13 && -1>=0 點(diǎn)擊后 0 < 13 && 0 >=0 Log.e('AAA','invalidate 1 + 0'); invalidate();//重繪 ==> 調(diào)用onDraw方法,重繪View中變化的部分 Log.e('AAA','invalidate 1 + 1'); if (animState == ANIM_NULL)// 動(dòng)畫狀態(tài)==沒有return; if (animState == ANIM_CHECK) {//動(dòng)畫狀態(tài)==開啟animCurrentPage++;//當(dāng)前頁(yè)數(shù)++ } else if (animState == ANIM_UNCHECK) {//動(dòng)畫狀態(tài)==關(guān)閉animCurrentPage--;//當(dāng)前頁(yè)數(shù)-- } this.sendEmptyMessageDelayed(0, animDuration / animMaxPage); Log.e('AAA', 'Count=' + animCurrentPage);} else { if (isCheck) { //isCheck==falseanimCurrentPage = animMaxPage - 1;// -1 = 13-1 } else {animCurrentPage = -1; } Log.e('AAA','invalidate 2 + 0'); invalidate();//重繪 ondraw中 使用invalidate 非UI線程中 使用 postInvalidate Log.e('AAA','invalidate 2 + 1'); animState = ANIM_NULL;// 動(dòng)畫狀態(tài)==沒有} }}; } /** * View大小確定 * @param w * @param h * @param oldw * @param oldh */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);mWidth = w;mHeight = h;Log.e('AAA','onSizeChanged'); } /** * 繪制內(nèi)容 * @param canvas */ @Override protected void onDraw(Canvas canvas) {super.onDraw(canvas);Log.e('AAA','onDraw');// 移動(dòng)坐標(biāo)系到畫布中央canvas.translate(mWidth / 2, mHeight / 2);// 繪制背景圓形canvas.drawCircle(0, 0, 240, mPaint);// 得出圖像邊長(zhǎng)int sideLength = okBitmap.getHeight();// 得到圖像選區(qū) 和 實(shí)際繪制位置Rect src = new Rect(sideLength * animCurrentPage, 0, sideLength * (animCurrentPage + 1), sideLength);Rect dst = new Rect(-200, -200, 200, 200);// 繪制canvas.drawBitmap(okBitmap, src, dst, null); } /** * 選擇 */ public void check() {if (animState != ANIM_NULL || isCheck) return;animState = ANIM_CHECK;animCurrentPage = 0;Log.e('AAA','check + 1');mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage); // (0,500/13)isCheck = true;Log.e('AAA','check + 2'); } /** * 取消選擇 */ public void unCheck() {if (animState != ANIM_NULL || (!isCheck)) return;animState = ANIM_UNCHECK;animCurrentPage = animMaxPage - 1;mHandler.sendEmptyMessageDelayed(0, animDuration / animMaxPage);isCheck = false; } /** * 設(shè)置動(dòng)畫時(shí)長(zhǎng) * @param animDuration */ public void setAnimDuration(int animDuration) {if (animDuration <= 0) return;this.animDuration = animDuration; } /** * 設(shè)置背景圓形顏色 * @param color */ public void setBackgroundColor(int color){mPaint.setColor(color); }}
問題解答
回答1:這是系統(tǒng)的機(jī)制, 繪制幾次你不用去關(guān)心.你只應(yīng)該關(guān)心, 什么狀態(tài), 應(yīng)該繪制什么樣的圖.用試圖使用繪制次數(shù)來作精確的參考. (當(dāng)然, 某些動(dòng)畫可以使用這個(gè)onDraw的同時(shí)改變狀態(tài))
回答2:首先,我對(duì)我的答案并不是特別肯定,甚至希望他是錯(cuò)的
產(chǎn)生這種問題的罪魁禍?zhǔn)资窍旅娴膬蓚€(gè)BUTTON...Button繼承于TextView,然后TextView有這樣一個(gè)特性,當(dāng)為他設(shè)置Text或者他的Text變化的時(shí)候,他會(huì)調(diào)用父ViewGroup的onLayout方法,這樣父ViewGroup會(huì)再一次layout,再一次drawChild,從而再次執(zhí)行onDraw,將button換成ImageView之后,就不會(huì)出現(xiàn)題問中的情況了。
至于為什么之前onDraw執(zhí)行了兩次而不是三次就不知道了
相關(guān)文章:
1. mysql - 把一個(gè)表中的數(shù)據(jù)count更新到另一個(gè)表里?2. mysql 查詢身份證號(hào)字段值有效的數(shù)據(jù)3. node.js - 為什么微信的消息MsgId出現(xiàn)重復(fù)了,無法排重了。。4. mysql的主從復(fù)制、讀寫分離,關(guān)于從的問題5. MySQL 截短某一列的字符串6. 請(qǐng)教使用PDO連接MSSQL數(shù)據(jù)庫(kù)插入是亂碼問題?7. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來哪些效率或者其他方面的好處8. mysql - 字符串根據(jù)字典替換9. 視頻文件不能播放,怎么辦?10. node.js - nodejs開發(fā)中常用的連接mysql的庫(kù)
