/** 获取 drawable 的图片   可以循环   1.图名    2.drawable 3.包名      **/

int imgid = getResources().getIdentifier("ic_launcher", "drawable", "com.example.anywight");
text.setBackgroundResource(imgid); /** 通过图片id获得Bitmap **/
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); /** 通过 assest 获取 获得Drawable bitmap **/
InputStream in = this.getAssets().open("ic_launcher");
Drawable da = Drawable.createFromStream(in, null);
Bitmap mm = BitmapFactory.decodeStream(in); /** 通过 sdcard 获得 bitmap **/
Bitmap bit = BitmapFactory.decodeFile("/sdcard/android.jpg"); /** view转Bitmap **/
public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight){ Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
view.draw(new Canvas(bitmap));
return bitmap;
} /** 将控件转换为bitmap **/
public static Bitmap convertViewToBitMap(View view){
// 打开图像缓存
view.setDrawingCacheEnabled(true);
// 必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件
// 测量View大小
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
// 发送位置和尺寸到View及其所有的子View
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// 获得可视组件的截图
Bitmap bitmap = view.getDrawingCache();
return bitmap;
} public static Bitmap getBitmapFromView(View view){
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
} /** 获取屏幕截图的bitmap对象的代码如下 **/
public Bitmap getScreenPic(View view){ View rootView = view.getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache();
// 不明白为什么这里返回一个空,有帖子说不能在oncreat方法中调用
// 测量View大小
rootView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
// 发送位置和尺寸到View及其所有的子View
rootView.layout(0, 0, rootView.getMeasuredWidth(), rootView.getMeasuredHeight());
// 解决措施,调用上面的measure和layout方法之后,返回值就不再为空
// 如果想要创建的是固定长度和宽度的呢?
Bitmap bitmap = rootView.getDrawingCache();
rootView.destroyDrawingCache();
return bitmap;
} /** 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; } /** bitmap → drawable **/
public static Drawable bitmapToDrawable(Context context,String filename){ Bitmap image = null;
BitmapDrawable ddd = null;
try {
AssetManager am = context.getAssets();
InputStream is = am.open(filename);
image = BitmapFactory.decodeStream(is);
ddd = new BitmapDrawable(context.getResources(), image);
is.close();
} catch (Exception e) {
}
return ddd; } /** byte[] → Bitmap **/
public static Bitmap byteToDrawable(Context context,byte[] bb){
Bitmap pp = BitmapFactory.decodeByteArray(bb, 0, bb.length);
return pp;
} /** Bitmap → byte[]**/
public static byte[] bitmapToByte(Bitmap bitmap){ ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] yy = baos.toByteArray();
return yy;
} /** 将text 转换成 bitmap **/
public static Bitmap createTxtImage(String txt, int txtSize) {
Bitmap mbmpTest = Bitmap.createBitmap(txt.length() * txtSize + 4,
txtSize + 4, Config.ARGB_8888);
Canvas canvasTemp = new Canvas(mbmpTest);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.WHITE);
p.setTextSize(txtSize);
canvasTemp.drawText(txt, 2, txtSize - 2, p);
return mbmpTest; } /** 显示将bitmap进行缩放 **/
public Bitmap bitmapScanel(Context context){
//通过openRawResource获取一个inputStream对象
InputStream inputStream = context.getResources().openRawResource(R.id.backageground);
//通过一个InputStream创建一个BitmapDrawable对象
BitmapDrawable drawable = new BitmapDrawable(inputStream);
//通过BitmapDrawable对象获得Bitmap对象
Bitmap bitmap = drawable.getBitmap();
//利用Bitmap对象创建缩略图
bitmap = ThumbnailUtils.extractThumbnail(bitmap, 40, 40);
return bitmap; } /** 放大缩小图片 **/
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;
} /** 获得圆角图片的方法 **/
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;
} /** 对 bitmap 进行裁剪 **/
public Bitmap bitmapClip(Context context , int id , int x , int y){
Bitmap map = BitmapFactory.decodeResource(context.getResources(), id);
map = Bitmap.createBitmap(map, x, y, 120, 120);
return map;
} /**
* 图片的倒影效果
*/
public static Bitmap createReflectedImage(Bitmap originalImage) {
final int reflectionGap = 4; int width = originalImage.getWidth();
int height = originalImage.getHeight(); Matrix matrix = new Matrix();
matrix.preScale(1, -1); // Create a Bitmap with the flip matrix applied to it.
// We only want the bottom half of the image
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false); // Create a new bitmap with same width but taller to fit reflection
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888); // Create a new Canvas with the bitmap that's big enough for
// the image plus gap plus reflection
Canvas canvas = new Canvas(bitmapWithReflection);
// Draw in the original image
canvas.drawBitmap(originalImage, 0, 0, null);
// Draw in the gap
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
// Draw in the reflection
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // Create a shader that is a linear gradient that covers the reflection
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
// Set the paint to use this shader (linear gradient)
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;
}

