Я потратил около 3 часов, пытаясь понять это, и кажется, что это так просто, но я не могу его обработать! Может кто-нибудь, пожалуйста, помогите мне! Все, что я хочу, чтобы он отображал изображение из папки доступных ресурсов. В нем говорится, что "невозможно преобразовать из Bitmap в Drawable".
package com.CS3040.Places;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.CS3040.*;
import com.CS3040.Coursework.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class PlaceOverlayItem extends OverlayItem {
private final GeoPoint point;
private final Place place;
private final Drawable marker;
//private final Context context;
public PlaceOverlayItem(Context context, Place p, String type) {
super(p.getGeoPoint(), p.getName(), p.getFormatted_address());
if(type.equals("restaurant"))
{
//this.marker =
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.restaurant);
this.marker = bmp;
}
//super.setMarker(this.marker);
this.point = p.getGeoPoint();
this.place = p;
}
/**
* @return the point
*/
public GeoPoint getPoint() {
return point;
}
/**
* @return the place
*/
public Place getPlace() {
return place;
}
}
Ну это правильно - Bitmap
не расширяет Drawable
. Я не занимался разработкой Android, но похоже, что вы хотите BitmapDrawable
:
Resources resources = context.getResources();
Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.restaurant);
this.marker = new BitmapDrawable(resources, bmp);
измените это:
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.restaurant);
this.marker = bmp;
к этому:
Drawable d = context.getResources().getDrawable(R.drawable.restaurant);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
this.marker = d;
Вы должны получить свой ресурс как Drawable:
if(type.equals("restaurant"))
{
this.marker = context.getResources().getDrawable(R.drawable.restaurant);
} else {
// marker would get no value without that else case - not allowed if you declare it final
this.marker = null;
}
if
положитьelse this.marker = null
поэтомуmarker
всегда будет получить какое - то значение