Android Google地图中的动画弹跳图标

时间:2014-10-30 04:10:47

标签: android google-maps animation

我正在尝试使用Android中的Google地图中的标记制作动画。

期望的效果如下:https://www.dropbox.com/s/tnlo0rynflmt5um/marker_gromia.gif?dl=0

显然我不能将gif设置为标记的图标..我也有动画的所有框架,但使用它们真的很重要。知道如何完成这个动画吗?

数字的最终外观也可以更“静态”而不是显示的效果。我没有要求代码,但有一个指针/想法如何做到这一点已经很好了。

谢谢:)

1 个答案:

答案 0 :(得分: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>