继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

图片二次采样简单示例

万千封印
关注TA
已关注
手记 90
粉丝 13
获赞 64

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

   >

    <ImageView

        android:id="@+id/iv"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

       />

</LinearLayout>

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);

}

    /**

     * 二次采样解析方法

     * @param newWidth 屏幕宽度

     * @param newHeight屏幕高度

     * @param imageId图片id

     * @return  返回bitmap

     */

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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP