如果涉及一些Android游戏开发、显示特效可以通过这些底层图形类来高效实现自己的应用。
如在View中画一个图形:
protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(Color.RED); //直接画一条直线,简单 canvas.drawLine(0, 0, 50, 50, paint); //加载图片资源文件 Resources r = this.getContext().getResources(); Drawable drawale = r.getDrawable(R.drawable.yellowstar); //创建内存中的一张图片 Bitmap bitmap = Bitmap.createBitmap(12,12,Bitmap.Config.ARGB_8888); //图片画片 Canvas cas = new Canvas(bitmap); drawale.setBounds(0,0,12,12); //图片加载到bitmap上 drawale.draw(cas); //画到View上 canvas.drawBitmap(bitmap, 30, 80, paint); }
转换方法:
1)Bitmap 转化为 byte
ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); byte[] array= out.toByteArray();2)byte转化为bitmap
final ContentResolver contentResolver = context.getContentResolver(); final PackageManager manager = context.getPackageManager(); final Cursor c = contentResolver.query(uri, null, null, null, null); final int icon3DIndex = c.getColumnIndexOrThrow(ColumnName); byte[] data = c.getBlob(icon3DIndex); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);3)bitmap 转换 drawable
Bitmap bitmap = new Bitmap(...); Drawable drawable = new BitmapDrawable(bitmap); //Drawable drawable = new FastBitmapDrawable(bitmap);
4)Drawable to Bitmap
a. BitmapDrawable, FastBitmapDrawable直接用getBitmap
b. 其他类型的Drawable用Canvas画到一个bitmap上
Canvas canvas = new Canvas(bitmap) drawable.draw(canvas); Drawable d = ImagesList.get(0); Bitmap bitmap = ((BitmapDrawable)d).getBitmap();