Pixel,Bitmap,Drawable,Canvas,Paint,Matrix,BitmapDrawable,BitmapFactory图相关

本文主要是介绍Pixel,Bitmap,Drawable,Canvas,Paint,Matrix,BitmapDrawable,BitmapFactory图相关,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Pixel

      像素,又称画素,为图像显示的基本单位。每个像素可有各自的颜色值,可采用三原色显示,因而又分成红、绿、蓝三种子像素(RGB色域),或者青、品红、黄和黑(CMYK色域,印刷行业以及打印机中常见)。照片是一个个采样点的集合,故而单位面积内的像素越多代表解析度越高,所显示的图像就会接近于真实物体。由像素组成的图像称为Bitmap(位图)。通常来说,对于一个显示屏幕,一个点就对应一个像素。


Bitmap

称作位图,又称栅格图(英语:Raster graphics)或称点阵图,是使用像素阵列来表示的图像,每个像素的颜色信息由RGB组合或者灰度值表示。根据颜色信息所需的数据位分为1、4、8、16、24及32位等,位数越高颜色越丰富,相应的数据量越大。其中使用1位表示一个像素颜色的位图因为一个数据位只能表示两种颜色,所以又称为二值位图。通常使用24位RGB组合数据位表示的的位图称为真彩色位图。一般来说,位图是没有经过压缩的,位图文件体积比较大。(位图常用的压缩算法是通过“索引颜色表”实现的),位图大多支持alpha通道(透明通道)。

常用方法:
static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)  
//Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.   
static Bitmap     createBitmap(int width, int height, Bitmap.Config config)  
//Returns a mutable bitmap with the specified width and height.   
static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height)  
//Returns an immutable bitmap from the specified subset of the source bitmap.   
static Bitmap     createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)  
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.   
static Bitmap     createBitmap(Bitmap src)  
//Returns an immutable bitmap from the source bitmap.   
static Bitmap     createBitmap(int[] colors, int width, int height, Bitmap.Config config)  
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.   
static Bitmap     createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)  
//Creates a new bitmap, scaled from an existing bitmap, when possible.  

从资源文件中获取:Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 

Drawable转化为Bitmap:
private Bitmap bitmap;
private void drawableToBitamp(Drawable drawable)
    {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        System.out.println("Drawable转Bitmap");
        Bitmap.Config config = 
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565;
        bitmap = Bitmap.createBitmap(w,h,config);
        //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(bitmap);   
        drawable.setBounds(0, 0, w, h);   
        drawable.draw(canvas);
    }

另外一种思想:

public static Bitmap drawableToBitmap(Drawable drawable) {        
        Bitmap bitmap = Bitmap
                        .createBitmap(
                                        drawable.getIntrinsicWidth(),
                                        drawable.getIntrinsicHeight(),
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
}

从资源文件中得到Bitmap:
private Bitmap bitmap;
private void drawableToBitamp(Drawable drawable)
    {
        BitmapDrawable bd = (BitmapDrawable) drawable;

        bitmap = bd.getBitmap();
    }

BitmapFactory

工厂类是一个工具类,提供了大量的方法,大多数是从不同的数据源来解码、创建Bitmap对象

static Bitmap   decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)  
//Decode an immutable bitmap from the specified byte array.   
//解析byte[]   
static Bitmap   decodeByteArray(byte[] data, int offset, int length)  
//Decode an immutable bitmap from the specified byte array.   
static Bitmap   decodeFile(String pathName)  
//Decode a file path into a bitmap.   
static Bitmap   decodeFile(String pathName, BitmapFactory.Options opts)  
//Decode a file path into a bitmap.   
static Bitmap   decodeFileDescriptor(FileDescriptor fd)  
//Decode a bitmap from the file descriptor.   
static Bitmap   decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)  
//Decode a bitmap from the file descriptor.   
static Bitmap   decodeResource(Resources res, int id, BitmapFactory.Options opts)  
//Synonym for opening the given resource and calling decodeResourceStream(Resources, TypedValue, InputStream, Rect, BitmapFactory.Options).   
static Bitmap   decodeResource(Resources res, int id)  
//Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) will null Options.   
static Bitmap   decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)  
//Decode a new Bitmap from an InputStream.   
static Bitmap   decodeStream(InputStream is)  
//Decode an input stream into a bitmap.   
static Bitmap   decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)  
//Decode an input stream into a bitmap.  


BitmapDrawable

继承于Drawable
//方法一   
Resources res;  
InputStream is=res.openRawResource(R.drawable.pic);  
BitmapDrawable bitmapDrawable=new BitmapDrawable(is);  
Bitmap bmp=bitmapDrawable.getBitmap();    
//方法二   Resources res;  
BitmapDrawable bitmapDrawable=(BitmapDrawable)res.getDrawable(R.drawable.pic);  
Bitmap bmp=bitmapDrawable.getBitmap();    
//方法三   

ImageView image;  
image.setImageBitmap(BitmapFactory.decodeStream(~~~~));  
BitmapDrawable bitmapDrawable=(BitmapDrawable)image.getDrawable();  
Bitmap bmp=bitmapDrawable.getBitmap();  


Drawable

当在Android工程的Drawable文件夹中导入图像文件时,Android SDK会为这个文件生成一个Drawable对象。可以通过R.drawable的方式访问这个对象。一般是调用Resource.getDrawable(int id)的方式直接获取。
Drawable 文件夹支持的图像格式有GIF、PNG、JPG,BMP。


