0
中獲取自定義視圖的屏幕中心我想將此代碼添加到我的項目Google Maps with Custom View Overlays中,但無法通過getScreenCenter()函數獲取。如何在android
活性,其中包括自定義視圖:
public class TempActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tempView);
//suppose that I have a map fragment initialise
getSearchRadius(); //this is the function where I am stuck;
}
/**
* @return distance in meters from center of circle to a point on the circle
*/
public double getSearchRadius() {
//Google Map LatLng objects must be converted to android Location objects
//in order to use distanceTo()
Point circlePoint = getScreenCenter();
circlePoint.x = circlePoint.x - getCircleRadius();
Point centerPoint = getScreenCenter();//how to get screenCenter
LatLng centerLatLng = map.getProjection().fromScreenLocation(centerPoint);
Location center = new Location("");
center.setLatitude(centerLatLng.latitude);
center.setLongitude(centerLatLng.longitude);
LatLng circleLatLng = map.getProjection().fromScreenLocation(circlePoint);
Location circlePointLocation = new Location("");
circlePointLocation.setLatitude(circleLatLng.latitude);
circlePointLocation.setLongitude(circleLatLng.longitude);
return center.distanceTo(circlePointLocation);
}
}
定製視圖:
public class DottedCircleView extends LinearLayout (due to exception I changed View to LinearLayout) {
int color = 0, radius = 0, width = 0, height = 0;
Paint p;
BitmapDrawable pin;
int pinXOffset =0 ,pinYOffset =0;
Context con;
public DottedCircleView(Context context)
{
super(context);
con = context;
}
public DottedCircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.DottedCircleView, 0, 0);
int color = a.getColor(R.styleable.DottedCircleView_circleColor, context.getResources().getColor(R.color.blue_btn_bg_color));
int radius = a.getInteger(R.styleable.DottedCircleView_radius, 0);
a.recycle();
setup();
}
public DottedCircleView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
width = View.MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
if (radius == 0) {
radius = Math.min(width, height)/2 - (int) p.getStrokeWidth();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width/2, height/2, radius, p);
//draw the map marker in middle of the circle
canvas.drawBitmap(pin.getBitmap(), (width/2) - pinXOffset, (height/2) - pinYOffset, null);
invalidate();
}
public void setup() {
p = new Paint();
p.setColor(getResources().getColor(color));
p.setStrokeWidth(10);
DashPathEffect dashPath = new DashPathEffect(new float[]{10.0f, 5.0f}, (float) 1.0);
p.setPathEffect(dashPath);
p.setStyle(Paint.Style.STROKE);
pin = (BitmapDrawable) getResources().getDrawable(R.drawable.boy_marker);
pinXOffset = pin.getIntrinsicWidth()/2;
pinYOffset = pin.getIntrinsicHeight();
}
}
Customview XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toast="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.support.android.designlibdemo.utils.DottedCircleView
android:id="@+id/dottedCircleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
toast:circleColor="@color/orange50"
toast:radius="10"/>
</LinearLayout>
請參閱此點circlePoint = getScreenCenter(); Point centerPoint = getScreenCenter(); circlePoint和centerPoint.Is它是圓和圓的中心點上的任何點嗎? – User42590
對不起,我看不懂你的評論,你能改說嗎? – fernandospr
請閱讀上面提到的文章中的這一段: fromScreenLocation(Point point)將返回屏幕點的緯度/經度,這正是我們所需要的。因此,我們只需獲取圓心的經緯度和圓上的一個點即可得到實際的半徑。 – User42590