[英]Cannot convert Bitmap to Drawable, why do i get this error?
我花了大約3個小時試圖解決這個問題,它看起來很簡單,但是我似乎無法解決! 有人可以幫我! 我要做的就是顯示可繪制資源文件夾中的圖像。 它說“無法從位圖轉換為可繪制”。
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);
您需要將資源作為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;
}
改變這個:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.