Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)

简介: Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)

运行有问题或需要源码请点赞关注收藏后评论区留言~~~

一、对图片进行简单加工

Android限制了大图的加载,这样可以避免大图导致系统卡顿,如果想解决超大图片的问题,可行方案如下

1:再显示图片之前调用setLayerType方法 将图层类型设置为软件加速,此时系统会对该视图关闭硬件加速

2:把图像视图的缩放类型改为center,表示保持图片的原尺寸并且居中显示,由于该类型不必缩放图片 也就无须加载图片的所有像素点

3:缩放类型保持fitCenter 同时事先缩小位图的尺寸,直至新位图的宽高均不超过4096 才再图像视图上显示新位图

以上三种 方法,唯有第三种方式兼顾了功能与性能上的需求 比较实用

然而通过系统 相机拍照之后,只能得到高清大图的路径对象,并非位图对象,得从大图路径获取它得位图对象才行 分成以下两步

1:调用内容 解析器得openInputStream方法 打开指定得路径对象  获得输入流对象

2:调用位图工厂得BitmapFactory得decodeStream方法,从输入流解码得到原始的位图

效果如下  可以调整不同的放缩比例以及旋转角度得到图片的不同 样式

代码如下

Java类

package com.example.chapter13;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import com.example.chapter13.util.BitmapUtil;
import java.io.InputStream;
public class ImageChangeActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String TAG = "ImageChangeActivity";
    private ImageView iv_photo; // 声明一个图像视图对象
    private Bitmap mBitmap; // 声明一个位图对象
    private Uri mImageUri; // 图片的路径对象
    private int CHOOSE_CODE = 3; // 选择照片的请求码
    private float mDegree; // 旋转角度
    private float mRatio; // 缩放比例
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_change);
        iv_photo = findViewById(R.id.iv_photo);
        findViewById(R.id.btn_choose).setOnClickListener(this);
        initScaleSpinner(); // 初始化缩放比率下拉框
        initRotateSpinner(); // 初始化旋转角度下拉框
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_choose) {
            // 创建一个内容获取动作的意图(准备跳到系统相册)
            Intent albumIntent = new Intent(Intent.ACTION_GET_CONTENT);
            albumIntent.setType("image/*"); // 设置内容类型为图像
            startActivityForResult(albumIntent, CHOOSE_CODE); // 打开系统相册
        }
    }
    // 初始化缩放比率下拉框
    private void initScaleSpinner() {
        ArrayAdapter<String> scaleAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, scaleArray);
        Spinner sp_scale = findViewById(R.id.sp_scale);
        sp_scale.setPrompt("请选择缩放比率");
        sp_scale.setAdapter(scaleAdapter);
        sp_scale.setOnItemSelectedListener(new ScaleSelectedListener());
        sp_scale.setSelection(0);
    }
    private String[] scaleArray = {"1.0", "0.5", "0.2", "0.10", "0.05", "0.01"};
    class ScaleSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            mRatio = Float.parseFloat(scaleArray[arg2]);
            showChangedImage(); // 显示变更后的图像
        }
        public void onNothingSelected(AdapterView<?> arg0) {}
    }
    // 初始化旋转角度下拉框
    private void initRotateSpinner() {
        ArrayAdapter<String> rotateAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, rotateArray);
        Spinner sp_rotate = findViewById(R.id.sp_rotate);
        sp_rotate.setPrompt("请选择旋转角度");
        sp_rotate.setAdapter(rotateAdapter);
        sp_rotate.setOnItemSelectedListener(new RotateSelectedListener());
        sp_rotate.setSelection(0);
    }
    private String[] rotateArray = {"0", "45", "90", "135", "180", "225", "270", "315"};
    class RotateSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            mDegree = Integer.parseInt(rotateArray[arg2]);
            showChangedImage(); // 显示变更后的图像
        }
        public void onNothingSelected(AdapterView<?> arg0) {}
    }
    // 显示变更后的图像
    private void showChangedImage() {
        if (mBitmap != null) {
            // 获得缩放后的位图对象
            Bitmap bitmap = BitmapUtil.getScaleBitmap(mBitmap, mRatio);
            // 获得旋转后的位图对象
            bitmap = BitmapUtil.getRotateBitmap(bitmap, mDegree);
            iv_photo.setImageBitmap(bitmap); // 设置图像视图的位图对象
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK && requestCode == CHOOSE_CODE) {
            if (intent.getData() != null) { // 从相册选择一张照片
                mImageUri = intent.getData();
                // 打开指定uri获得输入流对象
                try (InputStream is = getContentResolver().openInputStream(mImageUri)) {
                    // 从输入流解码得到原始的位图对象
                    mBitmap = BitmapFactory.decodeStream(is);
                    iv_photo.setImageBitmap(mBitmap); // 设置图像视图的位图对象
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "uri.getPath="+mImageUri.getPath()+",uri.toString="+mImageUri.toString());
            }
        }
    }
}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btn_choose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="打开相册选取照片"
        android:textColor="@color/black"
        android:textSize="17sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_marginLeft="5dp" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缩放比率:"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <Spinner
            android:id="@+id/sp_scale"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:spinnerMode="dialog" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转角度:"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <Spinner
            android:id="@+id/sp_rotate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:spinnerMode="dialog" />
    </LinearLayout>
    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="center" />
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
1月前
|
Android开发 开发者 UED
个人开发 App 成功上架手机应用市场的关键步骤
个人开发 App 成功上架手机应用市场的关键步骤
|
1月前
|
开发工具 数据安全/隐私保护 Android开发
【教程】APP 开发后如何上架?
【教程】APP 开发后如何上架?
|
26天前
|
移动开发
uni-app使用v-html输出富文本图片溢出解决
uni-app使用v-html输出富文本图片溢出解决
39 1
|
17天前
|
监控 数据可视化 安全
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
环境实时数据、动态监测报警,实时监控施工环境状态,有针对性地预防施工过程中的环境污染问题,打造文明生态施工,创造绿色的生态环境。
14 0
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
|
25天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
29天前
|
Java Android开发
Android Studio的使用导入第三方Jar包
Android Studio的使用导入第三方Jar包
12 1
|
1月前
|
Java Android开发 开发者
【Uniapp开发】APP的真机调试指南,从开发到上架全过程
【Uniapp开发】APP的真机调试指南,从开发到上架全过程
36 3
游戏直播APP平台开发多少钱成本:定制与成品源码差距这么大
开发一款游戏直播APP平台所需的费用是多少?对于计划投身这一领域的投资者来说,首要关心的问题之一就是。本文将探讨两种主要的开发模式——定制开发与成品源码二次开发的成本差异及其优劣势。
|
1月前
|
传感器 人工智能 数据可视化
Java智慧工地监管一体化云平台APP源码 SaaS模式
高支模监测:高支模立杆及倾斜角度,高支模立杆的荷载,架体的水平位移以及模板沉降情况,当检测数据超过预警值时,实时报警。
32 2