最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)圖片壓縮示例代碼

 更新時(shí)間:2017年01月18日 14:33:23   作者:檸萌草的味道  
本篇文章主要介紹了Android實(shí)現(xiàn)圖片壓縮示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

核心思想是通過BitmapFactory.Options來縮放圖片,主要是用到了它的inSampleSize參數(shù)(采樣率)

當(dāng)inSampleSize為1的時(shí)候,采樣后的圖片大小為圖片的原始大??;

當(dāng)inSampleSize為2的時(shí)候,采樣后的圖片的寬和高是原來的1/2,也就是說,它的像素點(diǎn)是原來的1/4,占的內(nèi)存自然就是原來的1/4了。以此類推。

當(dāng)inSampleSize小于1的時(shí)候,效果和等于1的時(shí)候是一樣的。

壓縮流程如下:

1.BitmapFactory.Options 的inJustDecodeBounds參數(shù)設(shè)置為true(這個(gè)時(shí)候BitmapFactory只是解析圖片的原始寬高,并不會(huì)去加載圖片)。

2.從BitmapFactory.Options 中取出圖片的原始寬高,outWidth,outHeight。

3.根據(jù)自己的需要設(shè)置合適的采樣率。

4.BitmapFactory.Options 的inJustDecodeBounds參數(shù)設(shè)置為false,然后就可以加載圖片了。

下面我們看代碼:

public Bitmap decodeSampledBitmapFromBytes(byte[] bytes,int reqWidth,int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
  }


public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
    if(reqWidth == 0 || reqHeight == 0){
      return 1;
    }
    final int width = options.outWidth;
    final int height = options.outHeight;
    int inSampleSize = 1;
    if( width > reqWidth || height > reqHeight){
      final int halfWidth = width / 2;
      final int halfHeight = height / 2;
      while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight){
        inSampleSize *=2;
      }
    }
    return inSampleSize;
  }

如此一來,就完成了一張圖片的壓縮。另外,BitmapFactory還有其它的decode方法,我們也可以仿照上面的來寫。

public Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res,resId,options);
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res,resId,options);
  }
public Bitmap decodeSampledBitmapFromDescrptor(FileDescriptor fd,int reqWidth,int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fd,null,options);
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFileDescriptor(fd,null,options);
  }

接下來結(jié)合一個(gè)小demo來實(shí)現(xiàn)一個(gè)完整的流程

先把圖片壓縮類封裝起來

public class ImageResizer {
  private static final String TAG = "ImageResizer";

  public ImageResizer(){}

  public Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res,resId,options);
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res,resId,options);
  }

  public Bitmap decodeSampledBitmapFromBytes(byte[] bytes,int reqWidth,int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap a = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
    Log.d(TAG, "before bitmap : " + a.getRowBytes() * a.getHeight());
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;
    Bitmap b = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
    Log.d(TAG, "after bitmap : " + b.getRowBytes() * b.getHeight());
    return b;
  }

  public Bitmap decodeSampledBitmapFromDescrptor(FileDescriptor fd,int reqWidth,int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fd,null,options);
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFileDescriptor(fd,null,options);
  }

  public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
    if(reqWidth == 0 || reqHeight == 0){
      return 1;
    }
    final int width = options.outWidth;
    final int height = options.outHeight;
    int inSampleSize = 1;
    if( width > reqWidth || height > reqHeight){
      final int halfWidth = width / 2;
      final int halfHeight = height / 2;
      while ((halfWidth / inSampleSize) >= reqWidth && (halfHeight / inSampleSize) >= reqHeight){
        inSampleSize *=2;
      }
    }
    return inSampleSize;
  }
}

然后就可以拿來用了:

activity_main2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main2"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.yuan.test.Main2Activity">

  <ImageView
    android:id="@+id/main2Iv"
    android:layout_width="200dp"
    android:layout_height="200dp" />

</RelativeLayout>

Main2Activity.Java

public class Main2Activity extends AppCompatActivity {

