android怎么获取res——Drawable的图片数量

android怎么获取res——Drawable的图片数量,第1张

获取数量只需要一行代码:  int a = RdrawableclassgetDeclaredFields()length;  。

Rdrawableclass的意思是:获取文件的地址是 R文件下的drawable文件夹。

getDeclaredFields()获得某个类的所有申明的字段,即包括public、private和proteced,但是不包括父类的申明字段。通过这个方法就能获取drawable的对象。

length是用来统计getDeclaredFields()方法获得数量的。

使用ImageLoader

源码地址

>

起因是测试在测试过程中发现需要裁剪的看不到,但是还能继续裁剪。

所以就检查代码,发现了bitmap的width和Height有3000多。

然后试了下把改成300300的就显示正常,

所以我们需要让根据大小不同,机器不同而改变的宽高

//设置分辨率

//1获取系统分辨率

Resources resources = thisgetResources();

DisplayMetrics dm = resourcesgetDisplayMetrics();

//2获取分辨率

BitmapFactoryOptions options = new BitmapFactoryOptions();

optionsinJustDecodeBounds = true;// 这个方式不会在内存创建一张,

Bitmap bitmap = BitmapFactorydecodeFile(filePath, options); //此时返回的bitmap为null,但是option会保留一部分参数

//3确定分辨率

int height = optionsoutHeight;

int width = optionsoutWidth;

if(optionsoutHeight>dmheightPixels15f){//当大小比屏幕大15倍的时候,直接以系统高度为高度

height = dmheightPixels;

}

if (optionsoutWidth>dmwidthPixels15f){

width = dmwidthPixels;

}

optionsinJustDecodeBounds = false;

1、首先你有一个保存的路径 filePath

还有一个现在的链接 url

String filePath="";

String url="";

bytes[] b=new bytes[1024];

File file=new File(filePath);

FileOutputStream fs=new FileOutputStream(file);

InputStream in=new URL(url);

while(inread(b)){

  fswirte(b);

}

Bitmap bitmap=BitmmapFactorydecodeStream(in);

imageViewsetBitmapResource()

大概就是这样详细的你自己弄吧

法一:String path = "file:///android_asset/文件名";

法二:InputStream inputStream = getClass()getResourceAsStream("/assets/文件名");

以上就是关于android怎么获取res——Drawable的图片数量全部的内容,包括:android怎么获取res——Drawable的图片数量、Android实现图库功能,查看本应用drawable下的所有图片,且能左右滑动切换上一页或下一页图片、android 获取的图片尺寸比图片属性显示的尺寸大很多等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9310217.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存