【android】利用滑動條seekBar控制圖片的縮放和旋轉

用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的各種操作

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章