samedi 25 juin 2016

Rotate a view continously using Canvas

I know I can achieve this via Animation. But I am learning Canvas and I am trying to implement using it. Is there an easy way to rotate a line or Rect continuosly.

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);
            }*/

    }
}

Aucun commentaire:

Enregistrer un commentaire