(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap

上传人:管****问 文档编号:118904376 上传时间:2019-12-28 格式:DOC 页数:7 大小:80KB
返回 下载 相关 举报
(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap_第1页
第1页 / 共7页
(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap_第2页
第2页 / 共7页
(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap_第3页
第3页 / 共7页
(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap_第4页
第4页 / 共7页
(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap》由会员分享,可在线阅读,更多相关《(bi商务智能)Android开发者学习笔记——View、Canvas、bitmap(7页珍藏版)》请在金锄头文库上搜索。

1、开发者学习笔记View&CanvasBitMap、View以及Canvas是我们Ophone程序中常用到的类。本日以feisky的学习笔记呈现,通过实例讲解View&Canvas等等。1. 从资源中获取位图可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。 当然,首先需要获取资源: Resources res=getResources(); 使用BitmapDrawable获取位图 使用BitmapDrawable (InputStream is)构造一个BitmapDrawable; 使用BitmapDrawable类的getBitmap()获取得到位图;

2、/ 读取InputStream并得到位图InputStream is=res.openRawResource(R.drawable.pic180);BitmapDrawable bmpDraw=new BitmapDrawable(is);Bitmap bmp=bmpDraw.getBitmap();或者采用下面的方式:BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);Bitmap bmp=bmpDraw.getBitmap();使用BitmapFactory获取位图(Creates Bitmap

3、 objects from various sources, including files, streams, and byte-arrays.) 使用BitmapFactory类decodeStream(InputStream is)解码位图资源,获取位图。Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180); BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。 以上方法在编程的时候可以自由选择,在Android SDK中说明可以支持的图片格式如下:

4、png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。 2. 获取位图的信息要获取位图信息,比如位图大小、像素、density、透明度、颜色格式等,获取得到Bitmap就迎刃而解了,这些信息在Bitmap的手册中,这里只是辅助说明以下2点: 在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题;Bitma

5、p还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充了。3. 显示位图显示位图可以使用核心类Canvas,通过Canvas类的drawBirmap()显示位图,或者借助于BitmapDrawable来将Bitmap绘制到Canvas。当然,也可以通过BitmapDrawable将位图显示到View中。 转换为BitmapDrawable对象显示位图/ 获取位图Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);/ 转换为Bit

6、mapDrawable对象BitmapDrawable bmpDraw=new BitmapDrawable(bmp);/ 显示位图ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);iv2.setImageDrawable(bmpDraw); 使用Canvas类显示位图 这儿采用一个继承自View的子类Panel,在子类的OnDraw中显示 public class MainActivity extends Activity /* Called when the activity is first created. */Over

7、ridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(new Panel(this);class Panel extends View public Panel(Context context) super(context); public void onDraw(Canvas canvas) Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);

8、 canvas.drawColor(Color.BLACK); canvas.drawBitmap(bmp, 10, 10, null); 4. 位图缩放(1)将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。 (2)在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)(3)

9、借助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不过要注意此时整个画布都缩放了。(4)借助Matrix:Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrix=new Matrix();matrix.postScale(0.2f, 0.2f);Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.

10、getWidth(),bmp.getHeight(),matrix,true);canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null); 5.位图旋转同样,位图的旋转也可以借助Matrix或者Canvas来实现。Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrix=new Matrix();matrix.postScale(0.8f, 0.8f);matrix.postRotate

11、(45);Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null); 旋转效果:6.图片水印的生成方法生成水印的过程。其实分为三个环节:第一,载入原始图片;第二,载入水印图片;第三,保存新的图片。/* * create the bitmap from a byte array * * param src the bitmap object y

12、ou want proecss* param watermark the water mark above the src * return return a bitmap object ,if paramters length is 0,return null*/ private Bitmap createBitmap( Bitmap src, Bitmap watermark ) String tag = createBitmap; Log.d( tag, create a new bitmap ); if( src = null ) return null; int w = src.ge

13、tWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); /create the new blank bitmap Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );/创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas( newb ); /draw src into cv.drawBitmap( src, 0, 0, null );/在 0,0坐标开始画入

14、src/draw watermark into cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );/在src的右下角画入水印 /save all clip cv.save( Canvas.ALL_SAVE_FLAG );/保存 /store cv.restore();/存储 return newb; 6.图片水印的生成方法生成水印的过程。其实分为三个环节:第一,载入原始图片;第二,载入水印图片;第三,保存新的图片。/* * create the bitmap from a byte array * * param src the

15、bitmap object you want proecss* param watermark the water mark above the src * return return a bitmap object ,if paramters length is 0,return null*/ private Bitmap createBitmap( Bitmap src, Bitmap watermark ) String tag = createBitmap; Log.d( tag, create a new bitmap ); if( src = null ) return null; int w = src.getWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); /create

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 经营企划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号