Android开发-对bitmap的一些的操作

/** 获取 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;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Q: `android.graphics.Bitmap` 是什么? A: `android.graphics.Bitmap` 是 Android 中处理位图图像的类。它表示一个位图图像,并允许程序对图像进行各种操作,例如缩放、裁剪、旋转、像素操作等。在 Android 应用开发中,经常使用 `Bitmap` 类来处理图像数据。 ### 回答2: Android.graphics.bitmapAndroid平台上常用的图像处理类,它允许应用程序创建、操作和保存图像数据。Bitmap类中的方法可以用来创建图像、读取和写入图像文件、缩放、旋转和裁剪图像、以及应用滤镜和其他效果等。 Bitmap类的主要特点是可扩展性和可定制性。它可以处理任何类型的位图图像,如JPEG、PNG、BMP等,并支持不同的图像格式和色彩空间。另外,Bitmap还可以与其他Android类库结合使用,如Canvas类、Drawable类和Paint类等,来完成更高级的图像处理任务。 Bitmap类的使用非常灵活,可以通过调用相应的方法来实现不同的功能。例如,要在应用程序中创建一个新图像,可以使用Bitmap.createBitmap()方法来创建一个空白的位图对象,然后使用Canvas类的方法来绘制图像。 Bitmap类还提供了一些辅助方法,如getAllocationByteCount()方法用于获取位图对象所占用的内存大小,getWidth()和getHeight()方法用于获取位图的宽度和高度,getPixel()方法用于获取像素颜色值等。 总之,Android.graphics.BitmapAndroid平台上非常重要的图像处理类,具备强大的功能和灵活的扩展性,可以让开发者更加方便地完成各种图像处理任务,从而提高应用程序的品质和用户体验。 ### 回答3: Android中的android.graphics.Bitmap表示一个图像,它由像素组成并被存储在内存中。Bitmap的宽度和高度以像素为单位,而每个像素占用4个字节(ARGB)的内存。Bitmap对象可以被用来显示在UI上或保存到文件中。 Bitmap类提供了很多方法来操作位图。例如,可以绘制位图,压缩位图,裁剪位图,旋转位图等。Bitmap还有一些实用的方法,例如从Drawable和资源文件中创建Bitmap对象。 Bitmap中的像素可以通过setPixel和getPixel方法进行访问和修改。同时,可以使用Bitmap.Config枚举类型来设置Bitmap对象的格式。Bitmap格式可以选择的选项包括ARGB_8888、RGB_565和ARGB_4444。 在使用Bitmap时,需要注意内存管理问题。由于Bitmap对象占用的内存较大,因此需要在使用Bitmap对象后将其回收。否则,当应用程序需要大量内存时,可能会导致应用程序crash或者运行缓慢。因此,建议使用Bitmap.recycle方法来释放Bitmap对象占用的内存。 总之,BitmapAndroid图形处理的重要部分,它可以让应用程序操作和显示图片。我们需要了解Bitmap的基本用法和内存管理,以便在应用程序中正确地使用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值