日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

發(fā)布時(shí)間:2024/4/17 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

版權(quán)聲明:本文為HaiyuKing原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明出處!

前言

用于縮放bitmap以及將bitmap保存成圖片到SD卡中

效果圖

代碼分析

bitmapZoomByHeight(Bitmap srcBitmap, float newHeight): 根據(jù)指定的高度進(jìn)行縮放(src是bitmap)

bitmapZoomByHeight(Drawable drawable, float newHeight) :根據(jù)指定的高度進(jìn)行縮放(src是drawable)

bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight): 根據(jù)指定的寬度比例值和高度比例值進(jìn)行縮放

drawableToBitmap(Drawable drawable) :將drawable對(duì)象轉(zhuǎn)成bitmap對(duì)象

drawableToBitmap2(Drawable drawable) :將drawable對(duì)象轉(zhuǎn)成bitmap對(duì)象

saveBitmapToSDCard(Bitmap bitmap, String path): 將bitmap對(duì)象保存成圖片到sd卡中

getBitmapFromSDCard(String path) :從sd卡中去除圖片的bitmap對(duì)象

使用步驟

一、項(xiàng)目組織結(jié)構(gòu)圖

?

注意事項(xiàng):

1、? 導(dǎo)入類文件后需要change包名以及重新import R文件路徑

2、? Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),如果項(xiàng)目中存在,則復(fù)制里面的內(nèi)容,不要整個(gè)覆蓋

二、導(dǎo)入步驟

將BitmapUtil復(fù)制到項(xiàng)目中

package com.why.project.bitmaputildemo.utils;import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream;/*** Created by HaiyuKing* Used*/public class BitmapUtil {public static Bitmap temp;/**根據(jù)指定的高度進(jìn)行縮放(source是bitmap)*/public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap, float newHeight) {float scale = newHeight / (((float)srcBitmap.getHeight()));return BitmapUtil.bitmapZoomByScale(srcBitmap, scale, scale);}/**根據(jù)指定的高度進(jìn)行縮放(source是drawable)*/public static Bitmap bitmapZoomByHeight(Drawable drawable, float newHeight) {Bitmap bitmap = BitmapUtil.drawableToBitmap(drawable);float scale = newHeight / (((float)bitmap.getHeight()));return BitmapUtil.bitmapZoomByScale(bitmap, scale, scale);}/**根據(jù)指定的寬度比例值和高度比例值進(jìn)行縮放*/public static Bitmap bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight) {int width = srcBitmap.getWidth();int height = srcBitmap.getHeight();Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap bitmap = Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, true);if(bitmap != null) {return bitmap;}else {return srcBitmap;}}/**將drawable對(duì)象轉(zhuǎn)成bitmap對(duì)象*/public static Bitmap drawableToBitmap(Drawable drawable) {int width = drawable.getIntrinsicWidth();int height = drawable.getIntrinsicHeight();Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;Bitmap bitmap = Bitmap.createBitmap(width, height, config);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, width, height);drawable.draw(canvas);return bitmap;}/**將drawable對(duì)象轉(zhuǎn)成bitmap對(duì)象*/public static Bitmap drawableToBitmap2(Drawable drawable) {BitmapDrawable bd = (BitmapDrawable) drawable;Bitmap bm= bd.getBitmap();return bm;}/**將bitmap對(duì)象保存成圖片到sd卡中*/public static void saveBitmapToSDCard(Bitmap bitmap, String path) {File file = new File(path);if(file.exists()) {file.delete();}try {FileOutputStream fileOutputStream = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.PNG, 100, ((OutputStream)fileOutputStream));//設(shè)置PNG的話,透明區(qū)域不會(huì)變成黑色 fileOutputStream.close();System.out.println("----------save success-------------------");}catch(Exception v0) {v0.printStackTrace();}}/**從sd卡中獲取圖片的bitmap對(duì)象*/public static Bitmap getBitmapFromSDCard(String path) {Bitmap bitmap = null;try {FileInputStream fileInputStream = new FileInputStream(path);if(fileInputStream != null) {BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 2; //當(dāng)圖片資源太大的適合,會(huì)出現(xiàn)內(nèi)存溢出。圖片寬高都為原來的二分之一,即圖片為原來的四分一bitmap = BitmapFactory.decodeStream(((InputStream) fileInputStream), null, options);}} catch(Exception e) {return null;}return bitmap;}}

在AndroidMainfest.xml文件中聲明權(quán)限

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.why.project.bitmaputildemo"><!-- =================BitmapUtil用到的權(quán)限========================== --><!-- 允許程序讀取外部存儲(chǔ)文件 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><!-- 允許程序?qū)懭胪獠看鎯?chǔ),如SD卡上寫文件 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>

添加運(yùn)行時(shí)權(quán)限的處理(本demo中采用的是修改targetSDKVersion=22)

三、使用方法

本Demo搭配《AppDir【創(chuàng)建緩存目錄】》使用

package com.why.project.bitmaputildemo;import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView;import com.why.project.bitmaputildemo.utils.AppDir; import com.why.project.bitmaputildemo.utils.BitmapUtil;import java.io.File;public class MainActivity extends AppCompatActivity {private ImageView img_source;private ImageView img_scale1;private ImageView img_scale2;private Button btn_save;private Button btn_show;private ImageView img_show;private String pngFilePath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();initDatas();initEvents();}private void initViews() {img_source = (ImageView) findViewById(R.id.img_source);img_scale1 = (ImageView) findViewById(R.id.img_scale1);img_scale2 = (ImageView) findViewById(R.id.img_scale2);btn_save = (Button) findViewById(R.id.btn_save);btn_show = (Button) findViewById(R.id.btn_show);img_show = (ImageView) findViewById(R.id.img_show);}private void initDatas() {img_source.setImageResource(R.mipmap.ic_launcher);Bitmap sourceBitmap = BitmapUtil.drawableToBitmap(getResources().getDrawable(R.mipmap.ic_launcher));Bitmap sacleBitmap1 = BitmapUtil.bitmapZoomByHeight(sourceBitmap,200);img_scale1.setImageBitmap(sacleBitmap1);Bitmap sacleBitmap2 = BitmapUtil.bitmapZoomByScale(sourceBitmap,2,1);img_scale2.setImageBitmap(sacleBitmap2);}private void initEvents() {btn_save.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {pngFilePath = AppDir.getInstance(MainActivity.this).IMAGE + File.separator + System.currentTimeMillis() + ".png";Bitmap sourceBitmap = BitmapUtil.drawableToBitmap(getResources().getDrawable(R.mipmap.ic_launcher));BitmapUtil.saveBitmapToSDCard(sourceBitmap,pngFilePath);}});btn_show.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String pngPath = pngFilePath;Bitmap pngBitmap = BitmapUtil.getBitmapFromSDCard(pngPath);img_show.setImageBitmap(pngBitmap);}});} }

混淆配置

參考資料

Android常用的Drawable和Bitmap之間的轉(zhuǎn)化方法

項(xiàng)目demo下載地址

https://github.com/haiyuKing/BitmapUtilDemo

轉(zhuǎn)載于:https://www.cnblogs.com/whycxb/p/7635940.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。