我已經創建了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>
你看着辦吧? – Snake 2014-11-15 03:58:57