注意:这个程序本人亲自测试运行过的,完美实现了图片缩放和旋转的效果,seekBar1是用来图片缩放的 seekBar2是图片旋转的,image是图片资源文件,可以自己设置
文件一:MainActivity.java
package com.example.zmap;
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.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private int minWidth = 80;
private ImageView image;
private TextView text;
private TextView text2;
private Matrix matrix = new Matrix();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar);
SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2);
text = (TextView) this.findViewById(R.id.text);
text2 = (TextView) this.findViewById(R.id.text2);
seekBar1.setOnSeekBarChangeListener(this);
seekBar2.setOnSeekBarChangeListener(this);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
seekBar1.setMax(dm.widthPixels - minWidth);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (seekBar.getId() == R.id.seekbar) {
int newWidth = progress + minWidth;
int newHeight = (int) (newWidth * 3 / 4);
image.setLayoutParams(new LinearLayout.LayoutParams(newWidth,
newHeight));
text.setText("图像高度: " + newWidth + "图像宽度" + newHeight);
} else if (seekBar.getId() == R.id.seekbar2) {
Bitmap bitmap = ((BitmapDrawable) (getResources()
.getDrawable(R.drawable.gougou))).getBitmap();
matrix.setRotate(progress);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
image.setImageBitmap(bitmap);
text2.setText(progress + "度");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
文件二: activity_main.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="200dp"
android:layout_height="150dp"
android:scaleType="fitCenter"
android:src="@drawable/gougou" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="图像宽度:240 图像高度:160" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:max="240"
android:progress="120" />
<TextView
android:id="@+id/text2"
android:layout_width="match_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:max="360"
android:progress="120" />
</LinearLayout>