使用SurfaceView来显示图片

最近在看图片浏览器的修改,贴下怎么用surfaceview 显示图片

package com.xxxxx.surfacephoto;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements
        SurfaceHolder.Callback {

    private static final String TAG = "SurfacePhoto";
    protected SurfaceView mSurfaceView;
    protected Bitmap bitmap = null;
    protected SurfaceHolder mSurfaceHolder = null;
    MediaPlayer mediaPlayer = new MediaPlayer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSurfaceView = findViewById(R.id.surfaceView);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();

    }

    protected void initBitmap() {
        String path = "/storage/417C-9D05/Picture/BMP/2K/1.bmp";
        BitmapFactory.Options options = new BitmapFactory.Options();
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            FileDescriptor fd = fileInputStream.getFD();
            if (fd == null) {
                Log.e(TAG, "fd null");
                return ;
            }
            bitmap = BitmapFactory.decodeFileDescriptor(fd, null, null);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    protected void drawImage() {
        Log.d(TAG, "drawImage...");
        Canvas canvas = mSurfaceHolder.lockCanvas(null);
        if (bitmap != null && canvas != null) {
            if (canvas != null) {
                canvas.drawColor(Color.WHITE);
            }
            Paint paint = new Paint();
            android.graphics.Rect src = new android.graphics.Rect();
            android.graphics.Rect dst = new android.graphics.Rect();
            src.left = 0;
            src.top = 0;
            src.bottom = bitmap.getHeight();
            src.right = bitmap.getWidth();
            dst.left = 0;
            dst.top = 0;
            dst.bottom = mSurfaceView.getHeight();
            dst.right = mSurfaceView.getWidth();

            canvas.drawBitmap(bitmap, src, dst, paint);
            Log.d(TAG, "canvas drawImage done " + "src:" + src.bottom + "x"+ src.right);
            Log.d(TAG, "canvas drawImage done " + "dst:" + dst.bottom + "x"+ dst.right);
        } else {
            Log.w(TAG, "bitmap:" + bitmap + ", canvas:" + canvas);
        }

        if (canvas != null) {
            mSurfaceHolder.unlockCanvasAndPost(canvas);
//            getHolder().unlockCanvasAndPost(canvas);
            Log.d(TAG, "canvas post done");
        }
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated");
        drawImage();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG, "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG, "surfaceDestroyed");
        }
    }

}

你可能感兴趣的:(使用SurfaceView来显示图片)