.java代码(实现图片的缩放和旋转):
package com.example.day6;
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.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class image extends Activity implements OnSeekBarChangeListener{
private ImageView image;
private SeekBar sone;
private SeekBar stwo;
private Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
image=(ImageView) findViewById(R.id.image);
sone=(SeekBar) findViewById(R.id.sone);
stwo=(SeekBar) findViewById(R.id.stwo);
// 设置SeekBar的progress值改变的监听事件
sone.setOnSeekBarChangeListener(this);
stwo.setOnSeekBarChangeListener(this);
matrix=new Matrix(); // Matrix类 可实现图片随意缩放,拖动,翻页
//获取屏幕的宽度
DisplayMetrics dis= new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dis);
//设置seekBar的最大progress值
sone.setMax(dis.widthPixels);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
switch (seekBar.getId()) { // 获得Id
case R.id.sone: //放大或缩小图片
int width=progress;
int height=progress*3/4;
image.setLayoutParams(new LinearLayout.LayoutParams(width, height));
break;
case R.id.stwo: //旋转图片
matrix.setRotate(progress); // Matrix类中的setRotate方法
// 获取图片
Bitmap bit = ((BitmapDrawable)getResources().getDrawable(R.drawable.one)).getBitmap();
// 获取旋转操作后的图片
Bitmap bitmap = bit.createBitmap(bit, 0, 0, bit.getWidth(), bit.getHeight(), matrix, true);
// 设置
image.setImageBitmap(bitmap);
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="300dip"
android:src="@drawable/one"
android:scaleType="fitCenter"/>
<SeekBar
android:id="@+id/sone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="100"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/one"
android:text="拖动来缩放图片"/>
<SeekBar
android:id="@+id/stwo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/two"
android:text="拖动来旋转图片"/>
</LinearLayout>