- Camera的rotate()相关方法是指定某一维度上旋转指定的角度。
- Matrix的rotate()相关方法实现的效果是顺时针旋转指定的角度;与Camera指定Z轴旋转效果相同,但方向相反。
- Camera的translate()方法根据某一维度上视点的位移实现图像的缩放,与Matrix的scale()相关方法作用效果相似,只是Matrix的scale()相关方法是直接指定缩放比例。
- Camera不支持倾斜操作,Matrix可以直接实现倾斜操作。
本文为练习Camera的使用。实现效果图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/b36807cc184042cd55b06949d2cc9719.gif)
Camera的处理结果只是生成一个Matrix,该Matrix用于Bitmap或Canvas绘制Bitmap时才能产生旋转或缩放的效果。
代码如下:
001 | package lab.sodino.camera; |
003 | import android.app.Activity; |
004 | import android.graphics.Bitmap; |
005 | import android.graphics.Camera; |
006 | import android.graphics.Matrix; |
007 | import android.graphics.drawable.BitmapDrawable; |
008 | import android.os.Bundle; |
009 | import android.util.Log; |
010 | import android.widget.ImageView; |
011 | import android.widget.SeekBar; |
012 | import android.widget.SeekBar.OnSeekBarChangeListener; |
013 | import android.widget.TextView; |
016 | * Camera与Matrix的比较:<br/> |
017 | * Camera的rotate()相关方法是指定某一维度上旋转指定的角度。<br/> |
018 | * Matrix的rotate()相关方法实现的效果是顺时针旋转指定的角度;与Camera指定Z轴旋转效果相同,但方向相反。<br/> |
020 | * Camera的translate()方法根据某一维度上视点的位移实现图像的缩放,与Matrix的scale()相关方法作用效果相似, |
021 | * 只是Matrix的scale()相关方法是直接指定缩放比例。<br/> |
023 | * Camera不支持倾斜操作,Matrix可以直接实现倾斜操作。<br/> |
025 | * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> Sodino E-mail:sodinoopen@hotmail.com |
026 | * @version Time:2011-9-26 下午04:17:49 |
028 | public class ActCamera extends Activity implements OnSeekBarChangeListener { |
029 | private Camera camera; |
031 | private SeekBar seekbarXRotate; |
032 | private SeekBar seekbarYRotate; |
033 | private SeekBar seekbarZRotate; |
034 | private TextView txtXRotate; |
035 | private TextView txtYRotate; |
036 | private TextView txtZRotate; |
037 | private SeekBar seekbarXSkew; |
038 | private SeekBar seekbarYSkew; |
039 | private SeekBar seekbarZTranslate; |
040 | private TextView txtXTranslate; |
041 | private TextView txtYTranslate; |
042 | private TextView txtZTranslate; |
043 | private ImageView imgResult; |
045 | private int rotateX, rotateY, rotateZ; |
046 | private float skewX, skewY; |
047 | private int translateZ; |
049 | /** Called when the activity is first created. */ |
051 | public void onCreate(Bundle savedInstanceState) { |
052 | super .onCreate(savedInstanceState); |
053 | setContentView(R.layout.main); |
055 | camera = new Camera(); |
058 | seekbarXRotate = (SeekBar) findViewById(R.id.seekbarXRotate); |
059 | seekbarXRotate.setOnSeekBarChangeListener( this ); |
060 | seekbarYRotate = (SeekBar) findViewById(R.id.seekbarYRotate); |
061 | seekbarYRotate.setOnSeekBarChangeListener( this ); |
062 | seekbarZRotate = (SeekBar) findViewById(R.id.seekbarZRotate); |
063 | seekbarZRotate.setOnSeekBarChangeListener( this ); |
064 | txtXRotate = (TextView) findViewById(R.id.txtXRotate); |
065 | txtYRotate = (TextView) findViewById(R.id.txtYRotate); |
066 | txtZRotate = (TextView) findViewById(R.id.txtZRotate); |
068 | seekbarXSkew = (SeekBar) findViewById(R.id.seekbarXSkew); |
069 | seekbarXSkew.setOnSeekBarChangeListener( this ); |
070 | seekbarYSkew = (SeekBar) findViewById(R.id.seekbarYSkew); |
071 | seekbarYSkew.setOnSeekBarChangeListener( this ); |
072 | seekbarZTranslate = (SeekBar) findViewById(R.id.seekbarZTranslate); |
073 | seekbarZTranslate.setOnSeekBarChangeListener( this ); |
074 | txtXTranslate = (TextView) findViewById(R.id.txtXSkew); |
075 | txtYTranslate = (TextView) findViewById(R.id.txtYSkew); |
076 | txtZTranslate = (TextView) findViewById(R.id.txtZTranslate); |
077 | imgResult = (ImageView) findViewById(R.id.imgResult); |
082 | private void refreshImage() { |
084 | BitmapDrawable tmpBitDra = (BitmapDrawable) getResources().getDrawable(R.drawable.honeycomb); |
085 | Bitmap tmpBit = tmpBitDra.getBitmap(); |
091 | Matrix matrix = new Matrix(); |
093 | camera.rotateX(rotateX); |
094 | camera.rotateY(rotateY); |
095 | camera.rotateZ(rotateZ); |
097 | camera.translate( 0 , 0 , translateZ); |
098 | camera.getMatrix(matrix); |
102 | matrix.preTranslate(tmpBit.getWidth() >> 1 , tmpBit.getHeight() >> 1 ); |
103 | matrix.preSkew(skewX, skewY); |
108 | Log.d(“ANDROID_LAB”, “width=” + tmpBit.getWidth() + “ height=” + tmpBit.getHeight()); |
109 | Bitmap newBit = null ; |
112 | newBit = Bitmap.createBitmap(tmpBit, 0 , 0 , tmpBit.getWidth(), tmpBit.getHeight(), matrix, true ); |
113 | } catch (IllegalArgumentException iae) { |
114 | iae.printStackTrace(); |
116 | if (newBit != null ) { |
117 | imgResult.setImageBitmap(newBit); |
122 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
123 | if (seekBar == seekbarXRotate) { |
124 | txtXRotate.setText(progress + “゜”); |
126 | } else if (seekBar == seekbarYRotate) { |
127 | txtYRotate.setText(progress + “゜”); |
129 | } else if (seekBar == seekbarZRotate) { |
130 | txtZRotate.setText(progress + “゜”); |
132 | } else if (seekBar == seekbarXSkew) { |
133 | skewX = (progress - 100 ) * 1 .0f / 100 ; |
134 | txtXTranslate.setText(String.valueOf(skewX)); |
135 | } else if (seekBar == seekbarYSkew) { |
136 | skewY = (progress - 100 ) * 1 .0f / 100 ; |
137 | txtYTranslate.setText(String.valueOf(skewY)); |
138 | } else if (seekBar == seekbarZTranslate) { |
139 | translateZ = progress - 100 ; |
140 | txtZTranslate.setText(String.valueOf(translateZ)); |
146 | public void onStartTrackingTouch(SeekBar seekBar) { |
151 | public void onStopTrackingTouch(SeekBar seekBar) { |