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

原文: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;
    }

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		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) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public 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 exception
			e.printStackTrace();
			newBitmap = null;
		}
    	
    	if (newBitmap != null) {
			imgResult.setImageBitmap(newBitmap);
		}
	}
}


布局文件:



    
    

    

        

        

        
    

    

        

        

        
    

    

        

        

        
    

    

    

        

        

        
    

    

        

        

        
    

    

    

        

        

        
    

    

        

        
    


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

你可能感兴趣的:(Android应用开发,UI,界面设计)