Bitmap转为Drawable:
BitmapDrawable bitmapDrawable= new BitmapDrawable(bitmap)   
 因为BtimapDrawable是Drawable的子类,最终直接使用bitmapDrawable即可。  

bitmap转为Canvas,Canvas转为Drawable

Canvas canvas = new Canvas(bitmap);   
drawable.setBounds(0, 0, w, h);   
drawable.draw(canvas);


Canvas Paint
理解Canvas对象,可以把它当做画布,Canvas的方法大多数是设置画布的大小、形状、画布背景颜色等等,要想在画布上面画画,一般要与Paint对象结合使用,顾名思义,Paint就是画笔的风格,颜料的色彩之类的。


Matrix
Matrix为矩阵的意思,一般用来与Bitmap配合,实现图像的缩放、变形、扭曲等操作。
public static Bitmap scaleBitmap(Bitmap bitmap, int scalWidth, int scaleHeight) {    
        int w = bitmap.getWidth();    
        int h = bitmap.getHeight();    
        // 创建操作图片用的Matrix对象     
        Matrix matrix = new Matrix();    
        // 计算缩放比例     
        float sx= ((float) scaleWidth / w);    
        float sy= ((float) scaleHeight / h);    
        // 设置缩放比例     
        matrix.postScale(sx, sy);    
        // 建立新的bitmap,其内容是对原bitmap的缩放后的图    
        Bitmap scaleBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);    
        return scaleBmp;    
    }    
Matrix类的其他典型方法。
boolean  postScale(float sx, float sy)//缩放   
boolean     postSkew(float kx, float ky)//扭曲   
boolean     postTranslate(float dx, float dy)//转换   
boolean     preConcat(Matrix other)//合并   
boolean     preRotate(float degrees)//旋转  



Bitmap2Bytes
public byte[] Bitmap2Bytes(Bitmap bmp) {    
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();    
        //public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)   
        bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream );    
        return byteArrayOutputStream.toByteArray();    
    }    

另外一种兼容代码:

private byte[] Bitmap2Bytes(Bitmap bm){
 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 
 return baos.toByteArray();
 }
Bytes2Bitmap
static Bitmap   decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)  
//Decode an immutable bitmap from the specified byte array.   
//解析byte[]  

另外一种兼容代码:

private Bitmap Bytes2Bimap(byte[] b){
   if(b.length!=0){
    return BitmapFactory.decodeByteArray(b, 0, b.length);
   }
   else {
    return null;
   }
 }

这篇关于Pixel,Bitmap,Drawable,Canvas,Paint,Matrix,BitmapDrawable,BitmapFactory图相关的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021734

相关文章

RecastNavigation之Poly相关类

Poly分成正常的Poly 和 OffMeshPoly。 正常的Poly 又分成 原始的Poly 和 Detail化的Poly,本文介绍这两种。 Poly的边分成三种类型: 1. 正常边:有tile内部的poly与之相邻 2.border边:没有poly与之相邻 3.Portal边:与之相邻的是外部tile的poly   由firstLink索引 得到第一个连接的Poly  通

以canvas方式绘制粒子背景效果,感觉还可以

这个是看到项目中别人写好的,感觉这种写法效果还可以,就存留记录下 就是这种的背景效果。如果想改背景颜色可以通过canvas.js文件中的fillStyle值改。 附上demo下载地址。 https://download.csdn.net/download/u012138137/11249872

SQL Server中,always on服务器的相关操作

在SQL Server中,建立了always on服务,可用于数据库的同步备份,当数据库出现问题后,always on服务会自动切换主从服务器。 例如192.168.1.10为主服务器,12为从服务器,当主服务器出现问题后,always on自动将主服务器切换为12,保证数据库正常访问。 对于always on服务器有如下操作: 1、切换主从服务器:假如需要手动切换主从服务器时(如果两个服务

pixel_link记录

export PYTHONPATH=/path2to/pixel_link/pylib/src:$PYTHONPATH   https://blog.csdn.net/northeastsqure/article/details/83655200   https://blog.csdn.net/u011440558/article/details/78606662   报错: All

相关网站

力扣  https://leetcode-cn.com/contest/weekly-contest-124

CALayer相关的属性

iOS开发UI篇—CAlayer层的属性 一、position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设置CALayer在父层中的位置 以父层的左上角为原点(0, 0)   @property CGPoint anchorPoint; 称为“定位点”、“锚点”

Avalonia 常用控件二 Menu相关

1、Menu 添加代码如下 <Button HorizontalAlignment="Center" Content="Menu/菜单"><Button.Flyout><MenuFlyout><MenuItem Header="打开"/><MenuItem Header="-"/><MenuItem Header="关闭"/></MenuFlyout></Button.Flyout></B

1_Image和Matrix的使用

参考博文: https://www.cnblogs.com/bomo/archive/2013/03/28/2986573.html

bootstrap和JS相关

下表列出了模态框中要用到事件。这些事件可在函数中当钩子使用。 bootstrap 显示隐藏div $('.show-info').click(function () {var show = $(this).data('show');if(show =='all'){$('#creative').show();$('#plan').show();$('#plan').attr('class','

3D模型相关生成

3D模型相关生成 1. DreamFusion Model DreamFusion Model 是一种将文本描述转化为三维模型的技术。你可以想象它是一个“魔法翻译器”,你告诉它一个场景或物体的描述,比如“一个飞翔的龙”,它就能生成一个相应的 3D 模型。 原理: 文本到图像生成:DreamFusion 首先将文本描述转化为一系列可能的 2D 图像。这部分利用了预训练的扩散模型(如 DALL