1.定义一个ImageData类如下:

package com.example.vidarecyclerviewstagger;public class ImageData {    private int resourceId;    private int height;    private int width;    public ImageData(int resourceId,int height,int width){        this.resourceId = resourceId;        this.height = height;        this.width = width;    }    public int getResourceId() {        return resourceId;    }    public void setResourceId(int resourceId) {        this.resourceId = resourceId;    }    public int getHeight() {        return height;    }    public void setHeight(int height) {        this.height = height;    }    public int getWidth() {        return width;    }    public void setWidth(int width) {        this.width = width;    }}

2.在drawable中放置一些图片:

private static int[] imgRes = new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,        R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,        R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,        R.drawable.img13,R.drawable.img14,R.drawable.img15,R.drawable.img16};

3.获取每个图片的高度宽度并保存数据到mData中:

private ArrayList<ImageData> mData = new ArrayList<>();public void InitData(){    for(int i = 0;i < imgRes.length;i++){        BitmapFactory.Options options = new BitmapFactory.Options();        BitmapFactory.decodeResource(getResources(),imgRes[i],options);        //获取图片的宽高        int height = options.outHeight;        int width = options.outWidth;        Log.i("YYYY","图片的宽度:"+width+"图片的高度:"+height);        ImageData imgData = new ImageData(imgRes[i],height,width);        mData.add(imgData);    }}

关键代码就这么几句:

BitmapFactory.Options options = new BitmapFactory.Options();BitmapFactory.decodeResource(getResources(),imgRes[i],options);//获取图片的宽高int height = options.outHeight;int width = options.outWidth;

更多相关文章

  1. Android中获取正在运行的应用程序-----ActivityManager.RunningA
  2. android 获取和设置屏幕亮度
  3. android获取当前Canvas位图代码
  4. Android(安卓)代码获取手机ip地址(个人笔记)
  5. android jetpack Navigation 获取当前激活的fragment
  6. 探讨android图片资源的抖动处理和格式转换
  7. Android(安卓)7+的一些报错
  8. (4.1.12)Android高效加载大图、多图解决方案,有效避免程序OOM
  9. 2010.12.16——— android listView 显示图片 内存溢出问题

随机推荐

  1. Mysql案例刨析事务隔离级别
  2. MySQL定时备份数据库(全库备份)的实现
  3. MySQL修改默认引擎和字符集详情
  4. Mysql树形结构的数据库表设计方案
  5. MySQL InnoDB 事务锁源码分析
  6. 浅谈MYSQL中树形结构表3种设计优劣分析与
  7. 浅谈mysql 树形结构表设计与优化
  8. Linux系统利用crontab定时备份Mysql数据
  9. MySQL中limit对查询语句性能的影响
  10. Mysql中关于Incorrect string value的解