加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android Bitmap常用代码片段

发布时间:2020-12-14 23:19:12 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 获取方式 /** 获取 drawable 的图片 可以循环 1.图名 2.drawable 3.包名 **/ int imgid = getResources().getIdentifier("ic_launcher","drawable","c

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

获取方式

        /** 获取 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,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));
        // 发送位置和尺寸到View及其所有的子View
        rootView.layout(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,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,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,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,bitmap.getWidth(),bitmap.getHeight());
        final RectF rectF = new RectF(rect);
 
        paint.setAntiAlias(true);
        canvas.drawARGB(0,0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF,roundPx,paint);
 
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap,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,height / 2,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,null);
        // Draw in the gap
        Paint defaultPaint = new Paint();
        canvas.drawRect(0,height + reflectionGap,defaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage,null);
 
        // Create a shader that is a linear gradient that covers the reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,originalImage.getHeight(),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,paint);
 
        return bitmapWithReflection;
    }

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读