ItemizedOverlay旋转标记和崩溃 [英] ItemizedOverlay Rotate marker and crash
问题描述
一个陌生的岗位作为该位是我贴过,认为这是解决了,但都没有!
我有一个情况我需要使用一个自定义的可绘制每个标记,我需要的效果是旋转标志由度一组号码,我可能完全要对这个错误的方式。
作为一个开始,我使用的优秀素材,因为这不我补充后的工作,只休息!
https://github.com/commonsguy/cw-advandroid/树/主/地图/ NooYawkAsync
在previous岗位和code是这里
<一个href="http://stackoverflow.com/questions/4187012/android-maps-nullpointerexception-itemizedoverlay">Android地图NullPointerException异常ItemizedOverlay
如果任何人都可以推荐一个更好,更稳定的方式来获得一个旋转绘制对象,把我在正确的道路,将是巨大的。
正如你可以从我的code看我不设覆盖中绘制对象,并在其他对象做到这一点之外,怀疑这就是为什么它被打破!
目前,我做这一套我可绘制...
市民可绘制getRotatedIcon(字符串字符串){
绘制对象标记= NULL;
双tempHeading = Double.valueOf(串);
长intHeading = Math.round(tempHeading / 10);
INT渣油= getResources()。则getIdentifier(
icon_rotate_+ Long.toString(intHeading),绘制,
com.test.testapp);
。标记= getResources()getDrawable(渣油);
marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
返回标记;
}
和再创造这一切我用这个...
如果(网站!= NULL){
sites.clearItems();
}其他{
网站=新SitesOverlay();
}
的for(int i = 0; I&LT; getMainOverlayArray()的大小();我++){
。tempOver = getMainOverlayArray()获得(一);
tempOver.setMarker(getRotatedIcon(tempOver.getcourse()));
如果(tempOver!= NULL){
sites.addItem(tempOver);
}
}
sites.finishedLoading();
和我SitesOverlay类...
私有类SitesOverlay扩展ItemizedOverlay&LT; pfOverlayItem&GT; {
私人列表&LT; pfOverlayItem&GT;项目=新的ArrayList&LT; pfOverlayItem&GT;();
私人PopupPanel面板=新PopupPanel(R.layout.popup);
私人MapLocation selectedMapLocation;
私有静态最终诠释CIRCLERADIUS = 2;
私人的ArrayList&LT; pfOverlayItem&GT; mOverlays =新的ArrayList&LT; pfOverlayItem&GT;();
公共SitesOverlay(){
超(空);
填充();
}
公共无效finishedLoading(){
填充();
}
@覆盖
保护pfOverlayItem createItem中(int i)以{
返程(items.get(一));
}
公共无效的addItem(OverlayItem叠加){
overlay.setMarker(boundCenter(overlay.getMarker(0)));
items.add((pfOverlayItem)叠加);
}
公共无效clearItems(){
runOnUiThread(新的Runnable(){
公共无效的run(){
items.clear();
myMapView.invalidate();
}
});
}
公共无效清除(){
mOverlays.clear();
myMapView.removeAllViews();
setLastFocusedIndex(-1);
填充();
}
@覆盖
公共无效画(油画画布,图形页面图形页面,布尔影子){
super.draw(帆布,图形页面,假);
尝试 {
如果(getMainOverlayArray()。大小()!= 0){
pfOverlayItem tempOver = NULL;
的for(int i = 0; I&LT; getMainOverlayArray()的大小();我++){
。tempOver = getMainOverlayArray()获得(一);
布尔isMatch = FALSE;
//Log.i("Selected名称,selectedName);
如果(tempOver.getTitle()。等于(selectedName))
{
isMatch = TRUE;
}
如果(isMatch){
投影投影= MapView.getProjection()在;
点对点=新的点();
projection.toPixels(tempOver.getPoint(),点);
油漆背景=新的油漆();
background.setColor(Color.WHITE);
background.setAlpha(150);
RectF RECT =新RectF();
rect.set(point.x - 50,point.y +15,
point.x + 90,+ point.y 50);
canvas.drawRoundRect(矩形,5,5,背景);
漆文=新的油漆();
text.setAntiAlias(真正的);
text.setColor(Color.BLUE);
text.setTextSize(14);
text.setTypeface(Typeface.MONOSPACE);
canvas.drawText(tempOver.getTitle()++ tempOver.getcallsign(),point.x -50,point.y + 30,文本);
canvas.drawText(tempOver.getdestination()++ tempOver.getdraft(),point.x -50,point.y + 45,文本);
}
}
}
}赶上(例外五){
Log.e(错误,问题工程视图,E);
e.printStackTrace();
}
}
@覆盖
受保护的布尔中的onTap(int i)以{
pfOverlayItem项目=的getItem(我);
如果(selectedName.equals(item.getTitle())){
尝试{
意图myIntent =新的意图(TestApp.this,DetailActivity.class);
myIntent.putExtra(INT,我);
myIntent.putExtra(字符串,selectedName);
PlaneFinderMain.this.startActivity(myIntent);
}赶上(例外五){
Log.e(错误,无法启动,E);
}
}
currentadshex = item.getmmsi();
新GetRouteTask(item.getmmsi())执行()。
将selectedItem =我;
selectedName = item.getTitle();
selectedPlanePoint = item.getPoint();
的GeoPoint地理= item.getPoint();
点PT = myMapView.getProjection()toPixels(GEO,空)。
查看查看= panel.getView();
((TextView的)view.findViewById(R.id.reg))的setText(item.getTitle());
((TextView的)view.findViewById(R.id.callsign))的setText(item.getcallsign());
((TextView的)view.findViewById(R.id.dest))的setText(item.getdestination());
((TextView的)view.findViewById(R.id.draft))的setText(item.getdraft());
返回(真);
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面){
如果(event.getAction()== MotionEvent.ACTION_DOWN){
如果(selectedPlanePoint!= NULL){
投影投影= MapView.getProjection()在;
点对点=新的点();
projection.toPixels(selectedPlanePoint,点);
点pointHit =新的点();
pointHit.x =(int)的event.getX();
pointHit.y =(int)的event.getY();
如果((point.x - pointHit.x)&GT; -100安培;及(point.x - pointHit.x)&LT; 70安培;及(point.y - pointHit.y)&LT; -25安培;及(point.y - pointHit.y)&GT; -95){
尝试{
意图myIntent =新的意图(TestApp.this,DetailActivity.class);
myIntent.putExtra(INT,则selectedItem);
myIntent.putExtra(字符串,selectedName);
TestApp.this.startActivity(myIntent);
}赶上(例外五){
Log.e(错误,无法启动,E);
}
}其他{
}
}
}
返回false;
}
@覆盖
公众诠释大小(){
返程(items.size());
}
公共无效addOverlay(OverlayItem O){
setLastFocusedIndex(-1);
填充();
}
}
这似乎是自然的API的选择, RotateDrawable
,是新空房禁地里你只能从充气使用布局。我已经适应了马克·墨菲code。在这个问题中引用到下面的活动,显示了纽约标志纺纱,使用替代RotateDrawable它包装一个包含绘制以更灵活的方式:
进口android.graphics.Canvas;
进口android.graphics.ColorFilter;
进口android.graphics.Rect;
进口android.graphics.drawable.Drawable;
进口android.os.Bundle;
进口android.os.Handler;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapView;
进口com.google.android.maps.MyLocationOverlay;
进口com.google.android.maps.OverlayItem;
进口的java.util.ArrayList;
进口的java.util.List;
公共类NooYawk扩展MapActivity {
私人MapView的地图= NULL;
私人MyLocationOverlay我= NULL;
私人SitesOverlay网站= NULL;
处理程序处理程序=新的处理程序();
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
地图=(图形页面)findViewById(R.id.map);
map.getController()setCenter(的GetPoint(40.76793169992044,-73.98180484771729))。
map.getController()setZoom(17)。
map.setBuiltInZoomControls(真正的);
我=新MyLocationOverlay(本,地图);
map.getOverlays()加(我)。
网站=新SitesOverlay();
。map.getOverlays()加(网站);
map.invalidate();
}
@覆盖
公共无效onResume(){
super.onResume();
me.enableCompass();
}
@覆盖
公共无效的onPause(){
super.onPause();
me.disableCompass();
}
@覆盖
保护的布尔isRouteDisplayed(){
返回(假);
}
私人的GeoPoint的GetPoint(双纬度,双LON){
返回(新的GeoPoint((int)的(纬度* 1000000.0),(int)的(LON * 1000000.0)));
}
私有类SitesOverlay扩展ItemizedOverlay&LT; CustomItem的&GT; {
私人列表&LT; CustomItem的&GT;项目=新的ArrayList&LT; CustomItem的&GT;();
公共SitesOverlay(){
超(空);
items.add(新的CustomItem(的GetPoint(40.748963847316034,-73.96807193756104)
联合国,联合国,getMarker(R.drawable.blue_full_marker)));
items.add(新的CustomItem(的GetPoint(40.76866299974387,-73.98268461227417)
林肯中心,家爵士乐在林肯中心,getMarker(R.drawable.orange_full_marker)));
items.add(新的CustomItem(的GetPoint(40.765136435316755,-73.97989511489868)
卡内基音乐厅,你在哪里去练习,练习,再练习,getMarker(R.drawable.green_full_marker)));
items.add(新的CustomItem(的GetPoint(40.70686417491799,-74.01572942733765),市中心的俱乐部,
原来的家海斯曼杯的,getMarker(R.drawable.purple_full_marker)));
填充();
}
@覆盖
受保护的CustomItem createItem中(int i)以{
返程(items.get(一));
}
@覆盖
公共无效画(油画画布,图形页面图形页面,布尔影子){
super.draw(帆布,图形页面,阴影);
handler.post(新的Runnable(){
@覆盖
公共无效的run(){
对于(CustomItem的项目:项目){
item.marker.rotate();
}
map.invalidate();
}
});
}
@覆盖
公众诠释大小(){
返程(items.size());
}
私人绘制对象getMarker(INT资源){
绘制对象标记= getResources()getDrawable(资源)。
marker.setBounds(0,0,marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
boundCenter(标记);
返回(标记);
}
}
//
//一个RotateDrawable不是新空房禁地。
//
私有类RotateDrawable扩展绘制对象{
私人绘制对象mDrawable;
私人浮动mPivotX = 0.5F;
私人浮动mPivotY = 0.5F;
私人浮动mCurrentDegrees = 0F;
公共RotateDrawable(绘制对象绘制){
this.mDrawable =绘制;
}
公共无效旋转(){
mCurrentDegrees + = 5;
如果(mCurrentDegrees&GT; = 360){
mCurrentDegrees- = 360;
}
}
公共无效画(油画画布){
INT SAVECOUNT = canvas.save();
矩形边界= mDrawable.getBounds();
INT W = bounds.right - bounds.left;
INT H = bounds.bottom - bounds.top;
浮动PX = W * mPivotX;
浮PY = H * mPivotY;
canvas.rotate(mCurrentDegrees,0,0);
mDrawable.draw(画布);
canvas.restoreToCount(SAVECOUNT);
}
@覆盖公众诠释getOpacity(){
返回mDrawable.getOpacity();
}
@覆盖
公共无效setAlpha(INT为arg0){
mDrawable.setAlpha(为arg0);
}
@覆盖
公共无效setColorFilter(ColorFilter为arg0){
mDrawable.setColorFilter(为arg0);
}
}
类CustomItem的扩展OverlayItem {
公共RotateDrawable标记= NULL;
CustomItem的(PT的GeoPoint,字符串名称,字符串片段,可绘制标记){
超(PT,名称,代码段);
this.marker =新RotateDrawable(标记);
}
@覆盖
公众可绘制getMarker(INT stateBitset){
可绘制的结果=(标记);
的setState(结果,stateBitset);
返回(结果);
}
}
}
有任何问题,请随时问。 : - )
Bit of a strange post as this is something I posted before, thought it was solved but didn't!
I have a situation where I need to use a custom Drawable for each marker, the effect I need is to rotate the marker by a set number of degrees and am probably going the wrong way about this completely.
As a start I am using the excellent Creative Commons as this does work and only breaks after my additions!
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/NooYawkAsync
The previous post and the code is here
Android Maps NullPointerException ItemizedOverlay
If anyone can recommend a better and more stable way to get a rotated Drawable and put me on the right path that would be great.
As you can see from my code I don't set the Drawable in the overlay and do this outside within another object and suspect this is why it is breaking!
At the moment I set my Drawable by doing this...
public Drawable getRotatedIcon(String string) {
Drawable marker = null;
Double tempHeading = Double.valueOf(string);
long intHeading = Math.round(tempHeading / 10);
int resID = getResources().getIdentifier(
"icon_rotate_" + Long.toString(intHeading), "drawable",
"com.test.testapp");
marker = getResources().getDrawable(resID);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
return marker;
}
And then to create it all I use this...
if (sites != null){
sites.clearItems();
}else{
sites = new SitesOverlay();
}
for (int i = 0; i < getMainOverlayArray().size(); i++) {
tempOver = getMainOverlayArray().get(i);
tempOver.setMarker(getRotatedIcon(tempOver.getcourse()));
if (tempOver != null){
sites.addItem(tempOver);
}
}
sites.finishedLoading();
And my SitesOverlay class...
private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> {
private List<pfOverlayItem> items = new ArrayList<pfOverlayItem>();
private PopupPanel panel=new PopupPanel(R.layout.popup);
private MapLocation selectedMapLocation;
private static final int CIRCLERADIUS = 2;
private ArrayList<pfOverlayItem> mOverlays = new ArrayList<pfOverlayItem>();
public SitesOverlay() {
super(null);
populate();
}
public void finishedLoading(){
populate();
}
@Override
protected pfOverlayItem createItem(int i) {
return (items.get(i));
}
public void addItem(OverlayItem overlay) {
overlay.setMarker(boundCenter(overlay.getMarker(0)));
items.add((pfOverlayItem) overlay);
}
public void clearItems(){
runOnUiThread(new Runnable() {
public void run() {
items.clear();
myMapView.invalidate();
}
});
}
public void clear() {
mOverlays.clear();
myMapView.removeAllViews();
setLastFocusedIndex(-1);
populate();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, false);
try {
if (getMainOverlayArray().size() != 0){
pfOverlayItem tempOver = null;
for (int i = 0; i < getMainOverlayArray().size(); i++) {
tempOver = getMainOverlayArray().get(i);
boolean isMatch = false;
//Log.i("Selected Name",selectedName);
if (tempOver.getTitle().equals(selectedName))
{
isMatch = true;
}
if (isMatch){
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(tempOver.getPoint(), point);
Paint background = new Paint();
background.setColor(Color.WHITE);
background.setAlpha(150);
RectF rect = new RectF();
rect.set(point.x - 50, point.y +15,
point.x + 90, point.y + 50);
canvas.drawRoundRect(rect, 5, 5, background);
Paint text = new Paint();
text.setAntiAlias(true);
text.setColor(Color.BLUE);
text.setTextSize(14);
text.setTypeface(Typeface.MONOSPACE);
canvas.drawText(tempOver.getTitle() + " " + tempOver.getcallsign(), point.x -50 , point.y + 30, text);
canvas.drawText(tempOver.getdestination() + " " + tempOver.getdraft(), point.x -50 , point.y + 45, text);
}
}
}
}catch (Exception e){
Log.e("Error", "Problem drawing view", e);
e.printStackTrace();
}
}
@Override
protected boolean onTap(int i) {
pfOverlayItem item = getItem(i);
if (selectedName.equals(item.getTitle())){
try{
Intent myIntent = new Intent(TestApp.this, DetailActivity.class);
myIntent.putExtra( "int", i);
myIntent.putExtra( "string", selectedName );
PlaneFinderMain.this.startActivity(myIntent);
}catch (Exception e){
Log.e("Error", "Cannot launch", e);
}
}
currentadshex = item.getmmsi();
new GetRouteTask(item.getmmsi()).execute();
selectedItem = i;
selectedName = item.getTitle();
selectedPlanePoint = item.getPoint();
GeoPoint geo=item.getPoint();
Point pt=myMapView.getProjection().toPixels(geo, null);
View view=panel.getView();
((TextView)view.findViewById(R.id.reg)).setText(item.getTitle());
((TextView)view.findViewById(R.id.callsign)).setText(item.getcallsign());
((TextView)view.findViewById(R.id.dest)).setText(item.getdestination());
((TextView)view.findViewById(R.id.draft)).setText(item.getdraft());
return (true);
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if (selectedPlanePoint != null){
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(selectedPlanePoint, point);
Point pointHit = new Point();
pointHit.x=(int)event.getX();
pointHit.y=(int)event.getY();
if ((point.x - pointHit.x) >-100 && (point.x - pointHit.x) <70 && (point.y - pointHit.y) < -25 && (point.y - pointHit.y) > -95){
try{
Intent myIntent = new Intent(TestApp.this, DetailActivity.class);
myIntent.putExtra( "int", selectedItem);
myIntent.putExtra( "string", selectedName );
TestApp.this.startActivity(myIntent);
}catch (Exception e){
Log.e("Error", "Cannot launch", e);
}
}else{
}
}
}
return false;
}
@Override
public int size() {
return (items.size());
}
public void addOverlay(OverlayItem o){
setLastFocusedIndex(-1);
populate();
}
}
It seems the natural API choice, RotateDrawable
, is braindead in that you can only use it from inflated layout. I have adapted Mark Murphy's code referenced in the question into the below activity which shows the New York markers spinning, using an alternative RotateDrawable which wraps a contained drawable in a more flexible way:
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
import java.util.List;
public class NooYawk extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
private SitesOverlay sites=null;
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map=(MapView)findViewById(R.id.map);
map.getController().setCenter(getPoint(40.76793169992044, -73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
sites=new SitesOverlay();
map.getOverlays().add(sites);
map.invalidate();
}
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
@Override
protected boolean isRouteDisplayed() {
return(false);
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0), (int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private List<CustomItem> items=new ArrayList<CustomItem>();
public SitesOverlay() {
super(null);
items.add(new CustomItem(getPoint(40.748963847316034, -73.96807193756104),
"UN", "United Nations",getMarker(R.drawable.blue_full_marker)));
items.add(new CustomItem(getPoint(40.76866299974387, -73.98268461227417),
"Lincoln Center", "Home of Jazz at Lincoln Center", getMarker(R.drawable.orange_full_marker)));
items.add(new CustomItem(getPoint(40.765136435316755, -73.97989511489868),
"Carnegie Hall", "Where you go with practice, practice, practice", getMarker(R.drawable.green_full_marker)));
items.add(new CustomItem(getPoint(40.70686417491799,-74.01572942733765), "The Downtown Club",
"Original home of the Heisman Trophy", getMarker(R.drawable.purple_full_marker)));
populate();
}
@Override
protected CustomItem createItem(int i) {
return(items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
handler.post(new Runnable() {
@Override
public void run() {
for (CustomItem item : items) {
item.marker.rotate();
}
map.invalidate();
}
});
}
@Override
public int size() {
return(items.size());
}
private Drawable getMarker(int resource) {
Drawable marker=getResources().getDrawable(resource);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
boundCenter(marker);
return(marker);
}
}
//
// A RotateDrawable that isn't braindead.
//
private class RotateDrawable extends Drawable {
private Drawable mDrawable;
private float mPivotX = 0.5f;
private float mPivotY = 0.5f;
private float mCurrentDegrees = 0f;
public RotateDrawable(Drawable drawable) {
this.mDrawable = drawable;
}
public void rotate() {
mCurrentDegrees+=5;
if (mCurrentDegrees>=360) {
mCurrentDegrees-=360;
}
}
public void draw(Canvas canvas) {
int saveCount = canvas.save();
Rect bounds = mDrawable.getBounds();
int w = bounds.right - bounds.left;
int h = bounds.bottom - bounds.top;
float px = w * mPivotX;
float py = h * mPivotY;
canvas.rotate(mCurrentDegrees, 0, 0);
mDrawable.draw(canvas);
canvas.restoreToCount(saveCount);
}
@Override public int getOpacity() {
return mDrawable.getOpacity();
}
@Override
public void setAlpha(int arg0) {
mDrawable.setAlpha(arg0);
}
@Override
public void setColorFilter(ColorFilter arg0) {
mDrawable.setColorFilter(arg0);
}
}
class CustomItem extends OverlayItem {
public RotateDrawable marker=null;
CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
super(pt, name, snippet);
this.marker=new RotateDrawable(marker);
}
@Override
public Drawable getMarker(int stateBitset) {
Drawable result=(marker);
setState(result, stateBitset);
return(result);
}
}
}
Any questions, feel free to ask. :-)
这篇关于ItemizedOverlay旋转标记和崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!