写一篇文章很辛苦啊!!!
转载请注明,联系请邮件nlp30508@qq.com
我学习Android都是结合源代码去学习,这样比较直观,非常清楚的看清效果,觉得很好,今天的学习源码是网上找的源码 百度搜就知道很多下载的地方 网上源码的名字叫:Android 实现图片的旋转.zip
z直接看代码:
布局文件:
<?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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="逆时针"
android:textSize="18sp" >
</Button>
<Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顺时针"
android:textSize="18sp" >
</Button>
</LinearLayout>
<ImageView
android:id="@+id/myImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/a" >
</ImageView>
</LinearLayout>
效果图:
实现的activity类:
import com.wust.imgrotate.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class DemoActivity extends Activity {
private ImageView mImageView;
private Button btn1, btn2;
private int ScaleTimes = 1, ScaleAngle = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView) findViewById(R.id.myImageView);
final Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),
R.drawable.a);
final int widthOrig = bmp.getWidth();
final int heightOrig = bmp.getHeight();
mImageView.setImageBitmap(bmp);
btn1 = (Button) findViewById(R.id.myButton1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ScaleAngle--;
if (ScaleAngle < -60) {
ScaleAngle = -60;
}
int newWidth = widthOrig * ScaleTimes;
int newHeight = heightOrig * ScaleTimes;
float scaleWidth = ((float) newWidth) / widthOrig;
float scaleHeight = ((float) newHeight) / heightOrig;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.setRotate(5 * ScaleAngle);
Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, widthOrig,
heightOrig, matrix, true);
BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(
resizeBitmap);
mImageView.setImageDrawable(myNewBitmapDrawable);
}
});
btn2 = (Button) findViewById(R.id.myButton2);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ScaleAngle++;
if (ScaleAngle > 60) {
ScaleAngle = 60;
}
int newWidth = widthOrig * ScaleTimes;
int newHeight = heightOrig * ScaleTimes;
float scaleWidth = ((float) newWidth) / widthOrig;
float scaleHeight = ((float) newHeight) / heightOrig;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.setRotate(5 * ScaleAngle);
Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, widthOrig,
heightOrig, matrix, true);
BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(
resizeBitmap);
mImageView.setImageDrawable(myNewBitmapDrawable);
}
});
}
}
运行的效果:
知识点学习:
1.
Matrix 是一个处理翻转、缩放等图像效果的重要类
Matrix.postScale 可设置缩放比例,默认为1
void |
setRotate(float degrees):参数是一个度数,默认绕(0,0)转
Set the matrix to rotate about (0,0) by the specified number of degrees.
|
void |
setRotate(float degrees, float px, float py):参数是一个度数,绕着点(x,y)
Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).
|
void |
setScale(float sx, float sy, float px, float py)
Set the matrix to scale by sx and sy, with a pivot point at (px, py).
|
void |
setScale(float sx, float sy)
Set the matrix to scale by sx and sy.
|
以点px,py为原点缩放 >=0 1为正常大小
如果是负数,图形就会翻转
如果没设置原点坐标,默认以0,0点缩放(如果发现图片不见了,检查一下是不是翻转出了屏幕)
更多关于Matrix的知识点请看我下篇转载的文章2.
Bitmap.createBitmap(source, x, y, width, height, m, filter)
@param source The bitmap we are subsetting
* @param x The x coordinate of the first pixel in source
* @param y The y coordinate of the first pixel in source
* @param width The number of pixels in each row
* @param height The number of rows
* @param m Optional matrix to be applied to the pixels
* @param filter true if the source should be filtered.
* @return A bitmap that represents the specified subset of source
Bitmap resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, widthOrig,heightOrig, matrix, true);