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

标签 android google-maps textview android-animation

我有一个 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/

相关文章:

javascript - Android 下的 Firefox/Chrome 在键入字母时不会触发 OnKeyPress、onKeyDown、onKeyUp 事件

java - 如何实现继承的抽象方法?

android - 至于TextView,以编程方式改变样式?

android - Textview 根据屏幕宽度的百分比居中对齐

java - 在 Android 上围绕现有的 PHP 系统实现 AES 加密/解密

java - WebView抓取表单数据

android - 在我的 android 应用程序的谷歌地图中添加我的位置按钮

javascript - 嵌入 Google 地球,在卫星 View 中创建网格布局

javascript - Google map 在单击标记时分配一个 php 弹出窗口

java - 如何更改其他 Activity 的文本?