使用Canvas连续旋转视图

时间:2016-06-17 17:02:16

标签: java android android-custom-view

我正在学习Canvas,我正在尝试使用它。是否有一种简单的方法可以rotate 行或Rect 连续。

ArcView.java

public class ArcView extends View {
    private Paint linePaint;
    private float angle = 45f;
    private float increment = 0.01f;

    private float canvasWidth,canvasHeight;
    private float xo,yo;

    public ArcView(Context context) {
        super(context);
        initVals();
    }

    public ArcView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initVals();
    }

    public ArcView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initVals();
    }

    private void initVals(){

        setBackgroundColor(Color.parseColor("#FFFF00"));
        //setBackgroundResource(R.drawable.process_dafault);
        //setBackground(ContextCompat.getDrawable(mContext,R.drawable.process_dafault));

        linePaint = new Paint();
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.GRAY);
        linePaint.setStrokeWidth(15);

    }

    @Override
    public void draw(final Canvas canvas) {
        super.draw(canvas);

        canvasWidth = getWidth();
        canvasHeight = getHeight();
        xo = canvasWidth / 2f;
        yo = canvasHeight / 2f;
        canvas.translate(xo, yo);


        for (int x = 0; x < 360; x += 45) {
            canvas.rotate(45f);
            canvas.drawLine(0f, 0f, 200f, 200f, linePaint);
        }

        /*for(int index = 0 ; index < 100 ; index++){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    canvas.save();
                    canvas.rotate(15f);
                    canvas.drawLine(0f,0f,200f,200f,linePaint);
                    canvas.restore();
                }
            },200);
            }*/

    }
}

我想用radius绘制一个以极坐标为动画的圆圈。现在我正在尝试使用直线。

首先,我将画布转换为使用中心。

 canvas.translate(xo, yo);

然后

我想找到使用给定公式的x和y polor坐标

x = Radius * Math.cos(theta);
y = Radius * Math.sin(theta);

然后在那一点画线或圆。

canvas.drawLine(0f, 0f, x, y, linePaint);

1 个答案:

答案 0 :(得分:0)

您可以这样做以连续旋转

public class ArcView extends View {
    private Paint linePaint;
    private float angle = 45f;
    private float increment = 0.01f;

    private float canvasWidth, canvasHeight;
    private float xo, yo;
    private float degreeToRotate = 0f;

    public ArcView(Context context) {
        super(context);
        initVals();
    }

    public ArcView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initVals();
    }

    public ArcView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initVals();
    }

    private void initVals() {

        setBackgroundColor(Color.parseColor("#FFFF00"));
        //setBackgroundResource(R.drawable.process_dafault);
        //setBackground(ContextCompat.getDrawable(mContext,R.drawable.process_dafault));

        linePaint = new Paint();
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.GRAY);
        linePaint.setStrokeWidth(15);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (degreeToRotate == -1) return;

        Log.d("alex.hong", "draw with degree = " + degreeToRotate);
        canvasWidth = getWidth();
        canvasHeight = getHeight();
        xo = canvasWidth / 2f;
        yo = canvasHeight / 2f;

        canvas.translate(xo, yo);
        canvas.rotate(degreeToRotate);
        canvas.drawLine(0f, 0f, 200f, 200f, linePaint);
    }

    public void startAnim() {
        new Rotate360Task().execute();
    }

    class Rotate360Task extends AsyncTask<Void, Float, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            float degree = 0f;
            while (degree <= 360f) {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                publishProgress(degree);
                degree += 0.5f;
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Float... values) {
            super.onProgressUpdate(values);

            degreeToRotate = values[0];
            invalidate();
        }
    }

当您想要旋转时,例如单击按钮或其他内容。调用方法startAnim()然后观察。

希望有所帮助!