大家好,又见面了,我是你们的朋友全栈君。
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by Administrator on 2017/4/17. */ public class MyView extends View{ private static final String TAG = "TJView"; private int padding = 8; //画笔工具 private Paint mPaint; //圆心坐标 private float currentX = 0; private float currentY = 0; //大圆半径 private float radiusBig = 200; //中圆半径 private float radiusCenter = radiusBig / 2; //小圆半径 private float radiusSmall = radiusCenter / 3; private int width; private int height; private float distance; private final float increment = 10; private final float min = 50; public MyView(Context context) { this(context, null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setAntiAlias(true); // setAnimation(); } /** * 设置缩放动画 */ /*private void setAnimation() { ScaleAnimation scaleAnimation2 = new ScaleAnimation(0.7f, 1.0f, 0.7f, 1.0f, ScaleAnimation.RELATIVE_TO_PARENT, 0.5f, ScaleAnimation.RELATIVE_TO_PARENT, 0.5f); scaleAnimation2.setDuration(500);// 设置持续时间 scaleAnimation2.setRepeatCount(99999);// 设置重复次数 scaleAnimation2.setFillAfter(true);// 保持动画结束时的状态 startAnimation(scaleAnimation2); }*/ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawBg(canvas); drawLeftHalfCirle(canvas); drawTBCirle(canvas); } /** * 画上下两个圆--中圆和小圆 * * @param canvas */ private void drawTBCirle(Canvas canvas) { //画上面的白中圆 mPaint.setColor(Color.WHITE); canvas.drawCircle(currentX, currentY - radiusBig / 2, radiusCenter, mPaint); //画上面的黑小圆 mPaint.setColor(Color.BLACK); canvas.drawCircle(currentX, currentY - radiusBig / 2, radiusSmall, mPaint); //画下面的黑中圆 mPaint.setColor(Color.BLACK); canvas.drawCircle(currentX, currentY + radiusBig / 2, radiusCenter, mPaint); //画下面的白小圆 mPaint.setColor(Color.WHITE); canvas.drawCircle(currentX, currentY + radiusBig / 2, radiusSmall, mPaint); } /** * 画左边半圆 * * @param canvas */ private void drawLeftHalfCirle(Canvas canvas) { mPaint.setColor(Color.WHITE); canvas.drawArc(new RectF(currentX - radiusBig, currentY - radiusBig, currentX + radiusBig, currentY + radiusBig), 90, 180, true, mPaint);//90度开始画180度 } /** * 画黑色的背景底板 * * @param canvas */ private void drawBg(Canvas canvas) { mPaint.setColor(Color.BLACK); canvas.drawCircle(currentX, currentY, radiusBig + padding, mPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); currentX = MeasureSpec.getSize(widthMeasureSpec) / 2; currentY = MeasureSpec.getSize(heightMeasureSpec) / 2; setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); width = getWidth(); height = getHeight(); } @Override public boolean onTouchEvent(MotionEvent event) { /*currentX = event.getX(); currentY = event.getY(); invalidate(); return true;*/ //手指的数量 int count = event.getPointerCount(); float x = event.getX(); float y = event.getY(); int action = event.getAction(); if(action==MotionEvent.ACTION_POINTER_DOWN){ distance = getDistance(event); } if(count==1) { if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_MOVE) { if (x > width - radiusBig) { x = width - radiusBig; } else if (x < radiusBig) { currentX = radiusBig; } else { currentX = x; } if (y > height - radiusBig) { currentY = height - radiusBig; } else if (y < radiusBig) { currentY = radiusBig; } else { currentY = y; } postInvalidate(); } } else if(count==2&&action == MotionEvent.ACTION_MOVE){ float maxR = 0; if(currentX<currentY){ maxR = currentX; }else{ maxR = currentY; } //判断手指方向 if(distance>getDistance(event)){ radiusBig -= increment; if(radiusBig<min){ radiusBig = min; } }else if(distance<getDistance(event)){ radiusBig += increment; if(radiusBig>maxR){ radiusBig = maxR; }else if(radiusBig>width/2){ radiusBig = width/2; } } distance = getDistance(event); postInvalidate(); } return true; } private float getDistance(MotionEvent event){ float xOne = event.getX(0); float yOne = event.getY(0); float xTwo = event.getX(1); float yTwo = event.getY(1); return (xOne - xTwo)*(xOne - xTwo)+(yOne - yTwo)*(yOne - yTwo); } }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136189.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...