zoukankan      html  css  js  c++  java
  • Android获得图片资源的三种方式

     

    、        使用BitmapFactory解析图片

     // --> 使用BitmapFactory解析图片
    public void myUseBitmapFactory(Canvas canvas){
    // 定义画笔
       Paint paint = new Paint();
    // 获取资源流
       Resources rec = getResources();
       InputStream in = rec.openRawResource(R.drawable.haha);
    // 设置图片
       Bitmap bitmap =BitmapFactory.decodeStream(in);
    // 绘制图片
       canvas.drawBitmap(bitmap, 0,20, paint);         
    }

    、        使用BitmapDrawable解析图片

    // --> 使用BitmapDrawable解析图片
        public void myUseBitmapDrawable(Canvas canvas){
        // 定义画笔
           Paint paint = new Paint();
        // 获得资源
           Resources rec = getResources();
        // BitmapDrawable
           BitmapDrawable bitmapDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.haha);
        // 得到Bitmap
           Bitmap bitmap = bitmapDrawable.getBitmap();
        // 在画板上绘制图片
           canvas.drawBitmap(bitmap, 20,120,paint);
        }

    三、        使用InputStream和BitmapDrawable绘制

    // --> 使用InputStream和BitmapDrawable解析图片
        public void myUseInputStreamandBitmapDrawable(Canvas canvas){
        // 定义画笔
           Paint paint = new Paint();
        // 获得资源
           Resources rec = getResources();
        // InputStream得到资源流
           InputStream in = rec.openRawResource(R.drawable.haha);
        // BitmapDrawable 解析数据流
           BitmapDrawable bitmapDrawable =  new BitmapDrawable(in);
        // 得到图片
           Bitmap bitmap = bitmapDrawable.getBitmap();
        // 绘制图片
           canvas.drawBitmap(bitmap, 100, 100,paint);
        }
  • 相关阅读:
    djangoadmin实现文件上传下载
    Apscheduler详解(转)
    django集成Apscheduler3
    springboot实现token鉴权
    xss攻击入门
    回顾2012——运维工作周年祭
    9个常用iptables配置实例
    ardunio 实验:超声波测距、声光报警模拟倒车雷达
    一款我用了好多年的多线程FTP软件
    线程Thread基础学习(2)
  • 原文地址:https://www.cnblogs.com/riaol/p/2343210.html
Copyright © 2011-2022 走看看