android imageview怎么把imageview 转为Bitmap

10:40 提问
如何把一个图片转换成位图?
我想要设置一个图片成为手机的壁纸,但是所有的壁纸功能都只接受位图。我不能使用WallpaperManager 因为我的系统是2.1之前的。
而且我的壁纸是从网上下载的,不在R.drawable里边。
按赞数排序
这些代码可能有用
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
这是一个图像是下载的版本
String name = c.getString(str_url);
URL url_value = new URL(name);
ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon);
if (profile != null) {
Bitmap mIcon1 =
BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
profile.setImageBitmap(mIcon1);
从网上下载的图片你也可以转化为位图,如下:
Drawable drawable =
drawable = Drawable.createFromStream(new URL(imgUrl).openStream(), "图片名称");
希望对你有帮助。
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawableinstanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
Bitmap bitmap= Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas= new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
用扫描仪扫描出来的图片就成了位图
其他相关推荐
其他相似问题34209人阅读
Android开发(1048)
今天做一个应用,新增一笔记录到sqlite中去,
记录中有一个字段是一个图像,在新增界面中我用的是imageview控件,点击弹出一个对话框,列出可供选择的图像,单击选择,但是提交后图像显示不正确,只显示一个黑框,我的代码是这样的
Bitmap image = Bitmap.createBitmap(imageView.getDrawable(). .....); 从imageview的Drawable里取出信息构造一个bitmap,事实证明是行不通的。
Bitmap image = Bitmap.createBitmap(imageView.getDrawable().
从imageview的Drawable里取出信息构造一个bitmap,事实证明是行不通的。
搜索了一下怎么把ImageView转换成Bitmap,发现网上提供一种方法:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
我试了一下,成功是成功了,但是得到的bitmap大小不对,原本很大的,放到列表里显示得超小,不是我想要的结果。
后来试了一下:
Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
成功搞定。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9755712次
积分:71732
积分:71732
排名:第23名
原创:505篇
转载:916篇
评论:2709条
如果您认为本博客不错,读后觉得有收获,不妨打赏赞助我一下,让我有动力继续写出高质量的博客。
赠人玫瑰,手有余香。分享技术,传递快乐。
QQ交流群:
有心课堂会员,请加入VIP QQ交流群:
文章:28篇
阅读:100528
文章:69篇
阅读:483873posts - 89,&
comments - 2,&
trackbacks - 0
&public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) { &&&&&if (bytes != null) &&&&&&&&if (opts != null)&&&&&&&&&&&&&&return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,&&opts);&&&&&&&&&else &&&&&&&&&&&&return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);&&&&&&return null; }这里我们主要来介绍一个BitmapFactory.Options这个类&&&&BitmapFactory.Options这个类的详解如下:public BitmapinBitmapIf set, decode methods that take the Options object will attempt to reuse this bitmap when loading content.public intinDensityThe pixel density to use for the bitmap.public booleaninDitherIf dither is true, the decoder will attempt to dither the decoded image.public booleaninInputShareableThis field works in conjuction with inPurgeable.public booleaninJustDecodeBoundsIf set to true, the decoder will return null (no bitmap), but the out…public booleaninMutableIf set, decode methods will always return a mutable Bitmap instead of an immutable one.public booleaninPreferQualityOverSpeedIf inPreferQualityOverSpeed is set to true, the decoder will try to decode the reconstructed image to a higher quality even at the expense of the decoding speed.publicBitmap.ConfiginPreferredConfigIf this is non-null, the decoder will try to decode into this internal configuration.public booleaninPurgeableIf this is set to true, then the resulting bitmap will allocate its pixels such that they can be purged if the system needs to reclaim memory.public intinSampleSizeIf set to a value & 1, requests the decoder to subsample the original image, returning a smaller image to save memory.public booleaninScaledWhen this flag is set, if inDensity and inTargetDensity are not 0, the bitmap will be scaled to match inTargetDensity when loaded, rather than relying on the graphics system scaling it each time it is drawn to a Canvas.public intinScreenDensityThe pixel density of the actual screen that is being used.public intinTargetDensityThe pixel density of the destination this bitmap will be drawn to.public byte[]inTempStorageTemp storage to use for decoding.public booleanmCancelFlag to indicate that cancel has been called on this object.public intoutHeightThe resulting height of the bitmap, set independent of the state of inJustDecodeBounds.public StringoutMimeTypeIf known, this string is set to the mimetype of the decoded image.public intoutWidthThe resulting width of the bitmap, set independent of the state of inJustDecodeBounds.这个表格是从android sdk文档里摘出来的,简单看一下说明就明白是什么意思了。下面我们回到我们的主题上来:怎样获取图片的大小?思路很简单:首先我们把这个图片转成Bitmap,然后再利用Bitmap的getWidth()和getHeight()方法就可以取到图片的宽高了。新问题又来了,在通过BitmapFactory.decodeFile(String path)方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。怎么避免它呢?这就用到了我们上面提到的BitmapFactory.Options这个类。BitmapFactory.Options这个类,有一个字段叫做 inJustDecodeBounds 。SDK中对这个成员的说明是这样的:If set to true, the decoder will return null (no bitmap), but the out…也就是说,如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。示例代码如下:BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds =Bitmap bmp = BitmapFactory.decodeFile(path, options);/* 这里返回的bmp是null */复制代码这段代码之后,options.outWidth 和 options.outHeight就是我们想要的宽和高了。有了宽,高的信息,我们怎样在图片不变形的情况下获取到图片指定大小的缩略图呢?比如我们需要在图片不变形的前提下得到宽度为200的缩略图。那么我们需要先计算一下缩放之后,图片的高度是多少 /* 计算得到图片的高度 *//* 这里需要主意,如果你需要更高的精度来保证图片不变形的话,需要自己进行一下数学运算 */int height = options.outHeight * 200 / options.outWoptions.outWidth = 200;options.outHeight = /* 这样才能真正的返回一个Bitmap给你 */options.inJustDecodeBounds =Bitmap bmp = BitmapFactory.decodeFile(path, options);image.setImageBitmap(bmp);复制代码这样虽然我们可以得到我们期望大小的ImageView但是在执行BitmapFactory.decodeFile(path, options);时,并没有节约内存。要想节约内存,还需要用到BitmapFactory.Options这个类里的
这个成员变量。我们可以根据图片实际的宽高和我们期望的宽高来计算得到这个值。inSampleSize = options.outWidth / 200;另外,为了节约内存我们还可以使用下面的几个字段:options.inPreferredConfig = Bitmap.Config.ARGB_4444;&&& // 默认是Bitmap.Config.ARGB_8888/* 下面两个字段需要组合使用 */options.inPurgeable =options.inInputShareable =&&&&
阅读(2249)
2829301234567101213141617192021252628303112345678
阅读排行榜
评论排行榜温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
推荐阅读:
阅读(37197)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'android:图片处理(本地uri图片显示,本地uri转为bitmap和网络uri转为bitmap显示)',
blogAbstract:'
设置imageView的图片:
1.参数为本地Uri
imageView.setImageURI(uri2); // content://media/external/images/media/63&&&&
&比如调用系统相机 返回uri2。&&&
&onActivityResult(int requestCode, int resultCode, Intent data)& &
&Uri uri2 = data.getData();&
2.参数为Bitmap
imageView.setImageBitmap(...);
A.把本地图片转为bitmap',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:5,
publishTime:0,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}Android : 转换 imageview
,为灰度, bitmap
到imageview
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我不要不要的得到任何警告关于 eclipse 编译此代码,但是当我在设备或模拟器上运行,该程序是被迫关闭。
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//convert imageview to bitmap
img =(ImageView) findViewById(R.id.imageView1);
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
final Bitmap imgbitmap = drawable.getBitmap();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//convert bitmap to grayscale
imgnew = toGrayscale(imgbitmap);
//convert bitmap to imageview
imgbit = (ImageView) findViewById(R.id.imageView2);
imgbit.setImageBitmap(imgnew);
public Bitmap toGrayscale(Bitmap bmpOriginal){
int width,
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpG
解决方法 1:
如果以上你的全是代码然后,基本问题是在那里,你还没有定义 btn 。您需要定义它,使用它之前, 别人时你 ar egoing,点击 button 它不会工作,这可能会关闭你 application 。
btn=(Button) findViewById(R.id.button1);}

我要回帖

更多关于 android imageview 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信