国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android UI之ImageView實現圖片旋轉和縮放

2020-04-11 11:26:47
字體:
來源:轉載
供稿:網友

這一篇,給大家介紹一下ImageView控件的使用,ImageView主要是用來顯示圖片,可以對圖片進行放大、縮小、旋轉的功能。
android:sacleType屬性指定ImageVIew控件顯示圖片的方式,例如:center表示圖像以不縮放的方式顯示在ImageView控件的中心,如果設置為fitCenter,表示圖像按照比例縮放至合適的位置,并在ImageView控件的中心。
首先我們開發一個簡單的案例,實現圖片的放大縮小和旋轉:
先看看實現的效果:
縮放截圖1:

縮放截圖2:

旋轉截圖1:

旋轉截圖2:

在實現圖片的縮放和旋轉時,我們都需要用到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方法來設置旋轉角度,以下是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對象,用來顯示旋轉的圖像   DisplayMetrics dm = new DisplayMetrics();   // 根據手機屏幕大小來縮放   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();    // 設置旋轉角度    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="拖動來旋轉圖片" /> </LinearLayout> 

最后說明一點,要在ImageView中顯示的圖片進行旋轉,請選擇一張符合Matrix的3*3矩陣的圖片,否則在旋轉過程中超過屏幕寬度會引起報錯,本例中選取的是一張正方形的圖片,如果是長方形的建議做一下代碼邏輯判斷處理。
以上就是關于Matrix的實現效果,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武乡县| 郎溪县| 改则县| 四川省| 枞阳县| 玛沁县| 湘潭市| 宁远县| 日喀则市| 台湾省| 巨鹿县| 阳高县| 平利县| 仲巴县| 开江县| 神农架林区| 台北市| 渑池县| 来安县| 遂平县| 邯郸县| 涪陵区| 柏乡县| 温州市| 什邡市| 淮安市| 湘乡市| 阜南县| 阜平县| 永靖县| 长顺县| 土默特左旗| 湘潭市| 都昌县| 鄯善县| 雷州市| 仁寿县| 仁寿县| 桐梓县| 雷州市| 寿宁县|