1,setCompoundDrawables(Drawable left, Drawable top,Drawable right, Drawable bottom)

  设置图片出现在textView,button,editText的各个方向.其中,left是drawable类型的.

2.如何获取上面的drawable

  Drawable drawable = getResources().getDrawable(R.drawable.username);

  drawable.setBounds(5,1,60,50);

  设置drawable的坐标为5,1,宽和高为:60和50

3.

手动设置文本与图片相对位置时,常用到如下方法:

setCompoundDrawables(left, top, right, bottom)

setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)

意思是设置Drawable显示在text的左、上、右、下位置。

但是两者有些区别:
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,
所以才有The Drawables must already have had setBounds(Rect) called.

使用之前必须使用Drawable.setBounds设置Drawable的长宽。

setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,
所以才有The Drawables' bounds will be set to their intrinsic bounds.

即通过getIntrinsicWidth()与getIntrinsicHeight()获得,

4.

1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
1、从资源中获取Bitmap
 
    1. Resources res = getResources();  
    2. Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);  

 

2、Bitmap → byte[]
 
public byte[] Bitmap2Bytes(Bitmap bm) {  

 ByteArrayOutputStream baos = new ByteArrayOutputStream();  

 bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();  

}  

 
 
     public Bitmap Bytes2Bimap(byte[] b) {  
      if (b.length != 0) {  
        return BitmapFactory.decodeByteArray(b, 0, b.length);  
     } else {  
          return null;  
     }  
     }  
 
     public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {  
       int w = bitmap.getWidth();  
        int h = bitmap.getHeight();  
        Matrix matrix = new Matrix();  
       float scaleWidth = ((float) width / w);  
        float scaleHeight = ((float) height / h);  
         matrix.postScale(scaleWidth, scaleHeight);  
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);  
        return newbmp;  
     }  
  drawable转化成bitmap
 
     public static Bitmap drawableToBitmap(Drawable drawable) {  
          // 取 drawable 的长宽  
          int w = drawable.getIntrinsicWidth();  
           int h = drawable.getIntrinsicHeight();  
    
           // 取 drawable 的颜色格式  
           Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                    : Bitmap.Config.RGB_565;  
        // 建立对应 bitmap  
           Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
          // 建立对应 bitmap 的画布  
             Canvas canvas = new Canvas(bitmap);  
           drawable.setBounds(0, 0, w, h);  
           // 把 drawable 内容画到画布中  
           drawable.draw(canvas);  
           return bitmap;  
       }  
获得圆角图片 
 
     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {  
         int w = bitmap.getWidth();  
         int h = bitmap.getHeight();  
         Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
        Canvas canvas = new Canvas(output);  
        final int color = 0xff424242;  
        final Paint paint = new Paint();  
         final Rect rect = new Rect(0, 0, w, h);  
        final RectF rectF = new RectF(rect);  
         paint.setAntiAlias(true);  
       canvas.drawARGB(0, 0, 0, 0);  
         paint.setColor(color);  
         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
         canvas.drawBitmap(bitmap, rect, rect, paint);  
       
         return output;  
    }  
 
 
 
1、Bitmap转换成Drawable
    .Bitmap bm=xxx; //xxx根据你的情况获取  
     BitmapDrawable bd= new BitmapDrawable(getResource(), bm);   
     因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
  
2、Drawable缩放
     public static Drawable zoomDrawable(Drawable drawable, int w, int h) {  
       int width = drawable.getIntrinsicWidth();  
        int height = drawable.getIntrinsicHeight();  
       // drawable转换成bitmap  
         Bitmap oldbmp = drawableToBitmap(drawable);  
         // 创建操作图片用的Matrix对象  
        Matrix matrix = new Matrix();  
        // 计算缩放比例  
         float sx = ((float) w / width);  
         float sy = ((float) h / height);  
         // 设置缩放比例  
         matrix.postScale(sx, sy);  
      // 建立新的bitmap,其内容是对原bitmap的缩放后的图  
         Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,  
                 matrix, true);  
        return new BitmapDrawable(newbmp);  
     }

