PhotoShake

package com.bingo.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bingo.BingoApp;
import com.bingo.util.ImageDispose;
import com.bingo.util.ImageMemory;

public class PhotoShake extends Activity {

	private List<String> list = new ArrayList<String>();
	private List<Bitmap> listbt = new ArrayList<Bitmap>();
	private Button shakeOk,pshake;
	private Bitmap bitmap;
	private ImageView shakeimg;
	private ImageButton shakeBack;
	
	//检测摇动相关变量
	private long initTime = 0;
	private long lastTime = 0;
	private long curTime = 0;
	private long duration = 0;

	private float last_x = 0.0f;
	private float last_y = 0.0f;
	private float last_z = 0.0f;

	private float shake = 0.0f;
	private float totalShake = 0.0f;
	
	
	DisplayMetrics dm = new DisplayMetrics();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		Intent intent = getIntent();
		list = (List<String>) intent.getSerializableExtra("listBitmap");
		setContentView(R.layout.photoshake);
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		listbt=imageBitmap();
		
		shakeimg = (ImageView) findViewById(R.id.shakeimg);
		bitmap =createBitmap(listbt);
		shakeimg.setImageBitmap(bitmap);
		
		shakeBack = (ImageButton) findViewById(R.id.shake_back);
		shakeBack.setOnClickListener(new shake_Back());
		
		shakeOk = (Button) findViewById(R.id.shakeok);
		shakeOk.setOnClickListener(new shake_Ok());
		
		pshake = (Button) findViewById(R.id.pshake);
		pshake.setOnClickListener(new shake_D());
		
		
		
		
		// 获取传感器管理器
		SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		// 获取加速度传感器
		Sensor acceleromererSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		// 定义传感器事件监听器
		SensorEventListener acceleromererListener = new SensorEventListener() {

			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
				//什么也不干
			}

			//传感器数据变动事件
			@Override
			public void onSensorChanged(SensorEvent event) {	

				//如果没有开始录音的话可以监听是否有摇动事件,如果有摇动事件可以开始录音
				
					//获取加速度传感器的三个参数
					float x = event.values[SensorManager.DATA_X];
					float y = event.values[SensorManager.DATA_Y];
					float z = event.values[SensorManager.DATA_Z];

					//获取当前时刻的毫秒数
					curTime = System.currentTimeMillis();

					//100毫秒检测一次
					if ((curTime - lastTime) > 100) {

						duration = (curTime - lastTime);

						// 看是不是刚开始晃动
						if (last_x == 0.0f && last_y == 0.0f && last_z == 0.0f) {
							//last_x、last_y、last_z同时为0时,表示刚刚开始记录
							initTime = System.currentTimeMillis();
						} else {
							// 单次晃动幅度
							shake = (Math.abs(x - last_x) + Math.abs(y - last_y) + Math.abs(z - last_z)) / duration * 100;
						}

						//把每次的晃动幅度相加,得到总体晃动幅度
						totalShake = shake;
						//textView.setText(String.valueOf(totalShake));
						// 判断是否为摇动,这是我自己写的标准,不准确,^_^
						if (totalShake > 8 ) {
							bitmap =createBitmap(listbt);
							shakeimg.setImageBitmap(bitmap);
							initShake();
						}

					

					last_x = x;
					last_y = y;
					last_z = z;
					lastTime = curTime;
				}
			}

		};

		//在传感器管理器中注册监听器
		sm.registerListener(acceleromererListener, acceleromererSensor, SensorManager.SENSOR_DELAY_NORMAL);
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
	
	class shake_Back implements OnClickListener{
		@Override
		public void onClick(View v) {
			finish();
		}
	}
	
	class shake_Ok implements OnClickListener{
		@Override
		public void onClick(View v) {
			BingoApp bingoApp = (BingoApp) getApplication();
			bingoApp.setShakeimg(bitmap);
			Intent intent = new Intent(PhotoShake.this,TestAC.class);
			startActivity(intent);
		}
	}
	
	class shake_D implements OnClickListener{
		@Override
		public void onClick(View v) {
			bitmap =createBitmap(listbt);
			shakeimg.setImageBitmap(bitmap);
		}
	}
	
	 private Bitmap createBitmap( List<Bitmap> list)  
	    {  
	        String tag = "createBitmap";  
	        Log.d( tag, "create a new bitmap" );  
	        if( list == null )  
	        {  
	            return null;  
	        }  
	        int r1 = 0;
			int r2 = 0;
			
			int x = 0;
			int y = 0;
			
	        for (int i = 0; i < 4; i++) {
				r1 = (int) (Math.random() * list.size());
				r2 = (int) (Math.random() * list.size());
				Bitmap temp;
				Bitmap temp01;
				if (r1 != r2) {
					temp = list.get(r1);
					temp01 = list.get(r2);

					list.set(r2, temp);

					list.set(r1, temp01);
				}
			}
	        
	        
	       
	        Bitmap newb = Bitmap.createBitmap(dm.widthPixels,
	        		dm.widthPixels, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图  
	        Canvas cv = new Canvas( newb ); 
	        for (int k = 0; k < list.size(); k++) {
	        	
	        	if (k == 1) {
					x =  + dm.widthPixels/2;
				}

				if (k == 2) {
					x = 0;
					y = y + dm.widthPixels/2;
				}

				if (k == 3) {
					x=0;
					y=0;
					x = x + dm.widthPixels/2;
					y = y + dm.widthPixels/2;
				}
	        	
	        	cv.drawBitmap( list.get(k), x, y, null);//在 0,0坐标开始画入src  
	        	
	        }
	        
	        cv.save( Canvas.ALL_SAVE_FLAG );//保存  
	        cv.restore();//存储  
	        return newb;  
	    }  
	 
	 
	 private List<Bitmap> imageBitmap(){
			
			List list01 = new ArrayList();
			Bitmap bitmap;
			
			for (int i = 0; i < list.size(); i++) {
				
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(list.get(i), opts);
			
			opts.inSampleSize = new ImageMemory().computeSampleSize(opts, -1, (dm.widthPixels/2) * (dm.widthPixels/2));
			opts.inJustDecodeBounds = false;

			Bitmap bmp = BitmapFactory.decodeFile(list.get(i), opts);

			ImageDispose iDispose= new ImageDispose();
			
			bitmap = iDispose.zoomBitmap(bmp, dm.widthPixels/2, dm.widthPixels/2);
			list01.add(bitmap);
			
			}
			
			
			return list01;
		}
	 
	//摇动初始化
		public void initShake() {
			lastTime = 0;
			duration = 0;
			curTime = 0;
			initTime = 0;
			last_x = 0.0f;
			last_y = 0.0f;
			last_z = 0.0f;
			shake = 0.0f;
			totalShake = 0.0f;
		}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值