欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android UI之ImageView實現(xiàn)圖片旋轉(zhuǎn)和縮放

 更新時間:2021年09月23日 09:41:54   投稿:lijiao  
這篇文章主要介紹了Android UI之ImageView實現(xiàn)圖片旋轉(zhuǎn)和縮放的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這一篇,給大家介紹一下ImageView控件的使用,ImageView主要是用來顯示圖片,可以對圖片進行放大、縮小、旋轉(zhuǎn)的功能。

android:sacleType屬性指定ImageVIew控件顯示圖片的方式,例如:center表示圖像以不縮放的方式顯示在ImageView控件的中心,如果設置為fitCenter,表示圖像按照比例縮放至合適的位置,并在ImageView控件的中心。

首先我們開發(fā)一個簡單的案例,實現(xiàn)圖片的放大縮小和旋轉(zhuǎn):

先看看實現(xiàn)的效果:

縮放截圖1:

縮放截圖2:

旋轉(zhuǎn)截圖1:

旋轉(zhuǎn)截圖2:

在實現(xiàn)圖片的縮放和旋轉(zhuǎn)時,我們都需要用到android.graphics.Matrix這個類,對于Matrix在API中的介紹如下:
Class Overview
The Matrix class holds a 3x3 matrix for transforming coordinates. Matrix does not have a constructor, so it must be explicitly initialized using either reset() - to construct an identity matrix, or one of the set..() functions (e.g. setTranslate, setRotate, etc.).

本實例中使用到android.graphics.Matrix的 setRotate方法來設置旋轉(zhuǎn)角度,以下是API中的該方法介紹:

void setRotate(float degrees, float px, float py)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

源代碼:

MainActivity.java

[html] view plaincopyprint?
package com.imageview.activity; 
 
import com.imageview.activity.R; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Matrix; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
 
public class MainActivity extends Activity implements OnSeekBarChangeListener { 
 private int minWidth = 80; 
 private ImageView imageView; 
 private SeekBar seekBar1; 
 private SeekBar seekBar2; 
 private Matrix matrix = new Matrix(); 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  imageView = (ImageView) findViewById(R.id.imageview1); 
  seekBar1 = (SeekBar) findViewById(R.id.seekbar1); 
  seekBar2 = (SeekBar) findViewById(R.id.seekbar2); 
  seekBar1.setOnSeekBarChangeListener(this); 
  seekBar2.setOnSeekBarChangeListener(this); 
  // 定義一個DisplayMetrics對象,用來顯示旋轉(zhuǎn)的圖像 
  DisplayMetrics dm = new DisplayMetrics(); 
  // 根據(jù)手機屏幕大小來縮放 
  getWindowManager().getDefaultDisplay().getMetrics(dm); 
  seekBar1.setMax(dm.widthPixels - minWidth); 
 } 
 
 @Override 
 public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { 
  switch (seekBar.getId()) { 
  case R.id.seekbar1: 
   int newWidth = progress + minWidth; 
   int newHeight = (int) (newWidth * 3 / 4); 
   imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight)); 
   break; 
  case R.id.seekbar2: 
   Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.pic)).getBitmap(); 
   // 設置旋轉(zhuǎn)角度 
   matrix.setRotate(progress); 
   // 重新繪制Bitmap 
   bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true); 
   imageView.setImageBitmap(bitmap); 
   break; 
  } 
 } 
 
 @Override 
 public void onStartTrackingTouch(SeekBar seekBar) { 
 
 } 
 
 @Override 
 public void onStopTrackingTouch(SeekBar seekBar) { 
 
 } 
}

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
 <ImageView 
  android:layout_width="200dp" 
  android:layout_height="150dp" 
  android:scaleType="fitCenter" 
  android:background="#FFFFFF" 
  android:src="@drawable/pic" 
  android:id="@+id/imageview1"/> 
 <SeekBar 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="100" 
  android:id="@+id/seekbar1"/> 
 <TextView 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="拖動來縮放圖片" /> 
 <SeekBar 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="100" 
  android:id="@+id/seekbar2"/> 
 <TextView 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="拖動來旋轉(zhuǎn)圖片" /> 
