1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
| package com.cm.widget;
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.PaintFlagsDrawFilter; import android.graphics.Path; import android.graphics.Path.Direction; import android.graphics.Path.FillType; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Xfermode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.widget.ImageView;
public class RoundMenuView extends ImageView implements OnGestureListener { private static final int childMenuSize = 8; private static final float childAngle = 360f / childMenuSize; private float offsetAngle = 0; private Paint paint; private GestureDetector gestureDetector; private int selectId = -1;
public RoundMenuView(Context context, AttributeSet attrs) { super(context, attrs); init(); }
private void init() { paint = new Paint(); paint.setColor(Color.BLACK); paint.setAntiAlias(true); paint.setFlags(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
gestureDetector = new GestureDetector(this); }
private void drawArc(Canvas canvas, RectF rectF) {
for (int i = 0; i < childMenuSize; i++) { paint.setColor(Color.BLUE);
if(i == selectId){ paint.setStyle(Style.FILL); canvas.drawArc(rectF, i * childAngle + offsetAngle, childAngle, true, paint); }else{ paint.setStyle(Style.STROKE); canvas.drawArc(rectF, i * childAngle + offsetAngle, childAngle, true, paint); }
double x = 200 + getRoundX(200f/3*2, i, childMenuSize, offsetAngle+childAngle/2); double y = 200 + getRoundY(200f/3*2, i, childMenuSize, offsetAngle+childAngle/2);
String str = "菜单"+i; Rect rect = new Rect(); paint.getTextBounds(str, 0, str.length(), rect); int strW = rect.width(); int strH = rect.height(); paint.setColor(Color.WHITE); canvas.drawText(str, (float)x-strW/2, (float)y-strH/2, paint);
} }
private double getRoundY(float r,int i,int n,float offset_angle) { return r * Math.sin(i * 2 * Math.PI / n + Math.PI / 180 * offset_angle); }
private double getRoundX(float r,int i,int n,float offset_angle) { return r * Math.cos(i * 2 * Math.PI / n + Math.PI / 180 * offset_angle); }
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: selectId = whichSector(event.getX()-200, event.getY()-200, 200); invalidate(); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } gestureDetector.onTouchEvent(event); return true; }
@Override protected void onDraw(Canvas canvas) {
RectF rectF = new RectF(0, 0, 400, 400); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setColor(Color.LTGRAY); canvas.drawCircle(200, 200, 200, paint); paint.setColor(Color.GREEN); canvas.drawCircle(200, 200, 190, paint); paint.setColor(Color.GRAY); canvas.drawCircle(200, 200, 180, paint);
paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); drawArc(canvas, new RectF(20, 20, 380, 380));
paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); canvas.drawCircle(200, 200, 25, paint);
Path path = new Path(); path.moveTo(175, 200); path.lineTo(225, 200); path.lineTo(200, 240); path.close(); canvas.drawPath(path, paint);
paint.setColor(Color.MAGENTA); canvas.drawCircle(200, 200, 20, paint);
}
private double calculateScrollAngle(float px1, float py1, float px2, float py2) { double radian1 = Math.atan2(py1, px1); double radian2 = Math.atan2(py2, px2); double diff = radian2 - radian1; return Math.round(diff / Math.PI * 180); }
@Override public boolean onDown(MotionEvent e) { return false; }
@Override public void onShowPress(MotionEvent e) {
}
@Override public boolean onSingleTapUp(MotionEvent e) { return false; }
@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { float tpx = e2.getX() - 200; float tpy = e2.getY() - 200; float disx = (int) distanceX; float disy = (int) distanceY; double scrollAngle = calculateScrollAngle(tpx, tpy, tpx + disx, tpy + disy); offsetAngle -= scrollAngle; selectId = whichSector(0, 40, 200); invalidate(); Log.e("CM", "offsetAngle:" + offsetAngle); return true; }
@Override public void onLongPress(MotionEvent e) {
}
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; }
private int whichSector(double X, double Y, double R) {
double mod; mod = Math.sqrt(X * X + Y * Y); double offset_angle; double arg; arg = Math.round(Math.atan2(Y, X) / Math.PI * 180); arg = arg < 0? arg+360:arg; if(offsetAngle%360 < 0){ offset_angle = 360+offsetAngle%360; }else{ offset_angle = offsetAngle%360; } if (mod > R) { return -2; } else { for(int i=0;i<childMenuSize;i++){ if(isSelect(arg, i,offset_angle) || isSelect(360+arg, i,offset_angle)){ return i; } } } return -1; }
private boolean isSelect(double arg, int i, double offsetAngle) { return arg>(i*childAngle+offsetAngle%360) && arg<((i+1)*childAngle+offsetAngle%360); }
}
|