  private ImageView iv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    testHttp(iv);
  }

  private void testHttp(final ImageView iv) {

    new Thread(new Runnable() {
      @Override
      public void run() {
        String urlString = "https://static.pexels.com/photos/295818/pexels-photo-295818.jpeg";
        HttpURLConnection urlConnection = null;
        InputStream in = null;
        ByteArrayOutputStream outStream = null;
        try {
          URL url = new URL(urlString);
          urlConnection = (HttpURLConnection) url.openConnection();
          in = urlConnection.getInputStream();
          byte[] buffer = new byte[1024];
          int len;
          outStream = new ByteArrayOutputStream();
          while ((len = in.read(buffer)) != -1){
            outStream.write(buffer,0,len);
          }
          final byte[] data = outStream.toByteArray();
          runOnUiThread(new Runnable() {
            @Override
            public void run() { //在主線程加載UI
              iv.setImageBitmap(new ImageResizer().decodeSampledBitmapFromBytes(data,200,200));
            }
          });
        } catch (IOException e) {
          e.printStackTrace();
        }finally {
          try {
            if(urlConnection !=null){
              urlConnection.disconnect();
            }
            if(in != null){
              in.close();
            }
            if(outStream != null){
              outStream.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }).start();
  }

  private void initView() {
    iv = (ImageView) findViewById(R.id.main2Iv);
  }
}

最后記得獲取網(wǎng)絡(luò)權(quán)限

運(yùn)行的結(jié)果:

壓縮前后的bitmap大小對(duì)比

壓縮前后的bitmap大小對(duì)比

壓縮前后的bitmap大小對(duì)比

這里寫圖片描述 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • flutter 實(shí)現(xiàn)多布局列表的示例代碼

    flutter 實(shí)現(xiàn)多布局列表的示例代碼

    這篇文章主要介紹了flutter 實(shí)現(xiàn)多布局列表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Android軟鍵盤彈出時(shí)的界面控制方法

    Android軟鍵盤彈出時(shí)的界面控制方法

    這篇文章主要介紹了Android軟鍵盤彈出時(shí)的界面控制方法,結(jié)合實(shí)例形式分析了Android軟鍵盤彈出后的三種模式,涉及Android針對(duì)AndroidManifet.xml的修改技巧,需要的朋友可以參考下
    2016-08-08
  • Android開發(fā)環(huán)境搭建

    Android開發(fā)環(huán)境搭建

    本文詳細(xì)介紹了Android開發(fā)環(huán)境搭建,十分的詳盡,圖文并茂,有需要的小伙伴參考下。
    2015-01-01
  • Android獲取高清app圖標(biāo)代碼分享

    Android獲取高清app圖標(biāo)代碼分享

    這篇文章主要為大家分享了Android獲取高清app圖標(biāo)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁

    Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁

    這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)第一次啟動(dòng)引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android自定義進(jìn)度條的圓角橫向進(jìn)度條實(shí)例詳解

    Android自定義進(jìn)度條的圓角橫向進(jìn)度條實(shí)例詳解

    本文通過實(shí)例代碼給大家詳細(xì)介紹了Android自定義進(jìn)度條的圓角橫向進(jìn)度條的相關(guān)資料。非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-09-09
  • Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法

    Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)兩個(gè)Activity相互切換而不使用onCreate()的方法,結(jié)合實(shí)例形式分析了多個(gè)Activity切換而不重新創(chuàng)建的操作技巧,需要的朋友可以參考下
    2017-01-01
  • 基于Flutter實(shí)現(xiàn)多邊形和多角星組件

    基于Flutter實(shí)現(xiàn)多邊形和多角星組件

    開發(fā)中,免不了會(huì)用到多邊形、多角星等圖案,比較常用的多邊形比如雷達(dá)圖、多角星比如評(píng)價(jià)星級(jí)的五角星等,本文章就使用Flutter繪制封裝一個(gè)這樣的組件,需要的可以參考一下
    2022-05-05
  • Android實(shí)現(xiàn)簡(jiǎn)單的banner輪播圖

    Android實(shí)現(xiàn)簡(jiǎn)單的banner輪播圖

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的banner輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • android控制密碼顯示與隱藏的方法

    android控制密碼顯示與隱藏的方法

    這篇文章主要為大家詳細(xì)介紹了android控制密碼顯示與隱藏的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評(píng)論

富川| 招远市| 钦州市| 灵丘县| 磴口县| 陈巴尔虎旗| 屏边| 兴海县| 黑水县| 连云港市| 黔南| 石河子市| 荥阳市| 镇原县| 南汇区| 子长县| 伊吾县| 将乐县| 韶山市| 乌审旗| 尚志市| 天柱县| 建宁县| 永胜县| 朝阳市| 芦山县| 文昌市| 琼海市| 应城市| 都匀市| 麻阳| 班玛县| 泸西县| 呼和浩特市| 韶关市| 历史| 太白县| 宁津县| 石台县| 怀仁县| 凌云县|