</LinearLayout> 

最后說明一點,要在ImageView中顯示的圖片進行旋轉(zhuǎn),請選擇一張符合Matrix的3*3矩陣的圖片,否則在旋轉(zhuǎn)過程中超過屏幕寬度會引起報錯,本例中選取的是一張正方形的圖片,如果是長方形的建議做一下代碼邏輯判斷處理。

以上就是關于Matrix的實現(xiàn)效果,希望對大家的學習有所幫助。

相關文章

  • Flutter應用框架搭建實現(xiàn)屏幕適配方案詳解

    Flutter應用框架搭建實現(xiàn)屏幕適配方案詳解

    移動設備多樣性,特別是Android的碎片化嚴重,存在各種各樣的分辨率,flutter跨平臺開發(fā)又需要同時支持Android和IOS,為盡可能的還原設計圖效果提升用戶的體驗,根據(jù)設計稿設計屏幕ui的時候我們需要考慮到屏幕適配的問題
    2022-11-11
  • Kotlin高階函數(shù)reduce與fold使用實例

    Kotlin高階函數(shù)reduce與fold使用實例

    Kotlin的高階函數(shù)reduce和fold可以用來對集合進行聚合操作。reduce函數(shù)將集合元素逐個累加,而fold函數(shù)則可以指定一個初始值進行累加。這兩個函數(shù)在處理大數(shù)據(jù)集時非常有用
    2023-04-04
  • Kotlin 協(xié)程 supervisorScope {} 運行崩潰解決方法

    Kotlin 協(xié)程 supervisorScope {} 運行崩潰解決方法

    看過很多?supervisorScope {}?文檔的使用,我照抄一摸一樣的代碼,運行就崩潰,最后找到了解決方法,應該是kotlin版本更新做過改動,當前我使用的是?androidx.core:core-ktx:1.9.0,本文給大家介紹Kotlin 協(xié)程 supervisorScope {} 運行崩潰解決方法,感興趣的朋友一起看看吧
    2024-01-01
  • 詳解Android studio 動態(tài)fragment的用法

    詳解Android studio 動態(tài)fragment的用法

    這篇文章主要介紹了Android studio 動態(tài)fragment的用法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Android XMPP通訊自定義Packet&Provider

    Android XMPP通訊自定義Packet&Provider

    這篇文章主要介紹了Android XMPP通訊自定義Packet&Provider的相關資料,需要的朋友可以參考下
    2016-08-08
  • 淺談Android View滑動沖突的解決方法

    淺談Android View滑動沖突的解決方法

    本篇文章主要介紹了淺談Android View滑動沖突的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • android?studio后臺服務使用詳解

    android?studio后臺服務使用詳解

    這篇文章主要為大家詳細介紹了android?studio后臺服務,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android 網(wǎng)絡圖片查看器與網(wǎng)頁源碼查看器

    Android 網(wǎng)絡圖片查看器與網(wǎng)頁源碼查看器

    本篇文章主要介紹了Android 網(wǎng)絡圖片查看器與網(wǎng)頁源碼查看器的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Android編程防止進程被第三方軟件殺死的方法

    Android編程防止進程被第三方軟件殺死的方法

    這篇文章主要介紹了Android編程防止進程被第三方軟件殺死的方法,涉及Android進程操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android百度定位導航之基于百度地圖移動獲取位置和自動定位

    Android百度定位導航之基于百度地圖移動獲取位置和自動定位

    項目需求是這樣的,首先定位我當前的起始位置,并跟隨移動不斷自動定位我的當前位置,下面通過本文給大家介紹android百度定位導航之基于百度地圖移動獲取位置和自動定位,需要的朋友參考下
    2016-01-01

最新評論