import
android.support.v7.app.ActionBarActivity;
import
android.util.DisplayMetrics;
import
android.annotation.SuppressLint;
import
android.graphics.Bitmap;
import
android.graphics.Matrix;
import
android.graphics.drawable.BitmapDrawable;
import
android.os.Bundle;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.SeekBar;
import
android.widget.TextView;
import
android.widget.SeekBar.OnSeekBarChangeListener;
@SuppressLint
(
"NewApi"
)
public
class
MainActivity
extends
ActionBarActivity
implements
OnSeekBarChangeListener{
//最小的缩放宽度
private
int
minWidth =
80
;
private
ImageView imageView;
private
SeekBar seekBar1;
private
SeekBar seekBar2;
private
TextView textView1;
private
TextView textView2;
//矩阵类,用于对图像进行旋转
private
Matrix matrix =
new
Matrix();
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView
= (ImageView)findViewById(R.id.imageView);
seekBar1
= (SeekBar)findViewById(R.id.seekBar1);
seekBar2
= (SeekBar)findViewById(R.id.seekBar2);
seekBar1.setOnSeekBarChangeListener(
this
);
seekBar2.setOnSeekBarChangeListener(
this
);
textView1
= (TextView)findViewById(R.id.textview1);
textView2
= (TextView)findViewById(R.id.textview2);
//创建一个空的展示矩阵,用于存储当前屏幕信息,如大小。
DisplayMetrics
displayMetrics =
new
DisplayMetrics();
//把当前管理的屏幕尺寸传递给displayMetrics
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
//设置放大的最大值不超过屏幕大小
seekBar1.setMax(displayMetrics.widthPixels-minWidth);
}
@Override
public
void
onProgressChanged(SeekBar seekBar,
int
progress,
boolean
fromUser) {
if
(seekBar.getId()==R.id.seekBar1) {
//设置宽度为80到最大
int
mwidth = progress+minWidth;
int
mheight = (
int
)(
2
*mwidth/
3
);
//设置imageView的输出大小参数
//因为图片是fitcenter参数,所以图片会随着image框架的大小变化而变化
//从而改变image框架的大小会引起图片大小的变化,实现图片的放大缩小功能
imageView.setLayoutParams(
new
LinearLayout.LayoutParams(mwidth, mheight));
textView1.setText(
"图片宽度:"
+mwidth+
"\t图片高度:"
+mheight);
}
else
if
(seekBar.getId()==R.id.seekBar2) {
//imageView自身的setRotation方法是绕图的中心顺时针防线旋转
//同时该旋转不会改变imageView的输出大小,所超出imageView的部分会照常显示在所跨越视图的下方
//imageView.setRotation(progress);
//如果想创建一个不超出imageView输出框架的旋转图像,那么需要把原图像取出,把原图像进行旋转,重新放入到imageView
中
//缺陷:占用系统较多的内存,会比较卡
Bitmap
bitmap = ((BitmapDrawable)(getResources().getDrawable(R.drawable.photo))).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
}
}
<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:id=
"@+id/imageView"
android:layout_width=
"300dp"
android:layout_height=
"200dp"
android:scaletype=
"fitCenter"
android:src=
"@drawable/photo"/
>
<textview
android:id=
"@+id/textview1"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"图片缩放"/
>
<seekbar
android:id=
"@+id/seekBar1"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:max=
"480"
android:progress=
"300"/
>
<textview
android:id=
"@+id/textview2"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"图片旋转"/
>
<seekbar
android:id=
"@+id/seekBar2"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:max=
"360"
android:progress=
"0"/
>
</linearlayout>
Android----imageView实现图片的旋转和缩放
原文地址:http://blog.csdn.net/u012230055/article/details/44219239