From 5a8fec79bee80a453adca739ac7317193c3e6541 Mon Sep 17 00:00:00 2001 From: CretinZp Date: Fri, 9 Mar 2018 14:09:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=B1=9E=E6=80=A7=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 39 +- .../www/wheelsurfdemo/MainActivity.java | 102 +++++ app/src/main/res/layout/activity_main.xml | 19 +- .../view/WheelSurfPanView.java | 374 +++++++++++------- .../wheelsruflibrary/view/WheelSurfView.java | 149 ++++++- 5 files changed, 547 insertions(+), 136 deletions(-) diff --git a/README.md b/README.md index ae9316a..a49a4fc 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,44 @@ typenum必传 typenum必传 typenum必传 - + +四、应用户要求,需要实现用代码设置这些属性,现在已实现 + + 请注意: + 使用这种方式需要在引入布局文件的时候在布局文件中设置mTypeNums = -1 来告诉我你现在要用代码传入这些属性 + 使用这种方式需要在引入布局文件的时候在布局文件中设置mTypeNums = -1 来告诉我你现在要用代码传入这些属性 + 使用这种方式需要在引入布局文件的时候在布局文件中设置mTypeNums = -1 来告诉我你现在要用代码传入这些属性 + + 重要的事情说三遍 + + 例如 + + + 请注意: + 你在传入所有的图标文件之后需要调用 WheelSurfView.rotateBitmaps() 此方法来处理一下你传入的图片 + 你在传入所有的图标文件之后需要调用 WheelSurfView.rotateBitmaps() 此方法来处理一下你传入的图片 + 你在传入所有的图标文件之后需要调用 WheelSurfView.rotateBitmaps() 此方法来处理一下你传入的图片 + + 重要的事情说三遍 + + 然后调用setConfig()方法来设置你的属性 + + 请注意: + .setmColors(colors) + .setmDeses(des) + .setmIcons(mListBitmap) + 这三个方法中的参数长度必须一致 否则会报运行时异常 + + 如果需要此功能,请使用最新的版本 + ``` + compile 'com.github.MZCretin:WheelSurfDemo:v1.1.0' + ``` + ------------------- ### 效果预览 diff --git a/app/src/main/java/com/cretin/www/wheelsurfdemo/MainActivity.java b/app/src/main/java/com/cretin/www/wheelsurfdemo/MainActivity.java index 65b9322..523d611 100644 --- a/app/src/main/java/com/cretin/www/wheelsurfdemo/MainActivity.java +++ b/app/src/main/java/com/cretin/www/wheelsurfdemo/MainActivity.java @@ -2,6 +2,9 @@ import android.animation.ValueAnimator; import android.content.DialogInterface; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; @@ -11,6 +14,8 @@ import com.cretin.www.wheelsruflibrary.listener.RotateListener; import com.cretin.www.wheelsruflibrary.view.WheelSurfView; +import java.util.ArrayList; +import java.util.List; import java.util.Random; public class MainActivity extends AppCompatActivity { @@ -104,5 +109,102 @@ public void onClick(DialogInterface dialog, int which) { } }); + + + /** + * 新增使用代码设置属性的方式 + * + * 请注意: + * 使用这种方式需要在引入布局文件的时候在布局文件中设置mTypeNums = -1 来告诉我你现在要用代码传入这些属性 + * 使用这种方式需要在引入布局文件的时候在布局文件中设置mTypeNums = -1 来告诉我你现在要用代码传入这些属性 + * 使用这种方式需要在引入布局文件的时候在布局文件中设置mTypeNums = -1 来告诉我你现在要用代码传入这些属性 + * + * 重要的事情说三遍 + * + * 例如 + * + * + * 然后调用setConfig()方法来设置你的属性 + * + * 请注意: + * 你在传入所有的图标文件之后需要调用 WheelSurfView.rotateBitmaps() 此方法来处理一下你传入的图片 + * 你在传入所有的图标文件之后需要调用 WheelSurfView.rotateBitmaps() 此方法来处理一下你传入的图片 + * 你在传入所有的图标文件之后需要调用 WheelSurfView.rotateBitmaps() 此方法来处理一下你传入的图片 + * + * 重要的事情说三遍 + * + * 请注意: + * .setmColors(colors) + * .setmDeses(des) + * .setmIcons(mListBitmap) + * 这三个方法中的参数长度必须一致 否则会报运行时异常 + */ + //颜色 + Integer[] colors = new Integer[]{Color.parseColor("#fef9f7"), Color.parseColor("#fbc6a9") + , Color.parseColor("#ffdecc"), Color.parseColor("#fbc6a9") + , Color.parseColor("#ffdecc"), Color.parseColor("#fbc6a9") + , Color.parseColor("#ffdecc")}; + //文字 + String[] des = new String[]{"王 者 皮 肤", "1 8 0 积 分", "L O L 皮 肤" + , "谢 谢 参 与", "2 8 积 分", "微 信 红 包", + "5 Q 币"}; + //图标 + List mListBitmap = new ArrayList<>(); + for ( int i = 0; i < colors.length; i++ ) { + mListBitmap.add(BitmapFactory.decodeResource(getResources(), R.mipmap.iphone)); + } + //主动旋转一下图片 + mListBitmap = WheelSurfView.rotateBitmaps(mListBitmap); + + //获取第三个视图 + final WheelSurfView wheelSurfView2 = findViewById(R.id.wheelSurfView2); + WheelSurfView.Builder build = new WheelSurfView.Builder() + .setmColors(colors) + .setmDeses(des) + .setmIcons(mListBitmap) + .setmType(1) + .setmTypeNum(7) + .build(); + wheelSurfView2.setConfig(build); + + //添加滚动监听 + wheelSurfView2.setRotateListener(new RotateListener() { + @Override + public void rotateEnd(int position, String des) { + Toast.makeText(MainActivity.this, "结束了 位置:" + position + " 描述:" + des, Toast.LENGTH_SHORT).show(); + } + + @Override + public void rotating(ValueAnimator valueAnimator) { + + } + + @Override + public void rotateBefore(ImageView goImg) { + AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); + builder.setTitle("温馨提示"); + builder.setMessage("确定要花费100积分抽奖?"); + builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + //模拟位置 + int position = new Random().nextInt(7) + 1; + wheelSurfView2.startRotate(position); + } + }); + builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }); + builder.show(); + + } + }); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0a9119b..3bc1c62 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -34,7 +34,6 @@ wheelSurfView:icons="@array/icons" wheelSurfView:minTimes="3" wheelSurfView:textColor="#FF0000" - wheelSurfView:textSize="16sp" wheelSurfView:type="1" wheelSurfView:typenum="7" wheelSurfView:vartime="75"> @@ -63,6 +62,24 @@ + + + + + + diff --git a/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfPanView.java b/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfPanView.java index caf91ae..77ecc4b 100644 --- a/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfPanView.java +++ b/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfPanView.java @@ -19,6 +19,7 @@ import android.util.AttributeSet; import android.util.Log; import android.view.View; +import android.widget.TextView; import com.cretin.www.wheelsruflibrary.R; import com.cretin.www.wheelsruflibrary.listener.RotateListener; @@ -78,11 +79,11 @@ public WheelSurfPanView(Context context, AttributeSet attrs, int defStyleAttr) { init(context, attrs); } - //当前类型 + //当前类型 1 自定义模式 2 暴力模式 private int mType; //最低圈数 默认值3 也就是说每次旋转都会最少转3圈 private int mMinTimes; - //分类数量 + //分类数量 如果数量为负数 通过代码设置样式 private int mTypeNum = 6; //每个扇形旋转的时间 private int mVarTime = 75; @@ -101,6 +102,50 @@ public WheelSurfPanView(Context context, AttributeSet attrs, int defStyleAttr) { //文字颜色 private int mTextColor; + public void setmType(int mType) { + this.mType = mType; + } + + public void setmMinTimes(int mMinTimes) { + this.mMinTimes = mMinTimes; + } + + public void setmVarTime(int mVarTime) { + this.mVarTime = mVarTime; + } + + public void setmTypeNum(int mTypeNum) { + this.mTypeNum = mTypeNum; + } + + public void setmDeses(String[] mDeses) { + this.mDeses = mDeses; + } + + public void setmIcons(List mIcons) { + this.mListBitmap = mIcons; + } + + public void setmColors(Integer[] mColors) { + this.mColors = mColors; + } + + public void setmMainImgRes(Integer mMainImgRes) { + this.mMainImgRes = mMainImgRes; + } + + public void setmHuanImgRes(Integer mHuanImgRes) { + this.mHuanImgRes = mHuanImgRes; + } + + public void setmTextSize(float mTextSize) { + this.mTextSize = mTextSize; + } + + public void setmTextColor(int mTextColor) { + this.mTextColor = mTextColor; + } + private void init(Context context, AttributeSet attrs) { mContext = context; setBackgroundColor(Color.TRANSPARENT); @@ -114,95 +159,99 @@ private void init(Context context, AttributeSet attrs) { mMinTimes = typedArray.getInteger(R.styleable.wheelSurfView_minTimes, 3); mTypeNum = typedArray.getInteger(R.styleable.wheelSurfView_typenum, 0); - if ( mVarTime == 0 ) - mVarTime = 75; + if ( mTypeNum == -1 ) { + //用代码去配置这些参数 + } else { + if ( mVarTime == 0 ) + mVarTime = 75; - if ( mTypeNum == 0 ) - throw new RuntimeException("找不到分类数量mTypeNum"); + if ( mTypeNum == 0 ) + throw new RuntimeException("找不到分类数量mTypeNum"); - //每一个扇形的角度 - mAngle = ( float ) (360.0 / mTypeNum); + //每一个扇形的角度 + mAngle = ( float ) (360.0 / mTypeNum); - if ( mType == 1 ) { - mHuanImgRes = typedArray.getResourceId(R.styleable.wheelSurfView_huanImg, 0); - if ( mHuanImgRes == 0 ) - mYuanHuan = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.yuanhuan); - else { - mYuanHuan = BitmapFactory.decodeResource(mContext.getResources(), mHuanImgRes); - } + if ( mType == 1 ) { + mHuanImgRes = typedArray.getResourceId(R.styleable.wheelSurfView_huanImg, 0); + if ( mHuanImgRes == 0 ) + mYuanHuan = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.yuanhuan); + else { + mYuanHuan = BitmapFactory.decodeResource(mContext.getResources(), mHuanImgRes); + } - //文字大小 - mTextSize = typedArray.getDimension(R.styleable.wheelSurfView_textSize, 14); - //文字颜色 默认粉红色 - mTextColor = typedArray.getColor(R.styleable.wheelSurfView_textColor, Color.parseColor("#ff00ff")); - - //描述 - int nameArray = typedArray.getResourceId(R.styleable.wheelSurfView_deses, -1); - if ( nameArray == -1 ) throw new RuntimeException("找不到描述"); - mDeses = context.getResources().getStringArray(nameArray); - //图片 - int iconArray = typedArray.getResourceId(R.styleable.wheelSurfView_icons, -1); - if ( iconArray == -1 ) throw new RuntimeException("找不到分类的图片资源"); - String[] iconStrs = context.getResources().getStringArray(iconArray); - List iconLists = new ArrayList<>(); - for ( int i = 0; i < iconStrs.length; i++ ) { - iconLists.add(context.getResources().getIdentifier(iconStrs[i], "mipmap", context.getPackageName())); - } - mIcons = iconLists.toArray(new Integer[iconLists.size()]); - //颜色 - int colorArray = typedArray.getResourceId(R.styleable.wheelSurfView_colors, -1); - if ( colorArray == -1 ) throw new RuntimeException("找不到背景颜色"); - String[] colorStrs = context.getResources().getStringArray(colorArray); - if ( mDeses == null || mIcons == null || colorStrs == null ) - throw new RuntimeException("找不到描述或图片或背景颜色资源"); - if ( mDeses.length != mTypeNum || mIcons.length != mTypeNum || colorStrs.length != mTypeNum ) - throw new RuntimeException("资源或描述或背景颜色的长度和mTypeNum不一致"); - mColors = new Integer[mTypeNum]; - //分析背景颜色 - for ( int i = 0; i < colorStrs.length; i++ ) { - try { - mColors[i] = Color.parseColor(colorStrs[i]); - } catch ( Exception e ) { - throw new RuntimeException("颜色值有误"); + //文字大小 + mTextSize = typedArray.getDimension(R.styleable.wheelSurfView_textSize, 14 * getScale()); + //文字颜色 默认粉红色 + mTextColor = typedArray.getColor(R.styleable.wheelSurfView_textColor, Color.parseColor("#ff00ff")); + + //描述 + int nameArray = typedArray.getResourceId(R.styleable.wheelSurfView_deses, -1); + if ( nameArray == -1 ) throw new RuntimeException("找不到描述"); + mDeses = context.getResources().getStringArray(nameArray); + //图片 + int iconArray = typedArray.getResourceId(R.styleable.wheelSurfView_icons, -1); + if ( iconArray == -1 ) throw new RuntimeException("找不到分类的图片资源"); + String[] iconStrs = context.getResources().getStringArray(iconArray); + List iconLists = new ArrayList<>(); + for ( int i = 0; i < iconStrs.length; i++ ) { + iconLists.add(context.getResources().getIdentifier(iconStrs[i], "mipmap", context.getPackageName())); } + mIcons = iconLists.toArray(new Integer[iconLists.size()]); + //颜色 + int colorArray = typedArray.getResourceId(R.styleable.wheelSurfView_colors, -1); + if ( colorArray == -1 ) throw new RuntimeException("找不到背景颜色"); + String[] colorStrs = context.getResources().getStringArray(colorArray); + if ( mDeses == null || mIcons == null || colorStrs == null ) + throw new RuntimeException("找不到描述或图片或背景颜色资源"); + if ( mDeses.length != mTypeNum || mIcons.length != mTypeNum || colorStrs.length != mTypeNum ) + throw new RuntimeException("资源或描述或背景颜色的长度和mTypeNum不一致"); + mColors = new Integer[mTypeNum]; + //分析背景颜色 + for ( int i = 0; i < colorStrs.length; i++ ) { + try { + mColors[i] = Color.parseColor(colorStrs[i]); + } catch ( Exception e ) { + throw new RuntimeException("颜色值有误"); + } + } + //加载分类图片 存放图片的集合 + mListBitmap = new ArrayList<>(); + for ( int i = 0; i < mTypeNum; i++ ) { + Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), mIcons[i]); + int ww = bitmap.getWidth(); + int hh = bitmap.getHeight(); + // 定义矩阵对象 + Matrix matrix = new Matrix(); + // 缩放原图 + matrix.postScale(1f, 1f); + // 向左旋转45度,参数为正则向右旋转 + matrix.postRotate(mAngle * i); + //bmp.getWidth(), 500分别表示重绘后的位图宽高 + Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, ww, hh, + matrix, true); + mListBitmap.add(dstbmp); + } + //文字画笔 + mTextPaint = new Paint(); + //设置填充样式 + mTextPaint.setStyle(Paint.Style.STROKE); + //设置抗锯齿 + mTextPaint.setAntiAlias(true); + //设置边界模糊 + mTextPaint.setDither(true); + //设置画笔颜色 + mTextPaint.setColor(mTextColor); + //设置字体大小 + mTextPaint.setTextSize(mTextSize); + } else if ( mType == 2 ) { + mMainImgRes = typedArray.getResourceId(R.styleable.wheelSurfView_mainImg, 0); + //直接大图 + if ( mMainImgRes == 0 ) + throw new RuntimeException("类型为2必须要传大图mMainImgRes"); + mMain = BitmapFactory.decodeResource(mContext.getResources(), mMainImgRes); + } else { + throw new RuntimeException("类型type错误"); } - //加载分类图片 存放图片的集合 - mListBitmap = new ArrayList<>(); - for ( int i = 0; i < mTypeNum; i++ ) { - Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), mIcons[i]); - int ww = bitmap.getWidth(); - int hh = bitmap.getHeight(); - // 定义矩阵对象 - Matrix matrix = new Matrix(); - // 缩放原图 - matrix.postScale(1f, 1f); - // 向左旋转45度,参数为正则向右旋转 - matrix.postRotate(mAngle * i); - //bmp.getWidth(), 500分别表示重绘后的位图宽高 - Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, ww, hh, - matrix, true); - mListBitmap.add(dstbmp); - } - //文字画笔 - mTextPaint = new Paint(); - //设置填充样式 - mTextPaint.setStyle(Paint.Style.STROKE); - //设置抗锯齿 - mTextPaint.setAntiAlias(true); - //设置边界模糊 - mTextPaint.setDither(true); - //设置画笔颜色 - mTextPaint.setColor(mTextColor); - //设置字体大小 - mTextPaint.setTextSize(mTextSize); - } else if ( mType == 2 ) { - mMainImgRes = typedArray.getResourceId(R.styleable.wheelSurfView_mainImg, 0); - //直接大图 - if ( mMainImgRes == 0 ) - throw new RuntimeException("类型为2必须要传大图mMainImgRes"); - mMain = BitmapFactory.decodeResource(mContext.getResources(), mMainImgRes); - } else { - throw new RuntimeException("类型type错误"); } } finally { //回收这个对象 typedArray.recycle(); @@ -315,56 +364,59 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); + if ( mTypeNum == -1 ) { + //先不管 + } else { + if ( mType == 1 ) { + // 计算初始角度 + // 从最上面开始绘制扇形会好看一点 + float startAngle = -mAngle / 2 - 90; + + final int paddingLeft = getPaddingLeft(); + final int paddingRight = getPaddingRight(); + final int paddingTop = getPaddingTop(); + final int paddingBottom = getPaddingBottom(); + int width = getWidth() - paddingLeft - paddingRight; + int height = getHeight() - paddingTop - paddingBottom; + + for ( int i = 0; i < mTypeNum; i++ ) { + //设置绘制时画笔的颜色 + mPaint.setColor(mColors[i]); + //画一个扇形 + RectF rect = new RectF(mCenter - mRadius, mCenter - mRadius, mCenter + + mRadius, mCenter + mRadius); + canvas.drawArc(rect, startAngle, mAngle, true, mPaint); + mTextPaint.setColor(mTextColor); + drawText(startAngle, mDeses[i], mRadius, mTextPaint, canvas); - if ( mType == 1 ) { - // 计算初始角度 - // 从最上面开始绘制扇形会好看一点 - float startAngle = -mAngle / 2 - 90; - - final int paddingLeft = getPaddingLeft(); - final int paddingRight = getPaddingRight(); - final int paddingTop = getPaddingTop(); - final int paddingBottom = getPaddingBottom(); - int width = getWidth() - paddingLeft - paddingRight; - int height = getHeight() - paddingTop - paddingBottom; - - for ( int i = 0; i < mTypeNum; i++ ) { - //设置绘制时画笔的颜色 - mPaint.setColor(mColors[i]); - //画一个扇形 - RectF rect = new RectF(mCenter - mRadius, mCenter - mRadius, mCenter - + mRadius, mCenter + mRadius); - canvas.drawArc(rect, startAngle, mAngle, true, mPaint); - mTextPaint.setColor(mTextColor); - drawText(startAngle, mDeses[i], mRadius, mTextPaint, canvas); - - int imgWidth = mRadius / 3; - - int w = ( int ) (Math.abs(Math.cos(Math.toRadians(Math.abs(180 - mAngle * i)))) * - imgWidth + imgWidth * Math.abs(Math.sin(Math.toRadians(Math.abs(180 - mAngle * i))))); - int h = ( int ) (Math.abs(Math.sin(Math.toRadians(Math.abs(180 - mAngle * i)))) * - imgWidth + imgWidth * Math.abs(Math.cos(Math.toRadians(Math.abs(180 - mAngle * i))))); - - float angle = ( float ) Math.toRadians(startAngle + mAngle / 2); - - //确定图片在圆弧中 中心点的位置 - float x = ( float ) (width / 2 + (mRadius / 2 + mRadius / 12) * Math.cos(angle)); - float y = ( float ) (height / 2 + (mRadius / 2 + mRadius / 12) * Math.sin(angle)); - // 确定绘制图片的位置 - RectF rect1 = new RectF(x - w / 2, y - h / 2, x + w / 2, y + h / 2); - canvas.drawBitmap(mListBitmap.get(i), null, rect1, null); - - //重置开始角度 - startAngle = startAngle + mAngle; - } + int imgWidth = mRadius / 3; - //最后绘制圆环 - Rect mDestRect = new Rect(0, 0, mWidth, mWidth); - canvas.drawBitmap(mYuanHuan, null, mDestRect, mPaint); - } else { - //大圆盘 - Rect mDestRect = new Rect(0, 0, mWidth, mWidth); - canvas.drawBitmap(mMain, null, mDestRect, mPaint); + int w = ( int ) (Math.abs(Math.cos(Math.toRadians(Math.abs(180 - mAngle * i)))) * + imgWidth + imgWidth * Math.abs(Math.sin(Math.toRadians(Math.abs(180 - mAngle * i))))); + int h = ( int ) (Math.abs(Math.sin(Math.toRadians(Math.abs(180 - mAngle * i)))) * + imgWidth + imgWidth * Math.abs(Math.cos(Math.toRadians(Math.abs(180 - mAngle * i))))); + + float angle = ( float ) Math.toRadians(startAngle + mAngle / 2); + + //确定图片在圆弧中 中心点的位置 + float x = ( float ) (width / 2 + (mRadius / 2 + mRadius / 12) * Math.cos(angle)); + float y = ( float ) (height / 2 + (mRadius / 2 + mRadius / 12) * Math.sin(angle)); + // 确定绘制图片的位置 + RectF rect1 = new RectF(x - w / 2, y - h / 2, x + w / 2, y + h / 2); + canvas.drawBitmap(mListBitmap.get(i), null, rect1, null); + + //重置开始角度 + startAngle = startAngle + mAngle; + } + + //最后绘制圆环 + Rect mDestRect = new Rect(0, 0, mWidth, mWidth); + canvas.drawBitmap(mYuanHuan, null, mDestRect, mPaint); + } else { + //大圆盘 + Rect mDestRect = new Rect(0, 0, mWidth, mWidth); + canvas.drawBitmap(mMain, null, mDestRect, mPaint); + } } } @@ -386,4 +438,60 @@ private void drawText(float startAngle, String string, int radius, Paint textPai //绘制文字 canvas.drawTextOnPath(string, circlePath, hOffset, radius / 4, textPaint); } + + //再一次onDraw + public void show() { + //做最后的准备工作 检查数据是否合理 + if ( mType == 1 ) { + if ( mHuanImgRes == null || mHuanImgRes == 0 ) + mYuanHuan = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.yuanhuan); + else { + mYuanHuan = BitmapFactory.decodeResource(mContext.getResources(), mHuanImgRes); + } + //文字大小 + if ( mTextSize == 0 ) + mTextSize = 14 * getScale(); + //文字颜色 默认粉红色 + if ( mTextColor == 0 ) + mTextColor = Color.parseColor("#ff00ff"); + + if ( mListBitmap.size() != mDeses.length || mListBitmap.size() != mColors.length + || mDeses.length != mColors.length ) { + throw new RuntimeException("Icons数量和Deses和Colors三者数量必须与mTypeNum一致"); + } + } else { + //直接大图 + if ( mMainImgRes == null || mMainImgRes == 0 ) + throw new RuntimeException("类型为2必须要传大图mMainImgRes"); + mMain = BitmapFactory.decodeResource(mContext.getResources(), mMainImgRes); + } + + if ( mTextPaint == null ) { + //文字画笔 + mTextPaint = new Paint(); + //设置填充样式 + mTextPaint.setStyle(Paint.Style.STROKE); + //设置抗锯齿 + mTextPaint.setAntiAlias(true); + //设置边界模糊 + mTextPaint.setDither(true); + //设置画笔颜色 + mTextPaint.setColor(mTextColor); + //设置字体大小 + mTextPaint.setTextSize(mTextSize); + } + if ( mTypeNum != 0 ) + mAngle = ( float ) (360.0 / mTypeNum); + if ( mVarTime == 0 ) + mVarTime = 75; + + //重绘 + invalidate(); + } + + private float getScale() { + TextView textView = new TextView(mContext); + textView.setTextSize(1); + return textView.getTextSize(); + } } diff --git a/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfView.java b/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfView.java index db597c6..2f3ec8d 100644 --- a/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfView.java +++ b/wheelsruflibrary/src/main/java/com/cretin/www/wheelsruflibrary/view/WheelSurfView.java @@ -3,6 +3,8 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; +import android.graphics.Bitmap; +import android.graphics.Matrix; import android.os.Build; import android.util.AttributeSet; import android.view.View; @@ -14,6 +16,9 @@ import com.cretin.www.wheelsruflibrary.R; import com.cretin.www.wheelsruflibrary.listener.RotateListener; +import java.util.ArrayList; +import java.util.List; + /** * Created by cretin on 2017/12/26. */ @@ -90,11 +95,38 @@ private void init(Context context, AttributeSet attrs) { @Override public void onClick(View v) { //调用此方法是将主动权交个调用者 由调用者调用开始旋转的方法 - rotateListener.rotateBefore(( ImageView ) v); + if ( rotateListener != null ) + rotateListener.rotateBefore(( ImageView ) v); } }); } + public void setConfig(Builder builder) { + if ( builder.mColors != null ) + mWheelSurfPanView.setmColors(builder.mColors); + if ( builder.mDeses != null ) + mWheelSurfPanView.setmDeses(builder.mDeses); + if ( builder.mHuanImgRes != 0 ) + mWheelSurfPanView.setmHuanImgRes(builder.mHuanImgRes); + if ( builder.mIcons != null ) + mWheelSurfPanView.setmIcons(builder.mIcons); + if ( builder.mMainImgRes != 0 ) + mWheelSurfPanView.setmMainImgRes(builder.mMainImgRes); + if ( builder.mMinTimes != 0 ) + mWheelSurfPanView.setmMinTimes(builder.mMinTimes); + if ( builder.mTextColor != 0 ) + mWheelSurfPanView.setmTextColor(builder.mTextColor); + if ( builder.mTextSize != 0 ) + mWheelSurfPanView.setmTextSize(builder.mTextSize); + if ( builder.mType != 0 ) + mWheelSurfPanView.setmType(builder.mType); + if ( builder.mVarTime != 0 ) + mWheelSurfPanView.setmVarTime(builder.mVarTime); + if ( builder.mTypeNum != 0 ) + mWheelSurfPanView.setmTypeNum(builder.mTypeNum); + mWheelSurfPanView.show(); + } + /** * 开始旋转 * @@ -147,4 +179,119 @@ public void onGlobalLayout() { //记录当前是否是第一次回调onMeasure private boolean isFirst = true; + + + //建造者模式 + public static final class Builder { + //当前类型 1 自定义模式 2 暴力模式 + private int mType = 0; + //最低圈数 默认值3 也就是说每次旋转都会最少转3圈 + private int mMinTimes = 0; + //分类数量 如果数量为负数 通过代码设置样式 + private int mTypeNum = 0; + //每个扇形旋转的时间 + private int mVarTime = 0; + //文字描述集合 + private String[] mDeses; + //自定义图标集合 + private List mIcons; + //背景颜色 + private Integer[] mColors; + //整个旋转图的背景 只有类型为2时才需要 + private Integer mMainImgRes = 0; + //GO图标 + private Integer mGoImgRes = 0; + //圆环的图片引用 + private Integer mHuanImgRes = 0; + //文字大小 + private float mTextSize = 0; + //文字颜色 + private int mTextColor = 0; + + public final WheelSurfView.Builder setmType(int mType) { + this.mType = mType; + return this; + } + + public final WheelSurfView.Builder setmTypeNum(int mTypeNum) { + this.mTypeNum = mTypeNum; + return this; + } + + public final WheelSurfView.Builder setmGoImgRes(int mGoImgRes) { + this.mGoImgRes = mGoImgRes; + return this; + } + + public final WheelSurfView.Builder setmMinTimes(int mMinTimes) { + this.mMinTimes = mMinTimes; + return this; + } + + public final WheelSurfView.Builder setmVarTime(int mVarTime) { + this.mVarTime = mVarTime; + return this; + } + + public final WheelSurfView.Builder setmDeses(String[] mDeses) { + this.mDeses = mDeses; + return this; + } + + public final WheelSurfView.Builder setmIcons(List mIcons) { + this.mIcons = mIcons; + return this; + } + + public final WheelSurfView.Builder setmColors(Integer[] mColors) { + this.mColors = mColors; + return this; + } + + public final WheelSurfView.Builder setmMainImgRes(Integer mMainImgRes) { + this.mMainImgRes = mMainImgRes; + return this; + } + + public final WheelSurfView.Builder setmHuanImgRes(Integer mHuanImgRes) { + this.mHuanImgRes = mHuanImgRes; + return this; + } + + public final WheelSurfView.Builder setmTextSize(float mTextSize) { + this.mTextSize = mTextSize; + return this; + } + + public final WheelSurfView.Builder setmTextColor(int mTextColor) { + this.mTextColor = mTextColor; + return this; + } + + public final Builder build() { + return this; + } + } + + //旋转图片 + public static List rotateBitmaps(List source) { + float mAngle = ( float ) (360.0 / source.size()); + List result = new ArrayList<>(); + for ( int i = 0; i < source.size(); i++ ) { + Bitmap bitmap = source.get(i); + int ww = bitmap.getWidth(); + int hh = bitmap.getHeight(); + // 定义矩阵对象 + Matrix matrix = new Matrix(); + // 缩放原图 + matrix.postScale(1f, 1f); + // 向左旋转45度,参数为正则向右旋转 + matrix.postRotate(mAngle * i); + //bmp.getWidth(), 500分别表示重绘后的位图宽高 + Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, ww, hh, + matrix, true); + result.add(dstbmp); + } + return result; + } }