我使用了旧指南,根据该指南,图标应出现在surfaceview中。
不幸的是,该应用程序显示除了图标之外的整个黑色表面视图。 我不 知道如何解决这个问题,因为android studio不会显示错误。 我应该添加或改变什么?
public class mySurfaceView extends SurfaceView {
private SurfaceHolder surfaceHolder;
private Bitmap bmpIcon;
public mySurfaceView(Context context) {
super(context);
init();
}
public mySurfaceView(Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public mySurfaceView(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
surfaceHolder = getHolder();
bmpIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
surfaceHolder.addCallback(new SurfaceHolder.Callback(){
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas(null);
drawSomething(canvas);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}});
}
protected void drawSomething(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if (bmpIcon != null){ canvas.drawBitmap(bmpIcon,
getWidth()/2, getHeight()/2, null);
}
}
}
我的图标
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF020000"
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>
答案 0 :(得分:0)
您在drawSomething
方法中使用了黑色,如果您更改背景颜色,则会改变颜色。
protected void drawSomething(Canvas canvas) {
//canvas.drawColor(Color.BLACK);
canvas.drawColor(Color.RED); // here i changed black to red(add you expected color here)
if (bmpIcon != null) {
canvas.drawBitmap(bmpIcon,
getWidth() / 2, getHeight() / 2, null);
}
}
由于您的drawble是vector drawable,因此请使用以下函数转换为位图
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
并像这样创建你的位图
bmpIcon = getBitmapFromVectorDrawable(getContext(), R.drawable.icon);
它会正常工作。
,你的最终代码将是这样的,
public class MySurface extends SurfaceView {
private SurfaceHolder surfaceHolder;
private Bitmap bmpIcon;
public MySurface(Context context) {
super(context);
init();
}
public MySurface(Context context,
AttributeSet attrs) {
super(context, attrs);
init();
}
public MySurface(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
private void init() {
surfaceHolder = getHolder();
bmpIcon = getBitmapFromVectorDrawable(getContext(), R.drawable.icon);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas(null);
drawSomething(canvas);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
});
}
protected void drawSomething(Canvas canvas) {
canvas.drawColor(Color.RED);
if (bmpIcon != null) {
canvas.drawBitmap(bmpIcon,
getWidth() / 2, getHeight() / 2, null);
}
}
}