首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为drawLine使用rotateAnimation

为drawLine使用rotateAnimation
EN

Stack Overflow用户
提问于 2015-06-12 20:13:42
回答 2查看 109关注 0票数 0

我有一条线,我用"canvas.drawLine( 250,400,250,200,p3 )“画它,我想用rotateAnimation旋转它,有没有办法呢?当我试图将drawLine(...)放在一个方法中时,它没有被编译...

代码语言:javascript
复制
import android.widget.Toast;

class Circles<Graphics> extends View
{
public Circles(Context context)
  {
      super(context);    
  }

  public void onDraw(final Canvas canvas) 
  {
      super.onDraw(canvas);
      //2 Circels
      Paint p1 = new Paint();
      p1.setColor(Color.BLUE);
      p1.setStyle(Style.STROKE);
       Paint p2 = new Paint();
      p2.setColor(Color.RED);
      p2.setStyle(Style.FILL);
      canvas.drawCircle(250, 400, 250, p1);
      canvas.drawCircle(250, 400, 20, p2);
    //  invalidate();

      // Seconds
      final Paint p3 = new Paint();
      p3.setColor(Color.RED);
      p3.setStyle(Style.FILL);
      int b1 ; 

      Runnable seconds = new Runnable() {
            public void run() {
         try {
         int seconds = 0;
     RotateAnimation rotateAnimation = new RotateAnimation(
                   (seconds - 1) * 6, seconds * 6,
                   Animation.RELATIVE_TO_SELF, 0.5f,
                   Animation.RELATIVE_TO_SELF, 0.5f);
         rotateAnimation.setInterpolator(new LinearInterpolator());
         rotateAnimation.setDuration(1000);
         rotateAnimation.setFillAfter(true);

    // now rotate this   canvas.drawLine(250, 400 , 250 , 200, p3 ) ; 


         } catch (Exception e) {

          }
  }

      };
  } 
EN

回答 2

Stack Overflow用户

发布于 2015-06-18 02:16:36

如果你想使用动画来绘制圆线,请使用我的代码。有一个动画,它只给你提供旋转动画的插值时间,所以你可以简单地刷新toAngleValue并刷新绘图状态,以平滑你的线条绘制。

代码语言:javascript
复制
/**
 * Created by GIGAMOLE on 17.06.2015.
 */
public class CustomView extends View {

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        startAnimation(new CustomAnimation(fromAngleValue, toAngleValue));
        setWillNotDraw(false);
    }

    //Out values for angle circle
    private float fromAngleValue = 0f;
    private float toAngleValue = 360f;
    private float currentAngleValue;

    //Paint
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG) {
        {
            setDither(true);
            setColor(Color.RED);
            setStrokeWidth(5);
            setStyle(Style.STROKE);
        }
    };

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

        //Always draw arc and invalidate it in animation
        canvas.drawArc(
                new RectF(
                        50,
                        50,
                        getWidth() - 50,
                        getHeight() - 50
                ),
                fromAngleValue,
                currentAngleValue,
                false,
                paint
        );
    }

    private class CustomAnimation extends RotateAnimation {

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

        public CustomAnimation(float fromDegrees, float toDegrees) {
            super(fromDegrees, toDegrees);

            setDuration(3000);
            setFillAfter(true);
        }

        public CustomAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
            super(fromDegrees, toDegrees, pivotX, pivotY);
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            currentAngleValue = interpolatedTime * toAngleValue;
            invalidate();

            //Change current value relative to animation
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-06-18 02:22:00

你也可以使用esiest方法,它只在失效后增加值,但没有像混凝土持续时间和任何类型的内插器这样的功能。

代码语言:javascript
复制
/**
 * Created by GIGAMOLE on 17.06.2015.
 */
public class CustomView extends View {

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setWillNotDraw(false);
    }

    //Out values for angle circle
    private float fromAngleValue = 0f;
    private float toAngleValue = 360f;
    private float currentAngleValue;

    //Paint
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG) {
        {
            setDither(true);
            setColor(Color.RED);
            setStrokeWidth(5);
            setStyle(Style.STROKE);
        }
    };

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

        //Always draw arc and invalidate it in animation

        canvas.drawArc(
                new RectF(
                        50,
                        50,
                        getWidth() - 50,
                        getHeight() - 50
                ),
                fromAngleValue,
                currentAngleValue,
                false,
                paint
        );

        if (currentAngleValue <= toAngleValue) {
            currentAngleValue++;
            postInvalidateDelayed(1);
        }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30802804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档