国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義控件實戰<四> 音量增減控件

2019-11-06 10:03:35
字體:
來源:轉載
供稿:網友

上一篇文章實現了一個動態的圓弧控件,這篇文章在此基礎上面繼續實現一個簡單的音量增減控件

一、首先看一下我們這次要實現的效果圖:

效果圖

二、在attrs.xml中添加自定義屬性:

<?xml version="1.0" encoding="utf-8"?><resources> <attr name="maxVoice" format="integer"/> <attr name="currentVoice" format="integer"/> <declare-styleable name="Voice三、對應的布局文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" android:orientation="horizontal" android:gravity="center_horizontal" android:padding="20dp" android:background="@m四、SimpleView_04自定義控件代碼:public class SimpleView_04 extends View { /** * 最大音量 */ private int maxVoice; /** * 當前音量 */ private int currentVoice; private Rect rectImage; public SimpleView_04(Context context) { this(context, null); } public SimpleView_04(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SimpleView_04(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); /** * 獲得我們所定義的自定義樣式屬性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.VoiceProgressBar, defStyleAttr, 0); int length = a.getIndexCount(); for (int i = 0; i < length; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.VoiceProgressBar_maxVoice: maxVoice = a.getInt(attr, 15); break; case R.styleable.VoiceProgressBar_currentVoice: currentVoice = a.getInt(attr, 5); break; } } a.recycle(); rectImage = new Rect(); } //增加音量 public void addVoice(){ if(currentVoice < maxVoice) { currentVoice++; postInvalidate(); } } //減少音量 public void subVoice(){ if(currentVoice > 0){ currentVoice--; postInvalidate(); } } //重寫onDraw方法,繪制音量圖案 @Override protected void onDraw(Canvas canvas) { Bitmap bitmapSilence = scaleBitmap(R.mipmap.silence); Bitmap bitmapVoice = scaleBitmap(R.mipmap.voice); Paint mPaint = new Paint(); mPaint.setAntiAlias(true); // 消除鋸齒 mPaint.setStrokeWidth(5); // 設置圓環的寬度 mPaint.setStrokeCap(Paint.Cap.ROUND); // 定義線段斷電形狀為圓頭 mPaint.setAntiAlias(true); // 消除鋸齒 mPaint.setStyle(Paint.Style.STROKE); // 設置空心 RectF oval = new RectF(); //RectF對象 oval.left = getPaddingLeft(); //左邊 oval.top = getPaddingTop(); //上邊 oval.right = getMeasuredWidth()-getPaddingRight(); //右邊 oval.bottom = getMeasuredHeight()-getPaddingBottom(); //下邊 mPaint.setColor(Color.WHITE); for (int i=0; i<maxVoice; i++){ canvas.drawArc(oval, 40-20.77f*i, -5f, false, mPaint); //繪制圓弧 } mPaint.setColor(Color.BLACK); for (int i=0; i<currentVoice; i++){ canvas.drawArc(oval, 40-20.77f*i, -5f, false, mPaint); //繪制圓弧 } int mWidth = getMeasuredWidth(); int mHeight = getMeasuredHeight(); if(currentVoice == 0){ rectImage.left = mWidth/2 - bitmapSilence.getWidth()/2; rectImage.right = mWidth/2 + bitmapSilence.getWidth()/2; rectImage.top = mHeight / 2 - bitmapSilence.getHeight() / 2; rectImage.bottom = mHeight/2 + bitmapSilence.getHeight()/2; canvas.drawBitmap(bitmapSilence,null,rectImage,mPaint); }else{ rectImage.left = mWidth/2 - bitmapVoice.getWidth()/2; rectImage.right = mWidth/2 + bitmapVoice.getWidth()/2; rectImage.top = mHeight/2 - bitmapVoice.getHeight()/2; rectImage.bottom = mHeight/2 + bitmapVoice.getHeight()/2; canvas.drawBitmap(bitmapVoice,null,rectImage,mPaint); } } //對獲取到的位圖進行大小縮放 private Bitmap scaleBitmap(int id) { Bitmap mTImage = BitmapFactory.decodeResource(getResources(), id); // 獲得圖片的寬高 int width = mTImage.getWidth(); int height = mTImage.getHeight(); // 設置想要的大小 int newWidth = (getMeasuredWidth()-getPaddingLeft()-getPaddingRight())*2/3; int newHeight = (getMeasuredHeight()-getPaddingTop()-getPaddingBottom())*2/3; // 計算縮放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要縮放的matrix參數 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的圖片 return Bitmap.createBitmap(mTImage, 0, 0, width, height, matrix, true); }}

到這里基本就實現了我們需要的自定義控件了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇赉县| 东辽县| 三台县| 池州市| 九龙坡区| 久治县| 喀喇| 建昌县| 年辖:市辖区| 安义县| 大方县| 西和县| 秀山| 嵊泗县| 梁平县| 徐汇区| 修武县| 微山县| 台北市| 马公市| 林口县| 皋兰县| 琼海市| 古浪县| 嘉定区| 固镇县| 新乐市| 诸暨市| 砚山县| 海淀区| 江源县| 巴彦淖尔市| 新龙县| 唐山市| 马公市| 瓦房店市| 宾阳县| 迁西县| 河北省| 峡江县| 博罗县|