Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用drawLine的rotateAnimation_Java_Android - Fatal编程技术网

Java 使用drawLine的rotateAnimation

Java 使用drawLine的rotateAnimation,java,android,Java,Android,我有一条线,我用“canvas.drawLine(250400250200P3)”画它,我想用RotateAimation旋转它,有没有其他方法可以做? 当我尝试将绘图线(…)放在一个方法中时,它没有得到编译 import android.widget.Toast; class Circles<Graphics> extends View { public Circles(Context context) { super(context); } p

我有一条线,我用“canvas.drawLine(250400250200P3)”画它,我想用RotateAimation旋转它,有没有其他方法可以做? 当我尝试将
绘图线(…)
放在一个方法中时,它没有得到编译

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) {

          }
  }

      };
  } 
导入android.widget.Toast;
类扩展视图
{
公众圈(背景)
{
超级(上下文);
}
公共空白onDraw(最终画布)
{
super.onDraw(帆布);
//2圈
油漆p1=新油漆();
p1.设置颜色(颜色为蓝色);
p1.设置样式(样式行程);
油漆p2=新油漆();
p2.设置颜色(颜色为红色);
p2.设置样式(填充样式);
画布.画圈(250400250P1);
画布.画圈(25040020P2);
//使无效();
//秒
最终油漆p3=新油漆();
p3.设置颜色(颜色为红色);
p3.设置样式(样式填充);
int b1;
可运行秒数=新的可运行秒数(){
公开募捐{
试一试{
整数秒=0;
RotateAnimation RotateAnimation=新的RotateAnimation(
(秒-1)*6,秒*6,
动画。相对于自身,0.5f,
动画。相对于自身,0.5f);
setInterpolator(新的LinearInterpolator());
旋转想象。设定持续时间(1000);
rotateAnimation.setFillAfter(true);
//现在旋转这个画布。抽绳(250400250200P3);
}捕获(例外e){
}
}
};
} 

如果要使用动画绘制圆线,请使用我的代码。 有一个动画只为您提供旋转动画的插值时间,因此您可以简单地刷新
到角度值
,并刷新绘图状态以平滑绘制线条



此外,您还可以使用ESESET方法,该方法仅使用post invalidate增加值,但没有诸如混凝土持续时间和任何类型的INTELPOLATOR之类的功能


/**
*由GIGAMOLE于2015年6月17日创建。
*/
公共类CustomView扩展了视图{
公共自定义视图(上下文){
这个(上下文,空);
}
公共自定义视图(上下文、属性集属性){
这(上下文,属性,0);
}
公共自定义视图(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
setWillNotDraw(假);
}
//输出角度圆的值
私人浮动fromAngleValue=0f;
私人浮动角度值=360f;
私人浮动价值;
//油漆
私有油漆油漆=新油漆(油漆.防油漆别名标志){
{
设置抖动(真);
setColor(Color.RED);
设定行程宽度(5);
设置样式(样式笔划);
}
};
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//始终绘制圆弧并在动画中使其无效
帆布拉弧(
新RectF(
50,
50,
getWidth()-50,
getHeight()-50
),
fromAngleValue,
当前角度值,
假,,
油漆
);

如果(currentAngleValue否,您不能这样做,请改用ValueAnimator/ObjectAnimator可以给我看一个代码示例吗?google叔叔?关于这个主题有无数页
/**
 * 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
        }
    }
}
/**
 * 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);
        }
    }

}