ITPub博客

首页 > IT职业 > IT图书 > 图片二次采样简单示例

图片二次采样简单示例

IT图书 作者:funnyok 时间:2021-09-09 09:59:49 0 删除 编辑

布局文件:

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

   >

   

        android:id="@+id/iv"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

       />

MainActivit.java

package com.example.day025_exloadertwince;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.view.Menu;

import android.widget.ImageView;

public class MainActivity extends Activity {

    ImageView iv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//初始化控件

iv=(ImageView) findViewById(R.id.iv);

//获取屏幕宽度和高度

int width=getResources().getDisplayMetrics().widthPixels;

int height=getResources().getDisplayMetrics().heightPixels;

//二次采样解析

Bitmap bm=getBitmap(width, height, R.drawable.oom);

iv.setImageBitmap(bm);

}

   

public Bitmap getBitmap(int newWidth,int newHeight,int imageId){

Bitmap bm=null;

BitmapFactory.Options opt=new BitmapFactory.Options();

opt.inJustDecodeBounds=true;

BitmapFactory.decodeResource(getResources(), imageId,opt);

int width=opt.outWidth;

int height=opt.outHeight;

int scaleX=width/newWidth;

int scaleY=height/newHeight;

int scale=scaleX>scaleY?scaleX:scaleY;

opt.inJustDecodeBounds=false;

opt.inSampleSize=scale;

              bm= BitmapFactory.decodeResource(getResources(),imageId,opt);

return bm;

}

}

原文链接:http://www.apkbus.com/blog-813041-61335.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3137/viewspace-2814829/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-07

  • 博文量
    95
  • 访问量
    238382