Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android.graphics.Camera 實現簡單的3D效果

android.graphics.Camera 實現簡單的3D效果

編輯:關於Android編程

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+xuTKtdXiuPa0+sLro6zKx9StwLTRp8+wtcTKsbryy9G1vbXEsru5/ba8wM/U58HLzfy8x8rHxMTA77XEwcuhozwvcD4KPHA+y7Ox49TawO/D5rzTwcvQqdeiys0goaO/tNfF0Ke5+828ICC21NXVtPrC6yAgvs3M2LHww/fBy8HLo6y499bW1+m6z7P2uPfW1rrc7MW1xNCnufs8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">/** * Camera與Matrix的比較:
* Camera的rotate()相關方法是指定某一維度上旋轉指定的角度。
* Matrix的rotate()相關方法實現的效果是順時針旋轉指定的角度;與Camera指定Z軸旋轉效果相同,但方向相反。
* * Camera的translate()方法根據某一維度上視點的位移實現圖像的縮放,與Matrix的scale()相關方法作用效果相似, * 只是Matrix的scale()相關方法是直接指定縮放比例。
* Camera不支持傾斜操作,Matrix可以直接實現傾斜操作。
* * @author Sodino E-mail:[email protected] * @version Time:2011-9-26 下午04:17:49 */ public class MainActivity extends Activity implements OnSeekBarChangeListener { private Camera camera; // views 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 SeekBar seekbarZTranslate; private TextView txtXTranslate; private TextView txtYTranslate; private TextView txtZTranslate; private ImageView imgResult; // integer params private int rotateX, rotateY, rotateZ; private float skewX, skewY; private int translateZ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // camera camera = new Camera(); // rotate 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); // translate seekbarXSkew = (SeekBar) findViewById(R.id.seekbarXSkew); seekbarXSkew.setOnSeekBarChangeListener(this); seekbarYSkew = (SeekBar) findViewById(R.id.seekbarYSkew); seekbarYSkew.setOnSeekBarChangeListener(this); seekbarZTranslate = (SeekBar) findViewById(R.id.seekbarZTranslate); seekbarZTranslate.setOnSeekBarChangeListener(this); txtXTranslate = (TextView) findViewById(R.id.txtXSkew); txtYTranslate = (TextView) findViewById(R.id.txtYSkew); txtZTranslate = (TextView) findViewById(R.id.txtZTranslate); imgResult = (ImageView) findViewById(R.id.imgResult); System.out.println("33333333333333333333"); // refresh refreshImage(); // post pre set 其實代表了Matrix 中方法變換的次序,pre是向前加入隊列執行,post從後面加入隊列執行。 // 舉一些例子: // matrix.preScale(2f,1f); matrix.preTranslate(5f, 0f); matrix.postScale(0.2f, 1f); matrix.postTranslate(0.5f, 0f); // 方法執行的順序為: // preTranslate(5, 0) -> preScale(2f, 1f) -> postScale(0.2f, 1f) -> postTranslate(0.5f, 0f) // 而matrix的set方法則會對先前的pre和post操作進行清除,而後再設置它的值,比如下列的方法調用: // matrix.postTranslate(2f, 0f); matrix.preScale(0.2f, 1f); matrix.setScale(1f, 1f); matrix.postScale(5f, 1f); matrix.preTranslate(0.5f, 0f); 方法執行的順序為: // translate(0.5f, 0f) -> scale(1f, 1f) -> scale(5f, 1). // Canvas裡scale, translate, rotate, concat方法都是pre方法,如果要進行更多的變換可以先從canvas獲得matrix, 變換後再設置回canvas. } private void refreshImage() { // 獲取待處理的圖像 BitmapDrawable tmpBitDra = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher); Bitmap tmpBit = tmpBitDra.getBitmap(); // 開始處理圖像 // 1.獲取處理矩陣 // 記錄一下初始狀態。save()和restore()可以將圖像過渡得柔和一些。 // Each save should be balanced with a call to restore(). camera.save(); Matrix matrix = new Matrix(); // rotate camera.rotateX(rotateX); camera.rotateY(rotateY); camera.rotateZ(rotateZ); // translate camera.translate(0, 0, translateZ); camera.getMatrix(matrix); // 恢復到之前的初始狀態。 camera.restore(); // 設置圖像處理的中心點 matrix.preTranslate(tmpBit.getWidth() >> 1, tmpBit.getHeight() >> 1); matrix.preSkew(skewX, skewY); // setScale(float sx, float sy, float px, float py) 放大 // setSkew(float kx, float ky, float px, float py) 斜切 // setTranslate(float dx, float dy) 平移 // setRotate(float degrees, float px, float py) 旋轉 // matrix.postSkew(skewX, skewY); // 直接setSkew(),則前面處理的rotate()、translate()等等都將無效。 // matrix.setSkew(skewX, skewY); // 2.通過矩陣生成新圖像(或直接作用於Canvas) Log.d("ANDROID_LAB", "width=" + tmpBit.getWidth() + "height=" + tmpBit.getHeight()); Bitmap newBit = null; try { // 經過矩陣轉換後的圖像寬高有可能不大於0,此時會拋出IllegalArgumentException newBit = Bitmap.createBitmap(tmpBit, 0, 0, tmpBit.getWidth(), tmpBit.getHeight(), matrix, true); } catch (IllegalArgumentException iae) { iae.printStackTrace(); } if (newBit != null) { imgResult.setImageBitmap(newBit); } } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (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(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }
工程地址:

http://download.csdn.net/detail/kongbaidepao/6885105

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved