android.graphics.Camera实现图像的旋转、缩放,配合Matrix...

来源:互联网 发布:mac照片如何导入iphone 编辑:程序博客网 时间:2024/05/29 17:03

原文:http://www.eoeandroid.com/thread-202761-1-1.html

 

  • Camera的rotate()相关方法是指定某一维度上旋转指定的角度。
  • Matrix的rotate()相关方法实现的效果是顺时针旋转指定的角度;与Camera指定Z轴旋转效果相同,但方向相反。
  • Camera的translate()方法根据某一维度上视点的位移实现图像的缩放,与Matrix的scale()相关方法作用效果相似,只是Matrix的scale()相关方法是直接指定缩放比例。
  • Camera不支持倾斜操作,Matrix可以直接实现倾斜操作。

    主程序:

    package com.example.progressui;import android.R.integer;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Camera;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.view.Menu;import android.view.MenuItem;import android.widget.ImageView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;import android.support.v4.app.NavUtils;public class ProgressActivity extends Activity implements OnSeekBarChangeListener {//声明变量private SeekBar seekbarXRotate;private SeekBar seekbarYRotate;private SeekBar seekbarZRotate;private TextView txtXRotate;private TextView txtYRotate;private TextView txtZRotate;private SeekBar seekbarXSkew;private SeekBar seekbarYSkew;private TextView txtXTranslate;private TextView txtYTranslate;private SeekBar seekbarZTranslate;private TextView txtZTranslate;private int rotateX,rotateY,rotateZ;private float skewX,skewY;private int translateZ;private ImageView imgResult;private Camera camera;private int dx;private int dy;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_progress);                camera = new Camera();                seekbarXRotate = (SeekBar)findViewById(R.id.seekbarXRotate);        seekbarXRotate.setOnSeekBarChangeListener(this);                seekbarYRotate = (SeekBar)findViewById(R.id.seekbarYRotate);        seekbarYRotate.setOnSeekBarChangeListener(this);                seekbarZRotate = (SeekBar)findViewById(R.id.seekbarZRotate);        seekbarZRotate.setOnSeekBarChangeListener(this);                txtXRotate = (TextView)findViewById(R.id.txtXRotate);        txtYRotate = (TextView)findViewById(R.id.txtYRotate);        txtZRotate = (TextView)findViewById(R.id.txtZRotate);                seekbarXSkew = (SeekBar)findViewById(R.id.seekbarXSkew);        seekbarXSkew.setOnSeekBarChangeListener(this);        seekbarYSkew = (SeekBar)findViewById(R.id.seekbarYSkew);        seekbarYSkew.setOnSeekBarChangeListener(this);                txtXTranslate = (TextView)findViewById(R.id.txtXSkew);        txtYTranslate = (TextView)findViewById(R.id.txtYSkew);                seekbarZTranslate = (SeekBar)findViewById(R.id.seekbarZTranslate);        seekbarZTranslate.setOnSeekBarChangeListener(this);        txtZTranslate = (TextView)findViewById(R.id.txtZTranslate);                imgResult = (ImageView)findViewById(R.id.imgResult);                refreshImage();                BitmapDrawable tmpbitDra = (BitmapDrawable)getResources().getDrawable(R.drawable.img);        Bitmap tmpBip = tmpbitDra.getBitmap();                dx = tmpBip.getWidth()/2;        dy = tmpBip.getHeight()/2;    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_progress, menu);        return true;    }@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stubif (seekBar == seekbarXRotate) {txtXRotate.setText(progress + "°");rotateX = progress;}else if (seekBar == seekbarYRotate) {txtYRotate.setText(progress + "゜");rotateY = progress;}else if (seekBar == seekbarZRotate) {txtZRotate.setText(progress + "゜");rotateZ = progress;}else if (seekBar == seekbarXSkew) {skewX = (progress - 100)*1.0f/100;txtXTranslate.setText(String.valueOf(skewX));}else if (seekBar == seekbarYSkew) {skewY = (progress - 100)*1.0f/100;txtYTranslate.setText(String.valueOf(skewY));}else if (seekBar == seekbarZTranslate) {translateZ = progress - 100;txtZTranslate.setText(String.valueOf(translateZ));}refreshImage();}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}    public void refreshImage() {//开始获得待处理的图像    BitmapDrawable tmpBitDra = (BitmapDrawable)getResources().getDrawable(R.drawable.img);    Bitmap tmpbit = tmpBitDra.getBitmap();        camera.save();    Matrix matrix = new Matrix();        camera.rotateX(rotateX);    camera.rotateY(rotateY);    camera.rotateZ(rotateZ);        camera.translate(dx, dy, translateZ);    camera.getMatrix(matrix);    //恢复之前的初始状态。    camera.restore();        matrix.preSkew(skewX, skewY);        Bitmap newBitmap = null;    try {newBitmap = Bitmap.createBitmap(tmpbit, 0, 0, tmpbit.getWidth(), tmpbit.getHeight(), matrix, true);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();newBitmap = null;}        if (newBitmap != null) {imgResult.setImageBitmap(newBitmap);}}}


    布局文件:

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#061D3F"    android:orientation="vertical" >        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="XYZ轴旋转( 单位:角度; 范围:0°~360° )"        />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="X轴" />        <SeekBar            android:id="@+id/seekbarXRotate"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:max="360" />        <TextView            android:id="@+id/txtXRotate"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Y轴" />        <SeekBar            android:id="@+id/seekbarYRotate"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:max="360" />        <TextView            android:id="@+id/txtYRotate"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Z轴" />        <SeekBar            android:id="@+id/seekbarZRotate"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:max="360" />        <TextView            android:id="@+id/txtZRotate"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="" />    </LinearLayout>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="XY轴倾斜" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="X轴" />        <SeekBar            android:id="@+id/seekbarXSkew"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:progress="100"            android:max="200" />        <TextView            android:id="@+id/txtXSkew"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Y轴" />        <SeekBar            android:id="@+id/seekbarYSkew"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:progress="100"            android:max="200" />        <TextView            android:id="@+id/txtYSkew"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="" />    </LinearLayout>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Z轴缩放" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Z轴" />        <SeekBar            android:id="@+id/seekbarZTranslate"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:progress="100"            android:max="200" />        <TextView            android:id="@+id/txtZTranslate"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/img" />        <ImageView            android:id="@+id/imgResult"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

    工程文件:http://download.csdn.net/detail/zhaoshiqing7/4630392