Custom View (implement circular rotation and follow the mouse)






 


First: Custom Introduction

Details: https://blog.csdn.net/d7hz99qulu/article/details/56678891



If you study in depth in the custom View, a few articles are far from being able to describe, so this article is only a relatively shallow effect.


The idea of ​​realizing the effect of the above picture:

About circles:

Write several colors into the array, and then divide the entire circle equally according to the length of the color array (360/length of the color array),

If you just want to achieve a circle, drawing a circle directly with a fan shape can satisfy the effect.

About following the mouse movement:

Initialize, rewrite the onTouchEvent method, use switch to implement the mouse Down, Up, Move events, and assign the coordinates to the initialized value


About rotation, about click acceleration,,,




public class MyView2 extends View{

    private Paint paint;
    private int[] colors;
    private int angle;
    private int widthX;
    private int widthY;
    private int coordX;
    private int coordY;
    private RectF rect;
    private Paint paintA;
    private  Paint paintB;
    public MyView2(Context context) {
        this(context,null)
;
    }

    public MyView2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
widthX = getMeasuredWidth();
widthY = getMeasuredHeight();
coordX = widthX/2;
coordY = widthY/2;
}                                        

    private void init() {
        paint = new Paint();
paintA = new Paint();
paintA.setColor(Color.RED);
paintB = new Paint();
paintB.setColor(Color.BLUE);
colors  = new int[]{
                

                

        
                Color.BLUE, Color.RED, Color.YELLOW, Color.CYAN,
                Color.BLACK, Color.GREEN, Color.BLUE, Color.RED, Color.YELLOW, Color.CYAN, Color.BLACK, Color.GREEN
        };
rect = new RectF(-100, -100, 100, 100)
        ;
angle = 360 / colors.length;
}
        
    



    private Canvas mycanvas;
    int d = 30;
@Override
protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
mycanvas = canvas;
canvas.translate(coordX,coordY);
canvas.rotate(d);
drawColor(canvas);
}        
                
        
        
    

    private void drawColor(Canvas canvas){
        int start_angle = 0;
        for (int i = 0; i < colors.length; i++) {
            int color = colors[i];
paint.setColor(color);
canvas.drawArc(rect,start_angle,angle,true,paint);
start_angle += angle;
}            
            
                    
    }

    @Override
public boolean onTouchEvent(MotionEvent event) {    

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                    coordX = (int)event.getX();
coordY = (int) event.getY();
TimerTask timerTask = new TimerTask() {
                    @Override
public void run() {
                        d+=1;
postInvalidate();
}                                                                                                    
                };
                Timer timer = new Timer();
                timer.schedule(timerTask,0,50);
                break;
            case MotionEvent.ACTION_UP:
                coordX = (int)event.getX();
coordY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                coordX = (int)event.getX();
coordY = (                                int) event.getY();
                break;
        }
        postInvalidate();
        return true;
    }


}








 


First: Custom Introduction

Details: https://blog.csdn.net/d7hz99qulu/article/details/56678891



If you study in depth in the custom View, a few articles are far from being able to describe, so this article is only a relatively shallow effect.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469587&siteId=291194637