android bitmap的 一些简单操作的更多相关文章

  1. Android Bitmap 载入与像素操作

    Android Bitmap 载入与像素操作 一:载入与像素读写 在Android SDK中,图像的像素读写能够通过getPixel与setPixel两个Bitmap的API实现. Bitmap AP ...

  2. [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888

    [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888 标签: androidbitmapjni 2014-05-09 20:35 2985人阅读 评论(1) 收 ...

  3. Android Bitmap 常见的几个操作:缩放,裁剪,旋转,偏移

    Android Bitmap 相关操作 常见的几个操作:缩放,裁剪,旋转,偏移      很多操作需要 Matrix 来支持:Matrix 通过矩阵来处理位图,计算出各个像素点的位置,从而把bitma ...

  4. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  5. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

  6. Android Bitmap 和 ByteArray的互相转换

    Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...

  7. Android Bitmap 全面解析(四)图片处理效果对比 ...

    对比对象: UIL Volley 官方教程中的方法(此系列教程一里介绍的,ImageLoader的处理方法和官方的差不多) -------------------------------------- ...

  8. 36、Android Bitmap 全面解析

    Android Bitmap 全面解析(一)加载大尺寸图片 http://www.eoeandroid.com/thread-331669-1-1.html Android Bitmap 全面解析(二 ...

  9. Android bitmap图片处理

    一.View转换为Bitmap         在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中 ...

随机推荐

  1. 每个Linux新手都应该记住的10个基本Linux命令

    Linux对我们的生活有着很大的影响.至少,你的安卓手机上面就有Linux内核.然而,头一次入手Linux只会让你觉得不适.因为在Linux上,你通常应该使用终端命令,而不是只要点击启动器图像(就像你 ...

  2. JQUERY1.9学习笔记 之属性选择器(二) 包含选择器

    jQuery("[attribute*='value']") 描述:选择所有与给定值匹配的属性值的标签. 例:找出所有name属性包含"man"的input标签 ...

  3. CSS3 box-shadow(阴影使用)

    from: http://jingyan.baidu.com/article/03b2f78c4d9fae5ea237aea6.html css3 box-shadow 内阴影与外阴影 1- box- ...

  4. 实现一个简单的sniffer

    #include<stdio.h> #include<pcap.h> #include<unistd.h> #include<stdlib.h> //# ...

  5. DevOps - Development And Operations

    简介: 研发运维一体化 相关资料: 关于DevOps你必须知道的11件事 我眼中的DevOps DevOps 门户 docker for dotnet系列 docker4dotnet #1 前世今生 ...

  6. ps的使用方法

    1.打开原图素材,Ctrl + J把背景图层复制一层,按Ctrl + Shift + U去色,执行:滤镜 > 模糊 > 高斯模糊,数值4,图层混合模式为滤色,图层不透明度改为27%. 2. ...

  7. unity3d中脚本生命周期(MonoBehaviour lifecycle)

    最近在做一个小示例,发现类继承于MonoBehaviour的类,有很多个方法,于是乎必然要问出一个问题:这么多个方法,执行先后顺序是如何的呢?内部是如何进行管理的呢?于是在网上找了许多资料,发现了Ri ...

  8. nodejs template

    Server-side http://cnodejs.org/topic/514ba98af848e01f6b2956bf http://jade-lang.com/ http://cnodejs.o ...

  9. hdu 5144 NPY and shot

    http://acm.hdu.edu.cn/showproblem.php?pid=5144 题意:给你初始的高度和速度,然后让你求出水平的最远距离. 思路:三分枚举角度,然后根据公式求出水平距离. ...

  10. 【HDOJ】2266 How Many Equations Can You Find

    简单DFS. #include <cstdio> #include <cstring> #define MAXN 15 char str[MAXN]; __int64 x; i ...