这里有两个主要的类,Matrix 和 DisaplayMetrics
Matrix 中文是矩阵的意思,用来实现图片的缩放,平移,旋转的。API里解释为:可以用这个类生成一个3 x 3 的一个矩阵,在这个类里面没有一个构造的方法,通过初试化来使用一个 reset(),
DisaplayMetrics:一个构造描述的一般信息显示,比如大小,密度,和字体缩放。也就是说这个图像缩放的功能,要按照这个屏幕的宽度来缩放。
因为有能屏幕的限制,然后呢,就需要我们自己去重新定义一个长度和宽度。
我们先在layout.xml文件下定义好两个SeekBar,然后这里我们定义了一个ImageView的图片,这里我们自己设置高度,我设置的宽度200高度150,比例是4:3.
然后在MainAcitivity.java文件下,我们用监听器setOnSeekBarChangeListener,去更改图片的大小,在监听器里面,我们自己要重新定义两个整形变量,一个是图片的高度,一个是图片的宽度,
这里又需要用到Bitmap这个类,Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。
layout.xml代码
<LinearLayout 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:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="150dp"
android:src="@drawable/w1"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="图像宽度:240 图像高度:150" />
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:max="240"
android:progress="120" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="0度" />
<SeekBar
android:id="@+id/seekbar2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:max="360" />
</LinearLayout>
、
MainActivity.java代码
package com.example.ll_imgeview_turn;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.R.integer;
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.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private int minWidth = 80;
private ImageView imageView;
private TextView textView1, textView2;
private SeekBar seekBar1, seekBar2;
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);
textView1 = (TextView) findViewById(R.id.textview1);
textView2 = (TextView) findViewById(R.id.textview2);
seekBar1.setOnSeekBarChangeListener(new seekBar1ChangeListenner());
seekBar2.setOnSeekBarChangeListener(new seekBar2ChangeListenner());
// 一个结构描述的一般信息显示,比如大小、密度、和字体缩放
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
seekBar1.setMax(displayMetrics.widthPixels - minWidth);
}
class seekBar1ChangeListenner implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
int newWidth = progress + minWidth;
int newHeight = (int) (newWidth * 3 / 4);//按照原图片的比例缩放
imageView.setLayoutParams(new LinearLayout.LayoutParams(
newWidth, newHeight));
textView1.setText("图像宽度:" + newWidth + " 图像高度:" + newHeight);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
class seekBar2ChangeListenner implements OnSeekBarChangeListener{
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
Bitmap bitmap = ((BitmapDrawable) (getResources()
.getDrawable(R.drawable.wm))).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
}
}
@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;
}
}