drawable以及Bitmap的基本操作

一.drawable  图形对象,可以转载常用格式的图像,可能是(位图)Bitmapdrawable,或者shapedrawable(图形),还可能是多种其他图片格式GIF,PNG,JEPG

二.Bitmap 就是位图,用于图片的处理

三.Canvas 意为画布,就是绘画的目标区域,用来管理Bitmp或者path路径

下面就是讲到drawable转换成Bitmap的方法

我所知有两种方法(两种方法谁好谁坏,得看情况)

(1)常见的一种方法就是创建个Bitmap出来,再用画布绑定这个位图,将drawable绘到Bitmap上(此方法用于此时的drawable对象不是bitmap图像时)

Bitmap bitmap=Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
//创建一个size宽size高的一个位图

Canvas canvas=new Canvas(bitmap);
//将画布绑定到该位图上,为接下来的drawable绘画做准备

drawable.setBounds(0,0,size,size);
//drawable设置大小与位图相同

drawable.draw(canvas);
//将drawable通过画布画到Bitmap上去

(2)另外一种方法就是直接获取Bitmap(bitmapdrawable)

Bitmap icon=BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource)
//将drawable对象中的icon_resource图像转换成位图文件icon

或者

BitmapDrawable bd = (BitmapDrawable) drawable;
//先转换成Bitdrawable
bitmap = bd.getBitmap();
//再通过getBitmap()获取到Bitmap

另外关于drawmap()方法的说明

drawBitmap()方法:绘制图像,该方法用于在画布上绘制图像,是通过Bitmap对象来实现,当要引入图片资源时,我们可以采取该方法。

drawBitmap (Bitmap bitmap, float left, float top, Paint paint)

参数说明

bitmap:Bitmap对象,代表了图像资源。

left:图像显示的左边位置。

top:图像显示的上边位置。

paint:绘制时所使用的画笔。

在上述方法1中就是采用了该方法。

public class MyTile extends View
{
	public Bitmap[] bitmap1;
        private static final int size=12;
	public MyTile(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		Get_Bitmap();
		// TODO 自动生成的构造函数存根
	}

	/* (非 Javadoc)
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	public void Get_Bitmap()
	{
		bitmap1=new Bitmap[1];
		Bitmap bitmap=Bitmap.createBitmap(size, size, Config.ARGB_8888);
//创建一个size正方形大小的新位图
                Drawable   drawable=getResources().getDrawable
                (R.drawable.greenstar);
//获取drawable对象
		Canvas canvas=new Canvas(bitmap);
//Bitmap绑定画布
		drawable.setBounds(0,0,size,size);
//设置drawable对象大小size
                drawable.draw(canvas);
//最后将drawable画到Bitmap的canvas上去		

                 bitmap1[0]=bitmap;
	}
	protected void onDraw(Canvas canvas)
	{
		// TODO 自动生成的方法存根
		super.onDraw(canvas);
		Paint paint=new Paint();
		paint.setColor(Color.BLACK);
	        canvas.drawBitmap(bitmap1[0], 200, 200, paint);
	}

}
  

  

上述方法可以裁定图片的大小尺寸,在自定义位图大小的情况下通常采用这种方法

时间: 2024-08-01 21:16:10

drawable以及Bitmap的基本操作的相关文章

Android Drawable、Bitmap、byte[]之间的转换

转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatch

Drawable和Bitmap转换

一.Bitmap转Drawable Bitmap mBitMap=getYourBitMap(); //getYourBitMap()是你获取BitMap的方法 BitmapDrawable mBitDrawable=new BitmapDrawable(mBitMap); //BtimapDrawable是Drawable的子类,直接把mBitDrawable对象当作Drawable使用即可. 二. Drawable转Bitmap Drawable mDrawable=getYourDrawa

Android -- Drawable与Bitmap

Drawable                                                                                 以下这个是测试加载1000个Drawable对象的代码: public class Main extends Activity { int number = 1000; Drawable[] array; @Override public void onCreate(Bundle savedInstanceState)

Drawable与Bitmap 自定义

Drawable简介 Drawable是Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF.PNG.JPG,当然也支持BMP.相比于View,我们并不需要去考虑如何measure.layout,仅仅只要去考虑如何draw(canavs). Though usually not visible to the application, Drawables may take a variety of forms 形式: Bitmap: the simplest Drawable,

简单谈谈Resource,Drawable和Bitmap之间的转换

一直接触这些东西,还是归个类整理一下比较好. Resource -> Drawable Drawable draw1 = this.getResources().getDrawable(R.drawable.icon); Drawable -> Bitmap 1. static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap { int width = drawable.getIntrinsicWidth();/

Drawable转换为Bitmap两种方法

如果通过网络加载了一张位图,想拿到这张位图的Bitmap,有两种办法,至于那种好,可能要看是在什么情况下了 1,根据已有的Drawable创建一个新的Bitmap: private Bitmap bitmap;private void drawableToBitamp(Drawable drawable) { int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); System.out.printl

Drawable、Bitmap、byte[]之间的转换

1.Drawable → Bitmap Java代码   public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARG

Drawable和Bitmap区别

Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一种存储对象比较好.    Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF.PNG.JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变.图形等. A bitmap is a Drawable. A Drawable is not necessarily

SimpleAdapter中使用Drawable和Bitmap对象的方法

我们平常使用SimpleAdapter作为ListView或GridView适配器时,如果要显示图片,我们通常使用图片的id即R.drawable.xxx的方式来将图片绑定到视图上. 但是,如果我们要使用的图片是个Drawable或Bitmap对象时,上面的方式就不能解决问题了.下面就介绍如何在SimpleAdapter中使用Drawable或Bitmap对象. 1.使用Drawable对象作为SimpleAdapter要适配的图片资源 SimpleAdapter adapter=new Sim