ImageView实现图片旋转和缩放功能
2014-03-28 23:42 kingshow 阅读(1274) 评论(0) 编辑 收藏 举报
在开发中实现对图像的缩放有很多方法,最简单的方法是改变ImageView控件的大小,我们只要将<ImageView>标签的android:scaleType的属性值设置为fitCenter,要是想实现图像的旋转可以使用android.graphics.Matirx类的setRotate来实现。
下面介绍,图片的旋转和缩放功能。
一、建立工程,如图
二、activity_main.xml中代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imageview" android:layout_width="200dp" android:layout_height="150dp" android:src="@drawable/background" android:scaleType="fitCenter" > </ImageView> <TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="图像宽度:240 图像高度:160" /> <SeekBar android:id="@+id/seekbar1" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:max="240" android:progress="120" /> <TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="0度" /> <SeekBar android:id="@+id/seekbar2" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:max="360" /> </LinearLayout>
三、MainActivity.java中代码
package com.study.rotateimageview; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.util.DisplayMetrics; import android.view.Menu; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity implements OnSeekBarChangeListener{ private int minWidth = 80; private ImageView imageView; private TextView textView1,textView2; private Matrix matrix = new Matrix(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView)this.findViewById(R.id.imageview); SeekBar seekBar1 = (SeekBar)this.findViewById(R.id.seekbar1); SeekBar seekBar2 = (SeekBar)this.findViewById(R.id.seekbar2); textView1 = (TextView)this.findViewById(R.id.textview1); textView2 = (TextView)this.findViewById(R.id.textview2); seekBar1.setOnSeekBarChangeListener(this); seekBar2.setOnSeekBarChangeListener(this); // DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); seekBar1.setMax(dm.widthPixels - minWidth); } @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; } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(seekBar.getId() == R.id.seekbar1){ int newWidth = progress + minWidth; int newHeight = (int)(newWidth*3/4);//按照原比例缩放 imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight)); textView1.setText("图像宽度:" + newWidth + " 图像高度:" + newHeight); }else if(seekBar.getId() == R.id.seekbar2){ Bitmap bitmap = ((BitmapDrawable)(getResources().getDrawable(R.drawable.background))).getBitmap(); matrix.setRotate(progress); bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); imageView.setImageBitmap(bitmap); textView2.setText(progress + "度"); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }
四、效果图
缩放:
旋转: