这是我到目前为止尝试的代码..
package com.gtxradeon.brands;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasthread;
public Paint paint;
private Bitmap scaled;
public Panel(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
setFocusable(true);
paint = new Paint();
}
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
setFocusable(true);
}
@Override
public void onDraw(Canvas canvas) {
//paint.setARGB(255, 255, 0, 0);
// canvas.drawColor(Color.BLACK);
// canvas.drawCircle(500f, 500f, 30, paint);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.mapmarker);
canvas.drawBitmap(bmp , 250f, 250f, null);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.grocery);
// float scale = (float) background.getHeight() / (float) getHeight();
// int newWidth = Math.round(background.getWidth() / scale);
// int newHeight = Math.round(background.getHeight() / scale);
// scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
canvasthread = new CanvasThread(getHolder(), this);
canvasthread.setRunning(true);
canvasthread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
canvasthread.setRunning(false);
while (retry) {
try {
canvasthread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
这是xml文件
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/grocery" /> -->
<com.gtxradeon.brands.Panel
android:id="@+id/SurfaceView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
我已尝试过图像视图,但它不会显示为背景,因为我认为surfaceview位于它之上。
我也尝试将图片设置为android:background="@drawable/bg"
之类的背景
但仍然没有显示..
我尝试过这个帖子的解决方案Setting image background in SurfaceView, getting black screen 但仍然没有帮助我..
唯一出现的是我在onDraw()方法中显示的制作者。
答案 0 :(得分:1)
在onDraw()中执行此操作。但是你应该先从工厂创建这个位图。不要在onDraw()中执行此操作 设置SurfaceView以使其on Top of all views behind有一些技巧。但是你应该了解要求并选择更合适的解决方案。
答案 1 :(得分:1)
还可以使用setFormat
设置持有者,就像我为透明表面视图所做的那样:
SurfaceHolder holder = getHolder();
holder.addCallback(this);
holder.setFormat(PixelFormat.OPAQUE);
// set the compass background as transparent
// holder.setFormat(PixelFormat.TRANSLUCENT);
答案 2 :(得分:1)
你需要做一些小问题才能在SurfaceView中使用任何后台工作,如下所示:
覆盖setBackground
和setBackgroundDrawable
方法
Drawable background
@Override
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
@Override
public void setBackground(Drawable background) {
this.background = background;
}
不要忘记在可绘制的上拨打setBounds
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
matrix = null;
if (background != null)
background.setBounds(left, top, right, bottom);
}
然后在代码中你锁定,绘制,解锁和发布你添加背景的画布
Canvas c = holder.lockCanvas();
if (background != null)
background.draw(c);
// do your stuff here
holder.unlockCanvasAndPost(c);
适用于XML和Java背景。