自定义SurfaceView加载图片

想用surfaceView去加载一张静态图片。网上查了半天很多都是没有效果的。最后自己研究了一下。可以加载图片了。

package com.showly.autopollrecyclerviewdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.showly.autopollrecyclerviewdemo.R;

/**
 * author : huyawen
 * e-mail : 1754397982@qq.com
 * time   : 2022/01/06
 * function:
 */
public class ImageSurface extends SurfaceView {
    private static final String TAG = "ImageSurface";
    private SurfaceHolder surfaceHolder;
    int resId;
    float h;
    float w;

    public void setData(float h, float w, int resId) {
        this.h = h;
        this.w = w;
        this.resId = resId;
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(callback);
    }


    private SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            BitmapFactory.Options opts = new BitmapFactory.Options();
            Bitmap bitm = BitmapFactory.decodeResource(getResources(), R.drawable.bg, opts);
//				解析图片的头文件
            opts.inJustDecodeBounds = true;
//				得到图片高、宽
            float imageH = opts.outHeight;
            float imageW = opts.outWidth;
            System.out.println("图片的高" + imageH);
            System.out.println("屏幕的高" + h);
            Canvas canvas = holder.lockCanvas();
            PaintFlagsDrawFilter pfd= new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
            canvas.setDrawFilter(pfd);//解决缩放后图片字体模糊的问题
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
//				设置缩放比
            Matrix matrix = new Matrix();
            matrix.setScale(w / imageW, h / imageH);
            canvas.drawBitmap(bitmap, matrix, null);
//				解锁
            holder.unlockCanvasAndPost(canvas);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }
    };


    public ImageSurface(Context context) {
        this(context, null);
    }

    public ImageSurface(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageSurface(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

package com.showly.autopollrecyclerviewdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.showly.autopollrecyclerviewdemo.view.ImageSurface;

public class TestActivity extends AppCompatActivity {

    ImageSurface imageSurfaceView;
    private SurfaceView sf;
    private SurfaceHolder holder;
    float h;
    float w;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        imageSurfaceView = findViewById(R.id.imgvideo);
        sf = findViewById(R.id.sf);
        //得到屏幕管理器
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        //屏幕高、宽
        h = wm.getDefaultDisplay().getHeight();
        w = wm.getDefaultDisplay().getWidth();
        imageSurfaceView.setData(h, w, R.drawable.bg);
    }


}

将图片替换为自己的drawable里的图片。就可以加载出来。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

留白的云

感谢鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值