为什么Google Maps Android中的TextView动画持续时间不一致?

时间:2015-08-29 22:46:14

标签: android google-maps textview android-animation

我有一个TextView,当用户的当前位置在另一个位置附近时,它会闪烁动画。它在地图的左上角附近闪烁。

我设置动画的持续时间为2000毫秒,但它会经常快速闪烁,然后再以正常速率闪烁。

我希望以一致的速度闪烁,但我不确定我的代码有什么问题。

的.java:

private static final LatLng POINTA = new LatLng(32.820193, -117.232568);
private static final LatLng POINTB = new LatLng(32.829129, -117.232204);
private static final LatLng POINTC = new LatLng(32.821114, -117.231534);
private static final LatLng POINTD = new LatLng(32.825157, -117.232003);

private ArrayList<LatLng> markerCoords = new ArrayList<LatLng>();
private static final int POINTS = 4;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    setUpMapIfNeeded();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Create MenuInflater object to insert ActionBar buttons
    MenuInflater inflater = getMenuInflater();

    // Display the ActionBar buttons from .xml
    inflater.inflate(R.menu.menu_map, menu);

    return true;
}

@Override
protected void onResume()
{
    super.onResume();
    setUpMapIfNeeded();

}

private void setUpMapIfNeeded()
{
    if (mMap == null)
    {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        // Check if we were successful in obtaining the map.
        if (mMap != null)
        {
            markerCoords.add(POINTA);
            markerCoords.add(POINTB);
            markerCoords.add(POINTC);
            markerCoords.add(POINTD);

            setUpMap();
        }
    }
}
private void setUpMap()
{
    mMap.setMyLocationEnabled(true);


    mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener()
        {
            @Override
            public void onMyLocationChange(Location location)
            {
                Location target = new Location("");
                for (LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD})
                {
                    target.setLatitude(point.latitude);
                    target.setLongitude(point.longitude);

                    if (location.distanceTo(target) < 500)
                    { 
                        LatLng newLatLng = new LatLng(target.getLatitude(), target.getLongitude());

                        if (newLatLng.equals(POINTA) )
                        {
                            nearestPlaces.setText("You are near Point A");
                        }
                        else if (newLatLng.equals(POINTB))
                        {
                            nearestPlaces.setText("You are near Point B");
                        }
                        else if (newLatLng.equals(POINTC))
                        {
                            nearestPlaces.setText("You are near Point C");
                        }
                        else if (newLatLng.equals(POINTD))
                        {
                            nearestPlaces.setText("You are near Point D");
                        }

                        blink();
                    }
                }
            }
        });


    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    if (myLocation != null)
    {
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        currLocation = new LatLng(latitude, longitude);
    }

    for (int i = 0; i < POINTS; i++)
    {
        mMap.addMarker(new MarkerOptions()
                .position(markerCoords.get(i))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.post_it_marker)));
    }
}

private void blink()
    {
        nearestPlaces = (TextView)findViewById(R.id.nearest);

        Animation animate = new AlphaAnimation(0.0f, 1.0f);

        animate.setDuration(2000);

        animate.setStartOffset(100);

        animate.setRepeatMode(Animation.REVERSE);

        animate.setRepeatCount(Animation.INFINITE);

        nearestPlaces.startAnimation(animate);
    }

1 个答案:

答案 0 :(得分:1)

每次更改文字时都无需致电blink。由于这个原因,动画不一致。

我建议您在blink

中拨打onCreate一次
nearestPlaces.setText("");
blink();

然后在onMyLocationChange中只需更改文字即可,无需调用blink

nearestPlaces.setText("You are near Point A");