【问题标题】:how to set a background on SurfaceView如何在 SurfaceView 上设置背景
【发布时间】:2013-12-30 14:38:38
【问题描述】:

这是我迄今为止尝试过的代码..

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>

我已经尝试了图像视图,但它不会显示为背景,因为我认为表面视图在它之上。

我还尝试将图像设置为背景,例如 android:background="@drawable/bg" 但仍然不显示..

我已经尝试了这个线程Setting image background in SurfaceView, getting black screen的解决方案 但仍然没有帮助我..

唯一出现的是我在 onDraw() 方法中显示的制造商。

【问题讨论】:

    标签: android xml bitmap android-canvas surfaceview


    【解决方案1】:

    在 onDraw() 中执行。但是您应该更早地从工厂创建此位图。不要在 onDraw() 中这样做。
    有一些技巧可以设置 SurfaceView 以使其成为on Top of all views behind。但是您应该了解需求并选择更合适的解决方案。

    【讨论】:

      【解决方案2】:

      也可以使用setFormat 设置在支架上,就像我为透明表面视图所做的那样:

      SurfaceHolder holder = getHolder();
      holder.addCallback(this);
      holder.setFormat(PixelFormat.OPAQUE);
      
      // set the compass background as transparent
      // holder.setFormat(PixelFormat.TRANSLUCENT);
      

      【讨论】:

        【解决方案3】:

        您必须做一些修改才能使 SurfaceView 中的任何后台都正常工作,如下所示:

        覆盖setBackgroundsetBackgroundDrawable 方法

           Drawable background
        
           @Override
           public void setBackgroundDrawable(Drawable background) {
              this.background = background;
           }
        
           @Override
           public void setBackground(Drawable background) {
              this.background = background;
           }
        

        不要忘记在drawable上调用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 背景。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-24
          • 2012-01-20
          • 2013-02-11
          • 1970-01-01
          • 2019-02-21
          • 1970-01-01
          • 2011-05-27
          • 1970-01-01
          相关资源
          最近更新 更多