我正在制作一个使用谷歌地图的应用程序,我有一个地方搜索片段应该是不可见的,只有当我按下按钮通过某种简单的动画在地图上添加新地点作为标记时才会出现,例如,从底部垂直移动到中心。如何通过动画使片段出现在屏幕中间,因为它是片段而不仅仅是视图?
这是布局文件:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivityRaw">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawermenu">
</android.support.design.widget.NavigationView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/tb_actionbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_constraint1">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
map:cameraZoom="10"
tools:context="com.example.styledmap.MapsActivityRaw" />
<TextView
android:id="@+id/tv_delete_marker"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_alignBottom="@id/map"
android:layout_marginStart="8dp"
android:text="@string/tv_delete"
android:textAlignment="center"
android:textSize="16sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<fragment
android:id="@+id/place_autocomplete"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_add_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/map"
app:layout_constraintVertical_bias="0.872" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
这是包含地图的活动:
public class MapsActivityRaw extends AppCompatActivity
implements OnMapReadyCallback {
private GoogleMap mMap;
private ArrayList<MarkerOptions> markers = new ArrayList<>();
private Geocoder gcd;
private static final String TAG = MapsActivityRaw.class.getSimpleName();
private LatLng mLatLong;
private String city;
private Location location;
private SQLiteDatabase mDb;
private TextView tv_delete;
private LinkedHashMap<Integer, MarkerOptions> markerHashMap;
private Cursor mCursor;
private Toolbar mActionBar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ConstraintSet mConstraintSet = new ConstraintSet();
private ConstraintSet mConstraintSet2 = new ConstraintSet();
private ConstraintLayout mConstraintLayout;
private boolean mOld = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Fragment fragment_place_search = new Fragment();
getSupportFragmentManager().beginTransaction().add(R.id.place_autocomplete, fragment_place_search).commit();
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps_raw);
Button btnAddPlace = (Button) findViewById(R.id.btn_add_place);
btnAddPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mOld){
getSupportFragmentManager().beginTransaction().show(fragment_place_search).commit();
mOld = !mOld;
} else {
getSupportFragmentManager().beginTransaction().hide(fragment_place_search).commit();
mOld = !mOld;
}
}
});
mActionBar = (Toolbar) findViewById(R.id.tb_actionbar);
setSupportActionBar(mActionBar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(MapsActivityRaw.this, mDrawerLayout, R.string.open_drawer, R.string.closed_drawer);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Set up views
tv_delete = (TextView) findViewById(R.id.tv_delete_marker);
//Set up Database
PlacesDBHelper placesDBHelper = new PlacesDBHelper(this);
mDb = placesDBHelper.getWritableDatabase();
// Get the SupportMapFragment and register for the callback
// when the map is ready for use.
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
markerHashMap = new LinkedHashMap<>();
//Set up location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider;
try {
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (SecurityException exception) {
exception.printStackTrace();
}
try {
mLatLong = new LatLng(location.getLatitude(), location.getLongitude());
} catch (Exception e) {
e.printStackTrace();
}
//Retrieve markers from db
mCursor = getAllPlaces();
retrieveMarkers(mCursor);
//Set up search bar for places
PlaceAutocompleteFragment placeAutoComplete = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete);
placeAutoComplete.setHint(getResources().getString(R.string.placeautocomplete_hint));
AutocompleteFilter filter = new AutocompleteFilter.Builder().setTypeFilter(5).build();
placeAutoComplete.setFilter(filter);
placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
//Retrieve information about selected place and zoom into it
mLatLong = place.getLatLng();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(mLatLong.latitude, mLatLong.longitude)) // Sets the center of the map to location user
.zoom(4) // Sets the zoom
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Log.d("Maps", "Place selected: " + place.getName());
//Retrieve location information from latitude and longitude
gcd = new Geocoder(MapsActivityRaw.this, Locale.getDefault());
try {
List<Address> address = gcd.getFromLocation(mLatLong.latitude, mLatLong.longitude, 1);
city = address.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
//Check if selected place was already added
mCursor = getAllPlaces();
retrieveMarkers(mCursor);
for (int i = 0; i < markerHashMap.size(); i++) {
MarkerOptions marker = (new ArrayList<>(markerHashMap.values()).get(i));
if (marker.getPosition().latitude == mLatLong.latitude && marker.getPosition().longitude == mLatLong.longitude) {
Snackbar duplicate = Snackbar.make(findViewById(R.id.drawer_layout), R.string.snackbar_duplicate, Snackbar.LENGTH_LONG);
View sb_duplicateView = duplicate.getView();
TextView tv_sb_duplicate = (TextView) sb_duplicateView.findViewById(android.support.design.R.id.snackbar_text);
tv_sb_duplicate.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
sb_duplicateView.setBackgroundColor(getColor(R.color.colorAccent));
duplicate.show();
return;
}
}
Snackbar placeAdded = Snackbar.make(findViewById(R.id.drawer_layout), R.string.snackbar_place_added, Snackbar.LENGTH_LONG);
View sb_placeAddedView = placeAdded.getView();
sb_placeAddedView.setBackgroundColor(getColor(R.color.colorPrimary));
TextView tv_sb_placeAdded = (TextView) sb_placeAddedView.findViewById(android.support.design.R.id.snackbar_text);
tv_sb_placeAdded.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
placeAdded.show();
MarkerOptions currentMarker = new MarkerOptions()
.position(mLatLong)
.title(city)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
mMap.addMarker(currentMarker);
//Add place into database
addNewPlace(city, mLatLong.latitude, mLatLong.longitude);
}
@Override
public void onError(Status status) {
Log.d("Maps", "An error occurred: " + status);
}
});
}
/**
* Manipulates the map when it's available.
* The API invokes this callback when the map is ready for use.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
//Initialize the map
mMap = googleMap;
//Setting default map rules
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setRotateGesturesEnabled(false);
//Disable Map Toolbar:
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return true;
}
});
//Configure custom infowindow through InfoWindowAdapter
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MapsActivityRaw.this);
mMap.setInfoWindowAdapter(adapter);
//Set onclicklistener for InfoWindow
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(MapsActivityRaw.this, MarkerActivity.class);
startActivity(intent);
}
});
//Set onclicklistener for Marker dragging to delete it
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
tv_delete.setVisibility(View.VISIBLE);
tv_delete.setTextColor(Color.BLACK);
}
@Override
public void onMarkerDrag(Marker marker) {
LatLng position = marker.getPosition();
Projection projection = mMap.getProjection();
Point screenPosition = projection.toScreenLocation(position);
int tv_delete_top_boundry = tv_delete.getTop();
if (screenPosition.y >= tv_delete_top_boundry) {
tv_delete.setTextColor(Color.RED);
} else {
tv_delete.setTextColor(Color.BLACK);
}
}
@Override
public void onMarkerDragEnd(Marker marker) {
String string_id = marker.getSnippet();
int id = Integer.parseInt(string_id);
LatLng position = marker.getPosition();
Projection projection = mMap.getProjection();
Point screenPosition = projection.toScreenLocation(position);
int tv_delete_top_boundry = tv_delete.getTop();
if (screenPosition.y >= tv_delete_top_boundry) {
boolean test = removeMarker(id);
marker.remove();
}
mMap.clear();
mCursor = getAllPlaces();
retrieveMarkers(mCursor);
setMarkers();
tv_delete.setVisibility(View.INVISIBLE);
}
});
//Set markers retrieved from database on now available map
setMarkers();
// Set the button for retrieving the current location and moving camera to it
try {
mMap.setMyLocationEnabled(true);
} catch (SecurityException e) {
e.printStackTrace();
}
//Style the map
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.map_style));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
// Position the map's camera
if (location != null) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(mLatLong) // Sets the center of the map to location user
.zoom(3) // Sets the zoom
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
// Database function for retrieving current database data in a cursor
private Cursor getAllPlaces() {
return mDb.query(
PlacesContract.PlacesEntry.TABLE_NAME,
null,
null,
null,
null,
null,
PlacesContract.PlacesEntry._ID
);
}
//Function to add new places into the database
private long addNewPlace(String name, double latitude, double longitude) {
ContentValues cv = new ContentValues();
cv.put(PlacesContract.PlacesEntry.COLUMN_CITY, name);
cv.put(PlacesContract.PlacesEntry.COLUMN_LATITUDE, latitude);
cv.put(PlacesContract.PlacesEntry.COLUMN_LONGITUDE, longitude);
return mDb.insert(PlacesContract.PlacesEntry.TABLE_NAME, null, cv);
}
//Remove marker from db
private boolean removeMarker(int id) {
return mDb.delete(PlacesContract.PlacesEntry.TABLE_NAME, PlacesContract.PlacesEntry._ID + "=" + id, null) > 0;
}
//Function to set all markers retrieved from database
private void setMarkers() {
ArrayList<MarkerOptions> markerOptionsList = new ArrayList<>(markerHashMap.values());
for (int i = 0; i < markerOptionsList.size(); i++) {
MarkerOptions option = markerOptionsList.get(i);
mMap.addMarker(option);
}
}
//Retrieve all markers from database
private void retrieveMarkers(Cursor cursor) {
markers.clear();
markerHashMap.clear();
cursor = getAllPlaces();
for (int i = 0; i < cursor.getCount(); i++) {
if (!cursor.moveToPosition(i)) {
Log.d("Cursor", "Cursor is empty");
} else {
String cCity = cursor.getString(cursor.getColumnIndex(PlacesContract.PlacesEntry.COLUMN_CITY));
double cLatitude = cursor.getDouble(cursor.getColumnIndex(PlacesContract.PlacesEntry.COLUMN_LATITUDE));
double cLongitude = cursor.getDouble(cursor.getColumnIndex(PlacesContract.PlacesEntry.COLUMN_LONGITUDE));
int cId = cursor.getInt(cursor.getColumnIndex(PlacesContract.PlacesEntry._ID));
LatLng cLatLng = new LatLng(cLatitude, cLongitude);
MarkerOptions cMarkerOptions = new MarkerOptions().title(cCity).snippet("" + cId).position(cLatLng).draggable(true).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
markerHashMap.put(cId, cMarkerOptions);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}