2014-10-30 52 views
2

我正嘗試在Android中的Google地圖中使用標記創建動畫。Android中的動畫反彈圖標Google地圖

所需的效果是這樣的:https://www.dropbox.com/s/tnlo0rynflmt5um/marker_gromia.gif?dl=0

很明顯,我不能設置GIF作爲標記的圖標..我也有動畫的所有幀,但它真的重來使用它們。任何想法如何完成這個動畫?

數字的最終外觀也可以更「靜態」,而不是顯示的效果。我不要求代碼,但有一個關於如何做到這一點的指針/想法已經很棒了。

謝謝:)

+0

你看着辦吧? – Snake 2014-11-15 03:58:57

回答

0

我已經創建了XML對象,然後對它們進行了動畫處理。這是唯一的方法,它與圖像太重。這是基本的代碼(我沒有重新創建整個動畫)。

我認爲這將是加速動畫的另一種解決方案,但直到現在,這是我想出的唯一想法。

new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         animateMarker(new LatLng(object.la, object.lo), object, numberOfClusters); 
        } 
       }, delay*index); 

是調用這個函數的代碼:

private void animateMarker(final LatLng position, final ClusterData object, final String clusterNumber) { 
    final MarkerOptions markerOptions = new MarkerOptions() 
      .position(position) 
      .anchor(.5f, .5f); 
    //.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.pallino_msg, numberOfClusters))); 

    int px = getResources().getDimensionPixelSize(R.dimen.cluster_size); 

    int startValue = px; 
    final int endValue = new Double(px * 1.5).intValue(); 

    ValueAnimator anim = ValueAnimator.ofInt(startValue, endValue); 
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      int val = (Integer) valueAnimator.getAnimatedValue(); 
      String clusterGiven = "0"; 
      if (val == endValue){ 
       clusterGiven = clusterNumber; 
      } 
      Marker marker = googleMap.addMarker(markerOptions.icon(createBitmap(val, 
        BASE_ALPHA + (val * 20/endValue), clusterGiven))); 
      marker.hideInfoWindow(); 
      clusters.put(marker.getPosition(), object); 
     } 
    }); 
    anim.setDuration(500); 
    anim.start(); 
} 

protected Bitmap writeTextOnDrawable(Bitmap bitmap, String text) { 
    Typeface tf = Typeface.create("fonts/font.ttf", Typeface.BOLD); 

    Paint paint = new Paint(); 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.WHITE); 
    paint.setTypeface(tf); 
    paint.setTextAlign(Paint.Align.CENTER); 
    paint.setTextSize(convertToPixels(getActivity(), 11)); 

    Rect textRect = new Rect(); 
    paint.getTextBounds(text, 0, text.length(), textRect); 

    Canvas canvas = new Canvas(bitmap); 

    //If the text is bigger than the canvas , reduce the font size 
    if (textRect.width() >= (canvas.getWidth() - 4))  //the padding on either sides is considered as 4, so as to appropriately fit in the text 
     paint.setTextSize(convertToPixels(getActivity(), 7));  //Scaling needs to be used for different dpi's 

    //Calculate the positions 
    int xPos = (canvas.getWidth()/2) - 2;  //-2 is for regulating the x position offset 

    //"- ((paint.descent() + paint.ascent())/2)" is the distance from the baseline to the center. 
    int yPos = (int) ((canvas.getHeight()/2) - ((paint.descent() + paint.ascent())/2)); 

    canvas.drawText(text, xPos, yPos, paint); 

    return bitmap; 
} 

public BitmapDescriptor createBitmap(int dimension, int alpha, String clusterNumber){ 
    Bitmap mDotMarkerBitmap = Bitmap.createBitmap(dimension, dimension, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(mDotMarkerBitmap); 
    Drawable shape = getResources().getDrawable(R.drawable.marker_base_shape); 
    shape.setAlpha(alpha); 
    shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight()); 
    shape.draw(canvas); 

    if (Integer.parseInt(clusterNumber) > 0){ 
     return BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(mDotMarkerBitmap, clusterNumber)); 
    } 

    return BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap); 
} 

這是XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > 
    <solid android:color="@color/red" /> 
</shape>