找到一個圖片旋轉的範例,範例是左右旋轉90度,但是當我把旋轉角度改成其他角度時圖片會越轉越小
在ImageView中加入android:scaleType,還是會越轉越小,有什麼解決辦法?
程式碼
.java- package com.example.imagerota;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Matrix;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.view.Menu;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button button_left = (Button)findViewById(R.id.button1);
- View.OnClickListener listener_left = new View.OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- //取得圖片
- ImageView imageView_left = (ImageView) findViewById(R.id.imageView1);
- //imageView_left.setDrawingCacheEnabled(true);
- //建立圖片的緩存,圖片的緩存本身就是一個Bitmap
- imageView_left.buildDrawingCache();
- //取得緩存圖片的Bitmap檔
- Bitmap bmp=imageView_left.getDrawingCache();
- //定義一個矩陣圖
- Matrix m=new Matrix();
- //取得圖片的寬度
- int width=bmp.getWidth();
- //取得圖片的長度
- int height=bmp.getHeight();
- //逆時針旋轉90度
- m.setRotate(-89);
- //產生新的旋轉後Bitmap檔
- Bitmap b=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
-
- //顯示圖片
- imageView_left.setImageBitmap(b);
- }
- };
- button_left.setOnClickListener(listener_left);
-
- //圖片右璇90
- Button button_right = (Button)findViewById(R.id.button2);
- View.OnClickListener listener_right = new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //取得圖片
- ImageView imageView_right = (ImageView) findViewById(R.id.imageView1);
- //imageView_right.setDrawingCacheEnabled(true);
- //建立圖片的緩存,圖片的緩存本身就是一個Bitmap
- imageView_right.buildDrawingCache();
- //取得緩存圖片的Bitmap檔
- Bitmap bmp=imageView_right.getDrawingCache();
- //定義一個矩陣圖
- Matrix m=new Matrix();
- //取得圖片的寬度
- int width=bmp.getWidth();
- //取得圖片的長度
- int height=bmp.getHeight();
- //順時針旋轉90度
- m.postRotate(89);
- //產生新的旋轉後Bitmap檔
- Bitmap b=Bitmap.createBitmap(bmp, 0, 0, width, height, m, true);
-
- //顯示圖片
- imageView_right.setImageBitmap(b);
-
- }
- };
- button_right.setOnClickListener(listener_right);
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
-
- }
複製代碼 .xml- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:scaleType="fitCenter"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:contentDescription="@string/hi"
- android:src="@drawable/hi" />
- <RelativeLayout
- android:id="@+id/relativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </RelativeLayout>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/imageView1"
- android:layout_below="@+id/imageView1"
- android:layout_marginTop="35dp"
- android:text="@string/left" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/button1"
- android:layout_alignBottom="@+id/button1"
- android:layout_alignRight="@+id/relativeLayout1"
- android:layout_marginRight="28dp"
- android:text="@string/right" />
- </RelativeLayout>
複製代碼
ImageRota.rar
(1.57 MB, 下載次數: 0)
|