標籤:ack 檔案 not asc sdn nim south cli lang
近期在學習android中的graphics中繪圖系列。依照大神思路。找葫蘆畫瓢實現了一個開關。下:
記錄一下實現方式:
1.畫背景
形狀。分成兩個半圓與一個矩形,那麼代碼能夠寫成:
private void drawBackground(Canvas canvas) { mBackGroundPaint.setColor(mCurrentBackGroundColor); //畫右邊的半圓 RectF recF = new RectF(mToggleWidth / 2 - mRadius, -mRadius, mToggleWidth / 2 + mRadius, mRadius); canvas.drawArc(recF, -90, 180, true, mBackGroundPaint); //畫矩形 RectF tangle = new RectF(-mToggleWidth / 2, -mRadius, mToggleWidth / 2, mRadius); canvas.drawRect(tangle, mBackGroundPaint); //畫左邊的半圓 RectF rectF = new RectF(-mToggleWidth / 2 - mRadius, -mRadius, -mToggleWidth / 2 + mRadius, mRadius); canvas.drawArc(rectF, 270, -180, true, mBackGroundPaint); canvas.restore(); }
3.畫圓點:
這裡我是用一個圓環[紫色的]+內切圓環的實心圓[藍色]構成的。或許你會說,直接設定panint.setStrokeWidth不就能夠了。這個傻冒,呵呵。事實上我也想。可是發現不行啊【比較難看,並且我的顏色還不好設定,呵呵,你試一下就知道了】。
紫色的圓環負責顯示包住內部實心圓,形成一個邊界作用。這個對於白色的實心圓是非常好看的。
4.設定開關
public void setOpen(boolean isOpen) { if (mIsOpen != isOpen) { mIsOpen = isOpen; showAnimation(mIsOpen); } }
能夠看出。這裡我用了動畫的顯示效果,在android橫行的年代,不加點動畫就會顯示生硬。舉個梨子,我第一個遇到哥們做相似的開關時。直接叫視覺project師[說白了就是美工]切了兩張圖,不得不說,那哥們非常聰明非常牛逼,我自然也是學到了,後來發現發現好生硬啊,沒有一點美感。於是我就自己寫了。
A.實心圓的圓心座標:
圖中能夠看到非常清楚了。那麼我能夠直接寫代碼了:
//mToggleWidth正好是矩形的矩形的長度哦: final float start = isOpen ? -mToggleWidth / 2 : mToggleWidth / 2; float end = isOpen ?
mToggleWidth / 2 : -mToggleWidth / 2; ValueAnimator animator = ValueAnimator.ofFloat(start, end); animator.setDuration(mAnimationTime); animator.setInterpolator(new LinearInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { mCurrentXPosition = (float) valueAnimator.getAnimatedValue(); setCurrentColor(); invalidate(); } }); animator.start(); }
B. 背景的變色:
從關閉顏色到開啟顏色,這當中是有顏色過度的。不清楚,好你看看這個:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzc2MjU3Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="這裡寫圖片描寫敘述" title=""> 清楚了吧,恩這個顏色該怎麼玩呢????
非常多大神的文章裡,都是有個叫顏色估值器的介紹,即:
ValueAnimator.ofArgb(...)
本來用這個非常easy實現的,能夠我練習的sdk min版本號碼是14,也真是懶得改,好吧,懶得改我就的想想方法了,方法是這種:
我在s點的座標我是知道的,那麼我就能夠計算我動畫啟動並執行百分比了吧,恩,啥意思?:
float percent = (mCurrentXPosition + mToggleWidth / 2) * 1.0f / mToggleWidth;
這個看圖能夠直觀些。我也就不多講了,事實上我數學也非常不好,還特麼剛學習Paint,也搞了好久,多搞搞或許就好了。知道了動畫進行的百分比,那麼我們就知道了隨意時刻的動畫了:
float percent = (mCurrentXPosition + mToggleWidth / 2) * 1.0f / mToggleWidth; int currentR = (int) ((mEndBgColorR - mStartBgColorR) * percent + mStartBgColorR); int currentG = (int) ((mEndBgColorG - mStartBgColorG) * percent + mStartBgColorG); int currentB = (int) ((mEndBgColorB - mStartBgColorB) * percent + mStartBgColorB); mCurrentBackGroundColor = Color.rgb(currentR, currentG, currentB);
這裡的EndBgColor是指結束時的背景顏色。而StartBgColor就是指開始時的背景顏色。
使用:
xml檔案裡配置:
<micro.com.testpaint.view.ToggleButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" app:circle_ring_width="4dp"/>
activity中調用:
ToggleButton button = (ToggleButton) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { button.setOpen(!button.ToggleIsOpen()); } });
然後就出現了我們點擊的樣子了。
。。。
好了,大家能夠看看,也能夠在我的基礎上擴充了。剛學習這方面的東西。大神勿噴,要臉。
最後,代碼 。
實現一個簡單的android開關