最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

縮放截圖1:

縮放截圖2:

旋轉(zhuǎn)截圖1:

旋轉(zhuǎn)截圖2:

在實(shí)現(xiàn)圖片的縮放和旋轉(zhuǎn)時(shí),我們都需要用到android.graphics.Matrix這個(gè)類,對(duì)于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.).

本實(shí)例中使用到android.graphics.Matrix的 setRotate方法來(lái)設(shè)置旋轉(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); 
  // 定義一個(gè)DisplayMetrics對(duì)象,用來(lái)顯示旋轉(zhuǎn)的圖像 
  DisplayMetrics dm = new DisplayMetrics(); 
  // 根據(jù)手機(jī)屏幕大小來(lái)縮放 
  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(); 
   // 設(shè)置旋轉(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="拖動(dòng)來(lái)縮放圖片" /> 
 <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="拖動(dòng)來(lái)旋轉(zhuǎn)圖片" /> 
</LinearLayout> 

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

以上就是關(guān)于Matrix的實(shí)現(xiàn)效果,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • android dialog背景模糊化效果實(shí)現(xiàn)方法

    android dialog背景模糊化效果實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了android dialog背景模糊化效果的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果

    Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)的具體代碼,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android多線程學(xué)習(xí)實(shí)例詳解

    Android多線程學(xué)習(xí)實(shí)例詳解

    這篇文章主要介紹了Android多線程,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android多線程的概念、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-10-10
  • Android 架構(gòu)之?dāng)?shù)據(jù)庫(kù)框架搭建

    Android 架構(gòu)之?dāng)?shù)據(jù)庫(kù)框架搭建

    這篇文章主要給大家介紹的是Android 架構(gòu)之?dāng)?shù)據(jù)庫(kù)框架搭建,在本篇中,將會(huì)讓你一點(diǎn)一滴從無(wú)到有創(chuàng)建一個(gè)不再為數(shù)據(jù)庫(kù)而煩惱的框架。需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)

    Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)

    這篇文章主要介紹了Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 解析android 流量監(jiān)測(cè)的實(shí)現(xiàn)原理

    解析android 流量監(jiān)測(cè)的實(shí)現(xiàn)原理

    本篇文章是對(duì)android中流量監(jiān)測(cè)的實(shí)現(xiàn)原理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android開發(fā)Jetpack組件LiveData使用講解

    Android開發(fā)Jetpack組件LiveData使用講解

    LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)
    2022-08-08
  • Android常用設(shè)計(jì)模式之原型模式詳解

    Android常用設(shè)計(jì)模式之原型模式詳解

    這篇文章主要為大家介紹了Android常用設(shè)計(jì)模式之原型模式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼

    android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼

    這篇文章主要介紹了android屏蔽按鈕連續(xù)點(diǎn)擊的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Android ListView之setEmptyView正確使用方法

    Android ListView之setEmptyView正確使用方法

    這篇文章主要介紹了Android ListView之setEmptyView正確使用方法的相關(guān)資料,希望通過(guò)本文能幫助到大家使用該方法,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

池州市| 湾仔区| 淮北市| 霸州市| 襄垣县| 苍南县| 沾益县| 峨山| 鲁山县| 芒康县| 岑巩县| 织金县| 喀什市| 天柱县| 当雄县| 成都市| 金昌市| 阿拉善左旗| 上杭县| 曲靖市| 水城县| 林芝县| 建阳市| 常熟市| 巴彦淖尔市| 德庆县| 巴楚县| 会泽县| 措勤县| 潮州市| 三穗县| 佛学| 八宿县| 罗城| 洪雅县| 策勒县| 扎兰屯市| 稷山县| 仁布县| 龙泉市| 鹤庆县|