android bitmap,canvas,paint常用方法API,总结



Bitmap:

1、Drawable → Bitmap

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;
 
}
 

2、从资源中获取Bitmap

Resources res=getResources();
 
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
 
3、Bitmap → byte[]


private byte[] Bitmap2Bytes(Bitmap bm){
 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
 
return baos.toByteArray();
 
}

 


4、byte[] → Bitmap


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


static boolean saveBitmap2file(Bitmap bmp,String filename){
 
CompressFormat format= Bitmap.CompressFormat.JPEG;
 
int quality = 100;
 
OutputStream stream = null;
 
try {
 
stream = new FileOutputStream("/sdcard/" + filename);
 
} catch (FileNotFoundException e) {
 
// TODO Auto-generated catch block
 
Generated by Foxit PDF Creator © Foxit Software
 
http://www.foxitsoftware.com For evaluation only.
 
e.printStackTrace();
 
}
 
return bmp.compress(format, quality, stream);
 
}
 

6、将图片按自己的要求缩放

 
// 图片源
 
Bitmap bm = BitmapFactory.decodeStream(getResources()
 
.openRawResource(R.drawable.dog));
 
// 获得图片的宽高
 
int width = bm.getWidth();
 
int height = bm.getHeight();
 
// 设置想要的大小
 
int newWidth = 320;
 
int newHeight = 480;
 
// 计算缩放比例
 
float scaleWidth = ((float) newWidth) / width;
 
float scaleHeight = ((float) newHeight) / height;
 
// 取得想要缩放的matrix参数
 
Matrix matrix = new Matrix();
 
matrix.postScale(scaleWidth, scaleHeight);
 
// 得到新的图片
 
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
 
true);
 
// 放在画布上
 
canvas.drawBitmap(newbm, 0, 0, paint);


File图片转Bitmap

Bitmap bt = BitmapFactory.decodeFile("/sdcard/myImage/" + "head.jpg");//图片地址
//图片转Bitmap
public Bitmap drawableToBitamp(int drawableResource) {<span style="white-space: pre;">  </span>//可以取raw里面的资源
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        InputStream is = this.getResources().openRawResource(drawableResource);
        BitmapFactory.decodeStream(is, null, opt);
        return BitmapFactory.decodeStream(is, null, opt);
    }
 



Canvas:


Canvas():创建一个空的画布,可以使用setBitmap()方法来设置绘制的具体画布;




    Canvas(Bitmap bitmap):以bitmap对象创建一个画布,则将内容都绘制在bitmap上,bitmap不得为null; 

    Canvas(GL gl):在绘制3D效果时使用,与OpenGL有关; 

    drawColor:设置画布的背景色; 
    setBitmap:设置具体的画布; 
    clipRect:设置显示区域,即设置裁剪区; 
    isOpaque:检测是否支持透明; 
    rotate:旋转画布;


  canvas.drawRect(RectF,Paint)方法用于画矩形,第一个参数为图形显示区域,第二个参数为画笔,设置好图形显示区域Rect和画笔Paint后,即可画图; 


    canvas.drawRoundRect(RectF, float, float, Paint) 方法用于画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。 

    canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint):第一个参数oval为RectF类型,即圆弧显示区域,startAngle和sweepAngle均为float类型,分别表示圆弧起始角度和圆弧度数,3点钟方向为0度,useCenter设置是否显示圆心,boolean类型,paint为画笔; 

    canvas.drawCircle(float,float, float, Paint)方法用于画圆,前两个参数代表圆心坐标,第三个参数为圆半径,第四个参数是画笔; 

 

   void drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象 

void  drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)  //贴图,参数一就是我们常规的Bitmap对象,参数二是源区域(这里是bitmap),参数三是目标区域(应该在 canvas的位置和大小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。 

void  drawLine(float startX, float startY, float stopX, float stopY, Paint paint)  //画线,参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint画刷对象。前四个参数的类型均为float,最后一个参数类型为Paint。表示用画笔paint从点(startX,startY)到点(stopX,stopY)画一条直线; 


void  drawPoint(float x, float y, Paint paint) //画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。


void drawText(String text, float x, float y, Paint paint)  //渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数四是Paint对象。


void  drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) //在路径上绘制文本,相对于上面第二个参数是Path路径对象



Paint:

setARGB(int a,int r,int g,int b);   

设置绘制的颜色,a代表透明度,r,g,b代表颜色值。   

   

setAlpha(int a);   

设置绘制图形的透明度。   

   

setColor(int color);   

设置绘制的颜色,使用颜色值来表示,该颜色值包括透明度和RGB颜色。   

   

etAntiAlias(boolean aa);   

设置是否使用抗锯齿功能,会消耗较大资源,绘制图形速度会变慢。   

   

setDither(boolean dither);   

设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰   

   

setFilterBitmap(boolean filter);   

如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示   

速度,本设置项依赖于dither和xfermode的设置   

   

setMaskFilter(MaskFilter maskfilter);   

设置MaskFilter,可以用不同的MaskFilter实现滤镜的效果,如滤化,立体等       *    

setColorFilter(ColorFilter colorfilter);   

设置颜色过滤器,可以在绘制颜色时实现不用颜色的变换效果   

   

setPathEffect(PathEffect effect);   

设置绘制路径的效果,如点画线等   

   

setShader(Shader shader);   

设置图像效果,使用Shader可以绘制出各种渐变效果   

  

setShadowLayer(float radius ,float dx,float dy,int color);   

在图形下面设置阴影层,产生阴影效果,radius为阴影的角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色   

   

setStyle(Paint.Style style);   

设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE   

   

setStrokeCap(Paint.Cap cap);   

当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式   

Cap.ROUND,或方形样式Cap.SQUARE   

   

setSrokeJoin(Paint.Join join);   

设置绘制时各图形的结合方式,如平滑效果等   

   

 setStrokeWidth(float width);   

 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度   

    

 setXfermode(Xfermode xfermode);   

 设置图形重叠时的处理方式,如合并,取交集或并集,经常用来制作橡皮的擦除效果   

     如:setXfermode (new PorterDuffXfermode(Mode.SRC_IN))


 2.文本绘制 

 setFakeBoldText(boolean fakeBoldText);   

 模拟实现粗体文字,设置在小字体上效果会非常差   

    

 setSubpixelText(boolean subpixelText);   

 设置该项为true,将有助于文本在LCD屏幕上的显示效果   

    

 setTextAlign(Paint.Align align);   

 设置绘制文字的对齐方向   

    

etTextScaleX(float scaleX);   

设置绘制文字x轴的缩放比例,可以实现文字的拉伸的效果   

    

 setTextSize(float textSize);   

 设置绘制文字的字号大小   

    

 setTextSkewX(float skewX);   

 设置斜体文字,skewX为倾斜弧度   

    

 setTypeface(Typeface typeface);   

 设置Typeface对象,即字体风格,包括粗体,斜体以及衬线体,非衬线体等   

    

 setUnderlineText(boolean underlineText);   

 设置带有下划线的文字效果   

    

 setStrikeThruText(boolean strikeThruText);   

 设置带有删除线的效果   




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值