Last active
November 23, 2020 15:25
-
-
Save pkliang/a48031e4d50cbcdbff7456e47257610b to your computer and use it in GitHub Desktop.
Android Custom View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<android.support.v7.widget.CardView | |
android:id="@+id/map_layout" | |
android:layout_width="wrap_content" | |
android:layout_height="@dimen/preview_map_height" | |
android:layout_marginLeft="@dimen/region_preview_map_cardview_margin" | |
android:layout_marginRight="@dimen/region_preview_map_cardview_margin" | |
app:cardCornerRadius="@dimen/region_preview_map_radius" > | |
<RelativeLayout | |
android:id="@+id/card_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:descendantFocusability="afterDescendants" > | |
<FrameLayout | |
android:id="@+id/map_fragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:paddingBottom="@dimen/northstar_padding_small" > | |
<com.schibsted.android.rocket.northstarui.components.mapchip.MapChip | |
android:id="@+id/location_text" | |
android:layout_width="match_parent" | |
android:layout_height="@dimen/northstar_chip_height" | |
android:gravity="center_horizontal" > | |
</com.schibsted.android.rocket.northstarui.components.mapchip.MapChip> | |
</LinearLayout> | |
</RelativeLayout> | |
</android.support.v7.widget.CardView> | |
<android.support.v7.widget.CardView | |
android:id="@+id/map_layout" | |
android:layout_width="wrap_content" | |
android:layout_height="@dimen/preview_map_height" | |
android:layout_marginLeft="@dimen/region_preview_map_cardview_margin" | |
android:layout_marginRight="@dimen/region_preview_map_cardview_margin" | |
app:cardCornerRadius="@dimen/region_preview_map_radius" > | |
<RelativeLayout | |
android:id="@+id/card_layout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:descendantFocusability="afterDescendants" > | |
<FrameLayout | |
android:id="@+id/map_fragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:paddingBottom="@dimen/northstar_padding_small" > | |
<com.schibsted.android.rocket.northstarui.components.mapchip.MapChip | |
android:id="@+id/location_text" | |
android:layout_width="match_parent" | |
android:layout_height="@dimen/northstar_chip_height" | |
android:gravity="center_horizontal" > | |
</com.schibsted.android.rocket.northstarui.components.mapchip.MapChip> | |
</LinearLayout> | |
</RelativeLayout> | |
</android.support.v7.widget.CardView> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomView extends LinearLayout implements OnMapReadyCallback { | |
SupportMapFragment mapFragment; | |
@BindView(R.id.location_text) MapChip locationText; | |
private GoogleMap googleMap; | |
private LocationWithPolygon locationWithPolygon; | |
private Polygon polygon; | |
public CustomView(Context context) { | |
super(context); | |
initLayout(context); | |
} | |
public CustomView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initLayout(context); | |
} | |
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
initLayout(context); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
private void initLayout(Context context) { | |
inflate(context, R.layout.custom_view, this); | |
ButterKnife.bind(this); | |
} | |
public void init(FragmentManager fragmentManager) { | |
if (fragmentManager == null) { | |
return; | |
} | |
mapFragment = SupportMapFragment.newInstance(); | |
fragmentManager.beginTransaction().replace(R.id.map_fragment, mapFragment).commit(); | |
mapFragment.getMapAsync(this); | |
} | |
private void initGoogleMap() { | |
googleMap.getUiSettings().setZoomControlsEnabled(false); | |
googleMap.getUiSettings().setAllGesturesEnabled(false); | |
if (mapFragment != null && mapFragment.getView() != null) { | |
mapFragment.getView().setClickable(false); | |
} | |
} | |
private void drawPolygon(List<LatLng> polygonCoordinates) { | |
if (googleMap == null) { | |
return; | |
} | |
if (polygon == null) { | |
polygon = googleMap.addPolygon(getPolygonOptions(polygonCoordinates)); | |
} | |
moveCamera(polygonCoordinates); | |
} | |
private PolygonOptions getPolygonOptions(List<LatLng> polygonPointsList) { | |
PolygonOptions opts = new PolygonOptions(); | |
for (LatLng location : polygonPointsList) { | |
opts.add(location); | |
opts = setOptions(opts); | |
} | |
return opts; | |
} | |
private void moveCamera(List<LatLng> polygonPointsList) { | |
LatLngBounds.Builder builder = new LatLngBounds.Builder(); | |
for (int i = 0; i < polygonPointsList.size(); i++) { | |
builder.include(polygonPointsList.get(i)); | |
} | |
LatLngBounds bounds = builder.build(); | |
int paddingInPixels = getResources().getDimensionPixelSize(R.dimen.map_polygon_padding); | |
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, paddingInPixels); | |
googleMap.setPadding(0, 0, 0, getResources().getDimensionPixelSize(R.dimen.detail_view_map_bottom_padding)); | |
googleMap.animateCamera(cu); | |
} | |
private PolygonOptions setOptions(PolygonOptions opts) { | |
int primaryColor = ResourcesCompat.getColor(getResources(), R.color.primaryNormal, null); | |
int colorWithAlpha = Color.argb(25, Color.red(primaryColor), Color.green(primaryColor), Color.blue(primaryColor)); | |
return opts.strokeWidth(getContext().getResources().getDimension(R.dimen.preview_map_circle_border_width)) | |
.strokeColor(ContextCompat.getColor(getContext(), R.color.primaryNormal)) | |
.fillColor(colorWithAlpha); | |
} | |
public void setLocationWithPolygon(LocationWithPolygon locationWithPolygonArg) { | |
this.locationWithPolygon = locationWithPolygonArg; | |
} | |
public void setLocationText(String address) { | |
locationText.setText(address); | |
} | |
@Override | |
public void onMapReady(GoogleMap googleMapArg) { | |
this.googleMap = googleMapArg; | |
initGoogleMap(); | |
updateMapWithRegionPolygon(); | |
} | |
private void updateMapWithRegionPolygon() { | |
if (locationWithPolygon != null) { | |
setLocationText(locationWithPolygon.getName() == null ? getContext().getString(R.string.no_address_available) | |
: locationWithPolygon.getName()); | |
List<LatLng> polygonCoordinates = locationWithPolygon.getPolygonCoordinates(); | |
if (polygonCoordinates != null) { | |
drawPolygon(polygonCoordinates); | |
} | |
} else { | |
if (polygon != null) { | |
polygon.remove(); | |
polygon = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment