需求面熟:ImageView.setImageBitmap设置较大的图片时会报错,安卓对图片的大小进行了限制。

参考文章: Android中高效的显示图片 - 加载大图(setImageBitmap)

效果展示
项目结构 效果展示
示例代码
  • MainActivity
package com.example.www.copyapp;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Point;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private ImageView mShowImg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mShowImg = (ImageView) findViewById(R.id.imageView);    }    public void copyImg(View view) {        WindowManager systemService = (WindowManager) getSystemService(WINDOW_SERVICE);        Point point = new Point();        systemService.getDefaultDisplay().getSize(point);        mShowImg.setImageBitmap(decodeSampledBitmapFromResource(this.getResources(),R.drawable.angel, point.x, point.y));    }    /***     *  加载缩小比例的图片     * @param res     * @param resId     * @param reqWidth     * @param reqHeight     * @return     */    public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {        // First decode with inJustDecodeBounds=true to check dimensions        final BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeResource(res, resId, options);        // Calculate inSampleSize        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);        // Decode bitmap with inSampleSize set        options.inJustDecodeBounds = false;        return BitmapFactory.decodeResource(res, resId, options);    }    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {        // Raw height and width of image        final int height = options.outHeight;        final int width = options.outWidth;        int inSampleSize = 1;        if (height > reqHeight || width > reqWidth) {            final int halfHeight = height / 2;            final int halfWidth = width / 2;            inSampleSize *= 2;            // Calculate the largest inSampleSize value that is a power of 2 and keeps both            // height and width larger than the requested height and width.            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {                inSampleSize *= 2;            }        }        return inSampleSize;    }}
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginStart="8dp"        android:layout_marginTop="8dp"        android:layout_marginEnd="8dp"        android:text="@android:string/copy"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        android:onClick="copyImg"/>    <ImageView        android:id="@+id/imageView"        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_marginStart="8dp"        android:layout_marginTop="8dp"        android:layout_marginEnd="8dp"        android:layout_marginBottom="8dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/button"        app:srcCompat="@android:color/white" />android.support.constraint.ConstraintLayout>
  • AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.www.copyapp">    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        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>

更多相关文章

  1. Android(安卓)ListView 下拉刷新 上拉更多[实例]
  2. android 图片处理大全
  3. android webview快速获取页面上的图片
  4. Android实现 ScrollView + ListView无滚动条滚动
  5. Android(安卓)手势 正则匹配图片
  6. android 读取电话本,并加载到list相关
  7. Android用ImageView显示本地和网上的图片
  8. Ubuntu 17.10 Android(安卓)studio 3.1 下的AVD配置
  9. Android(安卓)调用系统相册选择照片与调用系统相机拍照保存照片

随机推荐

  1. 【第417期】记忆前端早读课
  2. 2021-1-18
  3. 活动|坚持21天阅读分享
  4. 【招聘】杭州尚妆网招前端
  5. 【招聘】杭州上海蚂蚁金服招前端
  6. 专访|腾讯微信支付设计中心重构负责人@Gho
  7. 【招聘】广州百田招前端
  8. 数据库中间件 Sharding-JDBC 源码分析 —
  9. 招聘|北京字节跳动科技有限公司
  10. 【招聘】杭州蘑菇街招高级前端