android 图片的更多相关文章

  1. Android图片缓存之Lru算法

    前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...

  2. Android图片处理

    相信做Android开发的小伙伴对于Android图片压缩.裁剪一定有很深的印象,今天我将带领大家一起学习一下这个看着高深莫测的知识,以便再以后的学习.工作中可以帮助到大家. 首先我们看一下这个问题出 ...

  3. Android图片缓存之Glide进阶

    前言: 前面学习了Glide的简单使用(Android图片缓存之初识Glide),今天来学习一下Glide稍微复杂一点的使用. 图片缓存相关博客地址: Android图片缓存之Bitmap详解 And ...

  4. Android图片缓存之初识Glide

    前言: 前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架.技术本身就要不断的更迭,从最初的自己使用SoftReference实现自己的图片缓存,到后来做电商项目自己的实 ...

  5. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

  6. Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案

    1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...

  7. 一步一步打造自己的Android图片浏览器(原创)

    今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩 ...

  8. Android 图片浏览器 从原来位置放大至全屏显示

    android 图片浏览器 特点: 1.从网络加载图片,只需要传图片地址数组即可 2.点击图片,从原来位置放大至全屏 3.支持手势操作 4.完全自定义布局 项目源码请到GitHub下载:https:/ ...

  9. android图片处理方法

    Java代码 //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ...

  10. Android 图片文件操作、屏幕相关、.9图片的理解

     一:Android图片操作 1.存储bitmap到本地文件系统 public static void bitmapToFile(Bitmap bitmap, String uri) { if(!ex ...

随机推荐

  1. POJ2370【水题】

    //#include <bits/stdc++.h> #include<iostream> #include<string.h> #include<cstdi ...

  2. vue父组件调用子组件方法

    父组件: 代码 <sampleapplylinemodel ref="sampleapplylinemodel" @reLoad="_fetchRecords&qu ...

  3. LuoguP2700逐个击破【并查集/生成树/正难则反】By cellur925

    题目传送门 题目大意:给你一棵树,求把其中k个点相互隔离(不连通)所需要的边权代价. 这题我开始是想要求出把k个点联通的最小代价的,但后来发现还是实现起来比较困难,题解里貌似也没有这种做法,于是就鸽了 ...

  4. java实现数据结构

    数据结构与算法 :一.数据结构和算法简介 数据结构是指数据在计算机存储空间中的安排方式,而算法时值软件程序用来操作这些结构中的数据的过程.二. 数据结构和算法的重要性 几乎所有的程序都会使用到数据结构 ...

  5. stylus基础教程,stylus实例教程,stylus语法总结

    stylus特点富于表现力.具有健壮性.功能丰富.动态编码不需要写CSS的冒号.分号.大括号和LESS.SASS功能类似,会这些的入手很快stylus特点安装使用stylus语法(一)选择器(二)变量 ...

  6. Jquery实现相对浏览器位置固定、悬浮

      <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></sc ...

  7. 1-18String类简介

    字符串(String)的不可变性 String类在java.lang包下面,是Object类的直接子类,通过API或者源码可以看到,String类是final修饰的,这说明String类不能被继承. ...

  8. 微信小程序消息推送,前端操作

    <form bindsubmit="getFormId" report-submit="true"> <button form-type=&q ...

  9. COPY, RETAIN, ASSIGN , READONLY , READWRITE,STRONG,WEAK,NONATOMIC整理--转

    copy:建立一个索引计数为1的对象,然后释放旧对象 对NSString 对NSString 它指出,在赋值时使用传入值的一份拷贝.拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协 ...

  10. nginx中常见的变量

    $arg_PARAMETER        客户端GET请求PARAMETER的值. $args     请求中的参数. $binary_remote_addr 二进制码形式的客户端地址. $body ...