Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ImageView 圖片旋轉與縮放實作

編輯:關於 Android 程式設計

ImageView 圖片處理

ImageView 主要用來顯示圖片,也可以搭配 Matrix 做旋轉、縮放等效果。scaleType 屬性則可控制圖片在控制項中的顯示方式。

版面配置

<ImageView android:id="@+id/imageview1"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:scaleType="fitCenter"
    android:background="#FFFFFF"
    android:src="@drawable/pic"/>

<SeekBar android:id="@+id/seekbar1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"/>

<SeekBar android:id="@+id/seekbar2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"/>

縮放與旋轉程式碼

@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.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            imageView.setImageBitmap(bitmap);
            break;
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved