首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android谷歌地图中的动画弹跳图标

问Android谷歌地图中的动画弹跳图标
EN

Stack Overflow用户
提问于 2014-10-30 04:10:47
回答 1查看 911关注 0票数 2

我正在尝试用Android的Google中的标记创建一个动画。

所期望的效果如下:gromia.gif?dl=0

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

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

谢谢您:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-18 02:23:47

我已经创建了XML对象,然后对它们进行了动画化。这是唯一的办法,它太沉重的图像。这是基本代码(我还没有重新创建整个动画)。

我认为这将是加速动画的另一种解决方案,但到目前为止,这是我想出的唯一想法。

代码语言:javascript
复制
new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        animateMarker(new LatLng(object.la, object.lo), object, numberOfClusters);
                    }
                }, delay*index);

调用此函数的代码:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26644924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档