Bitmap用法总结

2024-09-12 版权声明 我要投稿

Bitmap用法总结(精选2篇)

Bitmap用法总结 篇1

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 Software7、bitmap的用法小结

BitmapFactory.Options option = new BitmapFactory.Options();

option.inSampleSize = 2;//将图片设为原来宽高的1/2,防止内存溢出

Bitmap bm = BitmapFactory.decodeFile(“",option);//文件流

URL url = new URL(”");

InputStream is = url.openStream();

Bitmap bm = BitmapFactory.decodeStream(is);

android:scaleType:

android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /

android:scaleType值的意义区别:

CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分

显示

CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长

(宽)

CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片

长/宽等于或小于View的长/宽

Generated by Foxit PDF Creator © Foxit Software

http:// For evaluation only.FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置 FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置 FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示

MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

//放大缩小图片

public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidht =((float)w / width);

float scaleHeight =((float)h / height);

matrix.postScale(scaleWidht, scaleHeight);

Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

return newbmp;

}

//将Drawable转化为Bitmap

public static Bitmap drawableToBitmap(Drawable drawable){

int width = drawable.getIntrinsicWidth();

int height = drawable.getIntrinsicHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height,drawable.getOpacity()!= PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

drawable.setBounds(0,0,width,height);

drawable.draw(canvas);

return bitmap;

Generated by Foxit PDF Creator © Foxit Software

http:// For evaluation only.}

//获得圆角图片的方法

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap

.getHeight(), Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xff424242;

final Paint paint = new Paint();

final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);

canvas.drawARGB(0, 0, 0, 0);

paint.setColor(color);

canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);

return output;

}

//获得带倒影的图片方法

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ final int reflectionGap = 4;

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.preScale(1,-1);

Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width,(height + height/2), Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(bitmap, 0, 0, null);

Paint deafalutPaint = new Paint();

Generated by Foxit PDF Creator © Foxit Software

http:// For evaluation only.canvas.drawRect(0, height,width,height + reflectionGap,deafalutPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);Paint paint = new Paint();

LinearGradient shader = new LinearGradient(0,bitmap.getHeight(), 0, bitmapWithReflection.getHeight()

+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

paint.setShader(shader);

// Set the Transfer mode to be porter duff and destination in

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

// Draw a rectangle using the paint with our linear gradient

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);

return bitmapWithReflection;

强调句型用法总结 篇2

一、强调句型的基本结构:It is/was+被强调成分+that/who+其余成分

【说明】1.强调句型能强调除谓语之外的任何成分,强调谓语要用do/did/does+动词原形。2.如果被强调部分所在的句子是一般现在时, 就用is; 如果是一般过去时,则用was。3.如果被强调部分是“人”,一般用who, 但也可用that; 如果被强调部分是“人”以外的“事/物”等,仅能用that。例如:

It was because he fell ill that he was absent from class this morning.

真的是由于他病了,所以今天早上没有上课。

It is I who/that am to blame for the accident.

由于这场事故,肯定是我要受到谴责。

It is an English composition that Mary is writing now.

玛丽现在正在写的的确是一篇英语作文。

二、判断是不是强调句型的标准:如果把句中的“It is/was”和“that/who”同时去掉,句子成分不残缺,句意完整,就是强调句型,否则,就不是。例如:

It was at eight that he got up. (强调句型) 他是在八点起床的。

It was a surprise that John got full marks in the English test.(不是强调句型)

约翰在这次英语测验中得了满分是一件吃惊的事。

三、强调句型的否定句、一般疑问句和特殊疑问句:

否定句为:It is/was+not+被强调成分+that/who+其余成分.

一般疑问句为:Is/Was it+被强调成分+that/who+其余成分?

特殊疑问句为:特殊疑问词+is/was it that+其余成分? 例如:

It is not only blind men who made such stupid mistakes.

不仅仅是盲人犯了如此愚蠢的错误。

Is it the girl in red who is your sister? 那个穿红衣服的女孩真的是你的姐姐吗?

Where was it that you found your wallet? 你究竟在什么地方找到了你的钱夹?

四、若对“not…until…”中的“until…”进行强调时,“not”必须置于“until…”之前,即形成了如下结构:“It is/was not until…+that+其余成分”。例如:

It was not until eleven o’clock that I came back. 直到11点我才回来。

It was not until she spoke that I had realized she was foreign.

的确直到她说话我才意识到她是外国人。

五、强调句型与相关句型的辨析:

1.与定语从句的辨析

①It is in the place that they knew each other. 正是在这个地方,他们彼此认识。

②It is the place where they knew each other. 这是他们彼此认识的地方。

分析:句①是强调句型,强调的是地点状语in the place, 若去掉It is和that, 剩余部分成分不残缺,句意完整。句②划线部分是定语从句, 修饰先行词the place, where引导定语从句并且在从句中作地点状语,不可换成that/which。

2.与状语从句的辨析

①It was at 12:00 a.m. that Jack came to visit me. 杰克是在上午十二点来看我的。

②It was 12:00 a.m. when Jack came to visit me. 当杰克来看我的时候是上午十二点。

分析:句①是强调句型,强调的是时间状语at 12:00 a.m., 若去掉It was和that, 剩余部分成分不残缺,句意完整。句②划线部分是时间状语从句。

3.与主语从句的辨析

①It is the museum that I will pay a visit to. 那正是我将要参观的博物馆。

②It is true that I will pay a visit to the museum. 我将要参观那家博物馆是真的。

分析:句①是强调句型, 强调的是原句的宾语the museum, 若去掉划线部分, 剩余部分成分不残缺,句意完整。句②是主语从句, It是形式主语, 真正主语是划线部分that I will pay a

上一篇:播音教师工作总结下一篇:师德师风演讲比赛园长讲话