2015-04-07 411 views
1

guys 我嘗試實現一個弧形頭上的黑色箭頭。箭頭用弧線生動,並具有圓弧軌跡。我已經根據不同的值實施了帶有動畫的紅色弧線。 請參閱附件。 如何在紅色弧線上實現黑色箭頭?因爲如果我採用與紅色圓弧動畫相同的方式,黑色箭頭將打印出不需要的軌跡。如何用動畫在畫布android上繪製帶圓弧軌跡的箭頭?

在此先感謝! 萊昂

enter image description here

回答

1

所有你需要的是畫布。

這裏是一個例子。

這是您的抽獎類:

public class CircleView extends View 
{ 
    public CircleView(Context context) { 
     super(context); 
     path = new Path(); 
     paint = new Paint(); 
    } 

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

     float angle = 270; 

     float radius = canvas.getWidth()/3; 
     float x = canvas.getWidth()/2; 
     float y = canvas.getHeight()/2; 
     final RectF oval = new RectF(); 

     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeWidth(25); 

     oval.set(x - radius, y - radius, x + radius,y + radius); 

     // Draw circle 
     paint.setColor(Color.RED); 
     canvas.drawArc(oval, 135, angle, false, paint); 
     paint.setColor(Color.BLUE); 
     canvas.drawArc(oval, angle, 405-angle, false, paint); 

     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(Color.BLACK); 

     float l = 1.2f; 
     float a = angle*(float)Math.PI/180; 
     // Draw arrow 
     path.moveTo(x+ (float)Math.cos(a) *radius, y + (float)Math.sin(a) * radius); 
     path.lineTo(x+ (float)Math.cos(a+0.1) *radius*l, y + (float)Math.sin(a+0.1) * radius*l); 
     path.lineTo(x+ (float)Math.cos(a-0.1) *radius*l, y + (float)Math.sin(a-0.1) * radius*l); 
     path.lineTo(x+ (float)Math.cos(a) *radius, y + (float)Math.sin(a) * radius); 
     canvas.drawPath(path, paint); 
    } 
    private Path path; 
    private Paint paint; 
} 

這是它的活動:

public class MyActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new CircleView(this)); 
    } 
} 

對於角度== 200,你會看到這樣的形象:

angle == 200

IntelliJ Idea的工作副本:https://github.com/weerf/android_circle

+0

嗨,基里爾。感謝您的回覆。我在動畫中使用這個代碼,黑色箭頭在路上繪製。我真正需要的是在紅色弧形動畫的頂部顯示一個箭頭。 (現在有多個箭頭與動畫過程一起繪製)。另一件事是黑箭頭的形狀與我在圖中所描述的不一樣。提前致謝。 –

+0

黑色的箭頭只是一個自由形式的多邊形。我的回答有三個頂點,在問題中有四個頂點。還有一點不同 - 我沒有設置標誌ANTI_ALIAS_FLAG。 如果問題是這樣的: 添加一個頂點到箭頭,並設置標誌ANTI_ALIAS_FLAG –

+0

我糾正它在https://github.com/weerf/android_circle/blob/master/app/src/main/java/com/ podlivaev/circle/CircleView.java圖像將像https://github.com/weerf/android_circle/blob/master/screen.png –