2012-11-01 71 views
0

我試圖用這個顯示圖像轉換成SurfaceView

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 

bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
    imageFileUri), null, bmpFactoryOptions); 
chosenImageView.setImageBitmap(bmp); 

chosenImageViewImageView使用意圖

Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, 
     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(choosePictureIntent, 0); 

獲得從SD卡的圖像,但使用SurfaceView每當我試圖,它不給我.setImageBitmap

我知道如何通過imageView而不是Surface獲得圖像我覺得很難得到這個概念。

回答

0

隨着SurfaceView你應該創建一個靜態ArrayListVector來存儲您的Bitmap並繪製位圖onDraw

public class Panle extends SurfaceView implements SurfaceHolder.Callback { 
    private static ArrayList<Bitmap> mElements; 
    private CanvasThread canvasThread; 
    private Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 

    public Panle(Context context) { 
     super(context); 
     mElements = new ArrayList<Bitmap>(); 
     getHolder().addCallback(this); 
     canvasThread = new CanvasThread(getHolder(), this); 

     p.setXfermode(new PorterDuffXfermode(Mode.SRC)); 
     p.setColor(Color.TRANSPARENT); 
     p.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL)); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     if (canvas != null) 
      canvas.drawColor(0xffffffff); 
     if (mElements != null && mElements.size() != 0) { 

      for (int i = 0; i <= mElements.size() - 1; i++) { 

       canvas.drawBitmap(mElements.get(i), 50, 50, p);// For example i fixed position at 50 50. You can change to another position 

      } 

     } 

     super.draw(canvas); 
    } 
public static ArrayList<Bitmap> setmElements() { 
    return mElements; 
} 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     if (!canvasThread.isAlive()) { 
      canvasThread = new CanvasThread(getHolder(), this); 
      canvasThread.setRunning(true); 
      canvasThread.start(); 
     } 

    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     if (canvasThread.isAlive()) { 
      canvasThread.setRunning(false); 
     } 

    } 

} 

現在,我們需要處理的線程在我們的面板類。

public class CanvasThread extends Thread { 
    private SurfaceHolder _surfaceHolder; 
    private Panle _panel; 
    private boolean _run = false; 

    public CanvasThread(SurfaceHolder surfaceHolder, Panle panel) { 
     _surfaceHolder = surfaceHolder; 
     _panel = panel; 
    } 

    public void setRunning(boolean run) { 
     _run = run; 
    } 
    public SurfaceHolder getSurfaceHolder() { 
     return _surfaceHolder; 
    } 

    @Override 
    public void run() { 
     Canvas c; 
     while (_run) { 
      c = null; 
      try { 
       c = _surfaceHolder.lockCanvas(null); 
       synchronized (_surfaceHolder) { 

        _panel.onDraw(c); 
       } 
      } finally { 
       // do this in a finally so that if an exception is thrown 
       // during the above, we don't leave the Surface in an 
       // inconsistent state 
       if (c != null) { 
        _surfaceHolder.unlockCanvasAndPost(c); 
       } 
      } 
     } 
    } 
} 

在xml中創建面板並將其放入您的活動中。爲了使用Intent從SDcard中繪製圖像來打開Gallery,我使用它將檢索到的圖像放入onActivityForResult中。

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (resultCode == RESULT_OK) { 

      if (requestCode == Constants.REQUEST_CODE_GETTING_IMAGE) { 

       if (data != null) { 

        Uri dataUri = data.getData(); 
        Bitmap bitmap = null; 
        bitmap = Utils.decodeSampledBitmapFromFile(ImageUtil.getRealPathFromURI(CheDetailActivity.this, dataUri), Panel.getPanelW(), Panel.getPanelH());// I resize image from Gallery to avoid OOM 
        if (bitmap != null) { 


         Panel.getmElements().add(bitmap); 
        } 

       } 
      } 

     } 
     super.onActivityResult(requestCode, resultCode, data); 
    } 

您自由添加使用Panel.getElements.add(bitmap);
另一個教程中,你可以看到任何位圖是2D Tutorial Series這顯示出非常基本步驟。

+0

您給我的例子是使用啓動器圖標作爲表面上的圖像繪圖,我想要的是從用戶獲取圖片並將其顯示在表面上,因爲我要使用android效果媒體在圖像上。 – k0sh

+0

我的代碼可以自由添加任何位圖。我向你展示了從圖庫中挑選圖像的例子,並添加到'Panel' –

+0

謝謝你的幫助和時間,我真的很感謝你的幫助。歡呼 – k0sh