我有一条线,我用"canvas.drawLine( 250,400,250,200,p3 )“画它,我想用rotateAnimation旋转它,有没有办法呢?当我试图将drawLine(...)放在一个方法中时,它没有被编译...
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) {
}
}
};
} 发布于 2015-06-18 02:16:36
如果你想使用动画来绘制圆线,请使用我的代码。有一个动画,它只给你提供旋转动画的插值时间,所以你可以简单地刷新toAngleValue并刷新绘图状态,以平滑你的线条绘制。
/**
* 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
}
}
}发布于 2015-06-18 02:22:00
你也可以使用esiest方法,它只在失效后增加值,但没有像混凝土持续时间和任何类型的内插器这样的功能。
/**
* 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);
}
}
}https://stackoverflow.com/questions/30802804
复制相似问题