Android volume adjustment control and android volume adjustment based on the View function customized by Hongyang blog
1. Create attrs. xml in values and write out the required attributes:
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="firstColor" format="color" /> <attr name="secondColor" format="color" /> <attr name="circleWidth" format="dimension" /> <attr name="dotCount" format="integer" /> <attr name="splitSize" format="integer" /> <attr name="bg" format="reference" /> <declare-styleable name="CustomVolumControlBar"> <attr name="firstColor" /> <attr name="secondColor" /> <attr name="circleWidth" /> <attr name="dotCount" /> <attr name="splitSize" /> <attr name="bg" /> </declare-styleable></resources>
2. Create a view class and implement the required business. For details, see the Code. The Code is very detailed:
Package com. zzw. custom. widget; import android. content. context; import android. content. res. typedArray; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. graphics. rectF; import android. media. audioManager; import android. OS. handler; import android. util. attributeSet; import android. util. log; import android. util. typedValue; import android. view. motionEvent; import android. view. view; import com. zzw. custom. r;/*** Created by zzw on 2016/6/1. * Description: */public class CustomVolumControlBar extends View {/*** first-lap color */private int mFirstColor;/*** second-lap color */private int mSecondColor; /* ** circle width */private int mCircleWidth;/* Paint Brush */private Paint mPaint;/* current progress */private int mCurrentCount = 3; /*** intermediate image */private Bitmap mImage;/*** gap between blocks */private int mSplitSize;/*** Number */private int mCount; /*** intermediate image boundary */private Rect mRect; private AudioManager mAudioManager; private Handler mHandler = new Handler (); public CustomVolumControlBar (Context context) {this (context, null);} public CustomVolumControlBar (Context context, AttributeSet attrs) {this (context, attrs, 0);} public CustomVolumControlBar (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init (context, attrs, defStyleAttr);} private void init (Context context, AttributeSet attrs, int defStyleAttr) {TypedArray a = context. getTheme (). obtainStyledAttributes (attrs, R. styleable. customVolumControlBar, defStyleAttr, 0); int n =. getIndexCount (); for (int I = 0; I <n; I ++) {int attr =. getIndex (I); switch (attr) {case R. styleable. customVolumControlBar_firstColor: this. mFirstColor =. getColor (attr, Color. BLACK); break; case R. styleable. customVolumControlBar_secondColor: this. mSecondColor =. getColor (attr, Color. WHITE); break; case R. styleable. customVolumControlBar_circleWidth: this. mCircleWidth =. getDimensionPixelSize (attr, (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_PX, 20, getResources (). getDisplayMetrics (); break; case R. styleable. customVolumControlBar_dotCount: this. mCount =. getInt (attr, 20); // The default value is 20 break; case R. styleable. customVolumControlBar_splitSize: this. mSplitSize =. getInt (attr, 20); break; case R. styleable. customVolumControlBar_bg: this. mImage = BitmapFactory. decodeResource (getResources (),. getResourceId (attr, 0); break ;}}. recycle (); mPaint = new Paint (); mRect = new Rect (); if (mAudioManager = null) mAudioManager = (AudioManager) context. getSystemService (Context. AUDIO_SERVICE);/*** get the maximum volume and current volume */mCount = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_MUSIC); mCurrentCount = mAudioManager. getStreamVolume (AudioManager. STREAM_MUSIC); Log. e ("=", "mCount:" + mCount + "mCurrentCount:" + mCurrentCount) ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); mPaint. setAntiAlias (true); // eliminate the Sawtooth mPaint. setStrokeWidth (mCircleWidth); // set the circle width mPaint. setStrokeCap (Paint. cap. ROUND); // defines the electric shape of a line segment. setStyle (Paint. style. FILL_AND_STROKE); // set the hollow int center = getWidth ()/2; // obtain the center int radius = center-mCircleWidth/2; // obtain the radius drawOval (canvas, center, radius);/*** calculates the position of the inner Cut Square */int relRadius = radius-mCircleWidth/2; // obtain the inner circle radius. *** the distance from the inner Cut Square to the top is mCircleWidth + relRadius-√ 2/2 */mRect. left = (int) (relRadius-Math. sqrt (2) * 1.0f/2 * relRadius) + mCircleWidth;/*** distance from the left side of the intangent square = mCircleWidth + relRadius-√ 2/2 */mRect. top = (int) (relRadius-Math. sqrt (2) * 1.0f/2 * relRadius) + mCircleWidth; mRect. bottom = (int) (mRect. left + Math. sqrt (2) * relRadius); mRect. right = (int) (mRect. left + Math. sqrt (2) * relRadius);/*** if the image is small, place it in the center of the Image Based on the image size */if (mImage. getWidth () <Math. sqrt (2) * relRadius) {mRect. left = (int) (mRect. left + Math. sqrt (2) * relRadius * 1.0f/2-mImage. getWidth () * 1.0f/2); mRect. top = (int) (mRect. top + Math. sqrt (2) * relRadius * 1.0f/2-mImage. getHeight () * 1.0f/2); mRect. right = (int) (mRect. left + mImage. getWidth (); mRect. bottom = (int) (mRect. top + mImage. getHeight ();} // draw canvas. drawBitmap (mImage, null, mRect, mPaint);}/*** draw block to */private void drawOval (Canvas canvas, int center, int radius) {/*** calculate the proportion of each block based on the number and gap required * 360 */float itemSize = (360 * 1.0f-mCount * mSplitSize)/mCount; /*** defines the arc shape and size boundaries */RectF oval = new RectF (center-radius, center-radius, center + radius, center + radius ); mPaint. setColor (mFirstColor); // set the color of the ring for (int I = 0; I <mCount; I ++) {canvas. drawArc (oval, I * (itemSize + mSplitSize), itemSize, false, mPaint); // draw an Arc Based on the Progress} mPaint. setColor (mSecondColor); // set the color of the ring for (int I = 0; I <mCurrentCount; I ++) {canvas. drawArc (oval, I * (itemSize + mSplitSize), itemSize, false, mPaint ); // draw an Arc Based on the Progress}/*** current number + 1 */public synchronized void up () {mHandler. postDelayed (new Runnable () {@ Override public void run () {mCurrentCount ++; if (mCurrentCount> mCount) mCurrentCount = mCount; postInvalidate (); setVolume (mCurrentCount) ;}}, 100) ;}/ *** set the volume ** @ param index */private void setVolume (int index) {mAudioManager. setStreamVolume (AudioManager. STREAM_MUSIC, index, 1);}/*** current quantity-1 */public synchronized void down () {mHandler. postDelayed (new Runnable () {@ Override public void run () {mCurrentCount --; if (mCurrentCount <0) mCurrentCount = 0; postInvalidate (); setVolume (mCurrentCount );}}, 100);} private int lastY, nowY; @ Override public boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: lastY = (int) event. getY (); break; case MotionEvent. ACTION_MOVE: nowY = (int) event. getY (); int updateYCount = (nowY-lastY)/30; if (updateYCount> 0) {for (int I = 0; I <updateYCount; I ++) {down () ;}} else if (updateYCount <0) {for (int I = updateYCount; I <0; I ++) {up ();}} lastY = nowY; break;} return true ;}}
3. Reference in xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zzw.Custom.MainActivity"> <com.zzw.Custom.widget.CustomVolumControlBar android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" app:bg="@mipmap/ic_horn" app:circleWidth="5dp" app:firstColor="@color/colorPrimary" app:secondColor="@color/colorAccent" app:splitSize="10" /></RelativeLayout>
Of course, in actual operations, OnTouch events are generally stored in activity or Fragment. You only need to copy them out of the Code.
Finally, I am very grateful to the great god, let us learn a lot, this article and the great god of the relevant blog address: http://blog.csdn.net/lmj623565791/article/details/24529807
CustomVolumControlBar