2014-02-27 58 views
1

我想準備動畫漣漪效果我在圖片上給你看enter image description here 我有興趣做出這個溺愛的圓圈,這個溺愛的圓圈應該從一個點褪去,變得越來越大,最後,圓圈應該消失,所以這應該看起來像這個屏幕上。這就像波紋效應如何動畫波紋溺愛效果?

現在我寫的代碼alghoritm創建新的圈子變得越來越大,然後消失,所以不是這個圈子,我想有點。

這是我的代碼threadclass:

import android.annotation.SuppressLint; 
import android.graphics.Canvas; 
import android.view.SurfaceHolder; 

public class CanvasThreadForCanvas extends Thread { 
    private SurfaceHolder mySurfaceHolder; 
    private PanelForCanvas myPanel; 
    public static boolean runIt = false; 

    public CanvasThreadForCanvas(SurfaceHolder surfaceHolder, 
      PanelForCanvas panel) 
    { 
     mySurfaceHolder = surfaceHolder; 
     myPanel = panel; 

    } 
    public void setRunning(boolean run) 
    { 
     runIt= run; 
    } 

    @SuppressLint("WrongCall") 
    @Override 
    public void run() { 
     Canvas c; 
     while(runIt) 
     { 
      try { 

       // how fast will be invoked on draw method 
       Thread.sleep(10, 0); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      c = null; 
      try 
      { 

       synchronized(mySurfaceHolder) 
       { 

        c = mySurfaceHolder.lockCanvas(null); 
        myPanel.onDraw(c); 
       } 
      }finally 
      { 
       if(c!= null) 
       { 
        mySurfaceHolder.unlockCanvasAndPost(c); 
       } 
      } 
     } 
     super.run(); 
    } 



} 

和方法,其中平局,我準備5線

public class PanelForCanvas extends SurfaceView implements SurfaceHolder.Callback { 

private CanvasThreadForCanvas canvasthread; 

public PanelForCanvas(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    getHolder().addCallback(this); 
    canvasthread = new CanvasThreadForCanvas(getHolder(),this); 
    setFocusable(true); 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 


} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    canvasthread.setRunning(true); 
    canvasthread.start(); 

} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    boolean retry = true; 
    canvasthread.setRunning(false); 
    while(retry) 
    { 
     try 
     { 
      canvasthread.join(); 
      retry = false; 
      canvasthread.setRunning(false); 
     } 
     catch(InterruptedException e) 
     { 

     } 
     catch(NullPointerException e) 
     { 

     } 
    } 

} 


int radiusOfCircle =50; 
boolean circleend = false; 


//set of values for every line in the animation 
//so we'll see 4 line in one moment 
// for first line 
Paint line1; 
float levelOfAlpha1 =255; 

int line2luncher=0; 

// for second line 
Paint line2; 
float levelOfAlpha2 =255; 
int line3luncher=0; 


//for third line 

Paint line3; 
float levelOfAlpha3 =255; 
int line4luncher=0; 


// for fourth line 
Paint line4; 
float levelOfAlpha4 =255; 
int line5luncher=0; 

// for second line 
Paint line5; 
float levelOfAlpha5 =255; 



@Override 
protected void onDraw(Canvas canvas) { 
    Paint paint = new Paint(); 

    Paint linePaint = new Paint(); 


    //Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.bccb3e123050fb9165ee8a91c447bcf3); 
    canvas.drawColor(Color.WHITE); 



    // need to add this style when you need to draw f.example 
    // circle without filling it 

    circleend = true; 
    linePaint.setStyle(Paint.Style.STROKE); 

    linePaint.setStrokeWidth(5); 


    // give for every line color/style/ Stroke 
    line1 =line2= line3 =line4 =line5 = linePaint; 

    // the part where we animating fade lines 
    // drawing this circle line 

    // first line 

    if(circleend == true) 
    { 
     // levelOfAlpha1 is set on the begining to 255, which 
     // means that it will be full colorer, as much as levelOfAlpha1 is 
     // decreasing as much the color became more transparently 
     // so if the level is set to 0 we didn't see any color in this 
     // place 
     line1.setColor(Color.argb((int) levelOfAlpha1, 135, 206, 250)); 
     canvas.drawCircle(1300, 0, 150, line1); 

     // -3.4 is taken from calculation 
     // 255 is max, we want to get the 0 during 
     // one cycle of circle growth, 
     // the loop must be made 75 times to make circle 
     // growing from min to max 
     // so 255/ 75 = 3.4 
     if(levelOfAlpha1==0) 
     { 
      levelOfAlpha1=255; 
     } 
     else 
     { 
      levelOfAlpha1-=3.4; 
      //after 5 cycles line luncher will be 5 
      //which lunch the animation of second line 
      if(line2luncher!=20){ 
       line2luncher++; 
      } 


     } 

    } 

    if(line2luncher==20) 
    { 
     //this same as for first line 
     line2.setColor(Color.argb((int) levelOfAlpha2, 135, 206, 250)); 
     canvas.drawCircle(1300, 0, 175, line2); 

     if(levelOfAlpha2==0) 
     { 
      levelOfAlpha2=255; 
     } 
     else 
     { 
      levelOfAlpha2-=3.4; 
      if(line3luncher!=20){ 
       line3luncher++; 
      } 
     } 
    } 

    if(line3luncher==20) 
    { 
     //this same as for first line 
     line3.setColor(Color.argb((int) levelOfAlpha3, 135, 206, 250)); 
     canvas.drawCircle(1300, 0, 200, line3); 

     if(levelOfAlpha3==0) 
     { 
      levelOfAlpha3=255; 
     } 
     else 
     { 
      levelOfAlpha3-=3.4; 
      if(line4luncher!=20){ 
       line4luncher++; 
      } 
     } 
    } 

    if(line4luncher==20) 
    { 
     //this same as for first line 
     line4.setColor(Color.argb((int) levelOfAlpha4, 135, 206, 250)); 
     canvas.drawCircle(1300, 0, 225, line4); 

     if(levelOfAlpha4==0) 
     { 
      levelOfAlpha4=255; 
     } 
     else 
     { 
      levelOfAlpha4-=3.4; 
      if(line5luncher!=20){ 
       line5luncher++; 
      } 
     } 
    } 

    if(line5luncher==20) 
    { 
     //this same as for first line 
     line5.setColor(Color.argb((int) levelOfAlpha5, 135, 206, 250)); 
     canvas.drawCircle(1300, 0, 250, line5); 

     if(levelOfAlpha5==0) 
     { 
      levelOfAlpha5=255; 
     } 
     else 
     { 
      levelOfAlpha5-=3.4;    
     } 
    } 
} 

,以及如何在屏幕上看起來。 enter image description here它並不那麼美麗。
我怎樣才能得到這種點的效果?

如果你知道任何更簡單的方法來獲得重複動畫圓點的相同效果,我將不勝感激。

+0

嘿,我需要你的幫助。我想顯示這樣的動畫可以提供給我確切的代碼。如果你可以,那麼它會對我很有幫助 –

+0

我的動畫看起來不太好,但給我郵件,我會發送代碼。 – MyWay

+0

謝謝。我會得到一個主意,至少 [email protected] –

回答

1

linePaint.setPathEffect(new DashPathEffect(new float[] {3,6}, 0));怎麼樣?

+0

沒錯這就是THX的作品很多 – MyWay

+0

好!你可以接受這樣的答案;) – Zoubiock

+0

順便說一下,不要在draw方法上創建Paint對象。創建它作爲全局變量並重用它,它會更快 – Zoubiock