gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:55:35 27 4
gpt4 key购买 nike

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

我把动画的持续时间设置为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);
}

最佳答案

您不需要在每次更改文本时都调用blink。动画因此不一致。

我建议在onCreate中调用一次blink

nearestPlaces.setText("");
blink();

然后在 onMyLocationChange 中更改文本,而不调用 blink

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

关于android - 为什么我的 TextView 动画持续时间在 Google Maps Android 中不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291427/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com