android之利用滑动条控制图片的缩放和旋转

用seekBar来控制一个图像的缩放大小和旋转度数,在网上查了很多资料,发现都不是我想要的效果,于是自己写了一个简单易懂的例子

1.首先,图片的缩放和旋转不能改变除它之外的布局,所以布局要选用相对布局,下面是布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:scaleType="fitCenter"
        android:background="#ffffff"
        android:src="@drawable/ic_haimian" />


    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/tv2"
        android:layout_marginTop="10dp"
        android:text="图像宽度:200"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/sb1"
        android:layout_marginTop="10dp"
        android:text="图像高度:200"
        android:textSize="20dp" />

    <SeekBar
        android:id="@+id/sb1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_above="@id/tv3"
        android:layout_marginTop="10dp"
        android:max="400"
        android:progress="200" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/sb2"
        android:layout_marginTop="10dp"
        android:text="0度"
        android:textSize="20dp" />

    <SeekBar
        android:id="@+id/sb2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="50dp"
        android:max="360" />


</RelativeLayout>

布局截图,我选择的图片是正方形图片,请自行更改

2.Activity代码:

package com.example.kh5;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorSpace;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private ImageView iv;
    private TextView tv1,tv2,tv3;
    private Matrix matrix;
    private Bitmap bitmap;
    private static float scale=1;//保存缩放比列
    private static float rotate=0;//保存旋转度数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //图片,文本对象
        iv = (ImageView)this.findViewById(R.id.iv);
        tv1 = (TextView)this.findViewById(R.id.tv1);
        tv2 = (TextView)this.findViewById(R.id.tv2);
        tv3 = (TextView)this.findViewById(R.id.tv3);
        //矩阵对象,用于旋转
        matrix=new Matrix();
        //滑动条对象
        SeekBar sb1 = (SeekBar)this.findViewById(R.id.sb1);
        SeekBar sb2 = (SeekBar)this.findViewById(R.id.sb2);
        //给滑动条设置监听,自我监听
        sb1.setOnSeekBarChangeListener(this);
        sb2.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//进度条发生改变的时候会触发;
        //三个参数:(发生改变的滑动条的对象,滑动条当前的值,是否是用户在操作)
        if (fromUser){//是用户在操作才能执行以下内容
            if(seekBar.getId() == R.id.sb1){//缩放滑动条
                //缩放的长和宽的比列
                float w=(float)progress/200;
                scale=w;//保存缩放比例
                //设置矩阵的缩放比列,并继承上次旋转度数
                matrix.setScale(w, w);
                matrix.postRotate(rotate);
                //控制最大最小缩放倍数,避免过大过小手机崩溃
                if (w<0.2){
                    w=0.2f;
                }
                if (w>1.8){
                    w=1.8f;
                }
                //获取图像对象
                bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_haimian);
                //将位图缩放,保留之前旋转度数
                bitmap = bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
                //将图像改变
                iv.setImageBitmap(bitmap);
                //改变文本的值
                tv1.setText("图像宽度:" + progress);
                tv2.setText("图像高度:" + progress);
            }else if(seekBar.getId() == R.id.sb2){//旋转滑动条
                rotate=(float)progress;//保存旋转比列
                //设置旋转度数,继承之前缩放大小
                matrix.setRotate(progress);
                matrix.postScale(scale,scale);
                //获取图像对象
                bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_haimian);
                //将位图旋转
                bitmap = bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
                //将图像改变
                iv.setImageBitmap(bitmap);
                //改变文本的值
                tv3.setText(progress + "度");
            }
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {//进度条按下去的时候会触发

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {//进度条松开的时候会触发

    }
}

结果截图:

好,这就是所有的代码,实现了同时响应两种操作——缩放和旋转,是比较令我满意和清楚的答案了

依靠的是martix矩阵实现bitmap的缩放和旋转,如果对这两个类不了解的童鞋,请站在如下巨人的肩膀上:

Matrix知识学习

深入理解Android Bitmap的各种操作

发布了13 篇原创文章 · 获赞 14 · 访问量 990

猜你喜欢

转载自blog.csdn.net/qq_42495847/article/details/104535536
今日推荐