3.2学习内容:android图片灰度化,VelocityTracker类

今日项目出现一个bug:

出现代码:

int width, height;
				height = bmSrc.getHeight();
				width = bmSrc.getWidth();

				bmpGray = Bitmap.createBitmap(width, height,
						Bitmap.Config.RGB_565);// android4.4.2(SM-N9008V),NativeMethod
				Canvas c = new Canvas(bmpGray);
				Paint paint = new Paint();
				ColorMatrix cm = new ColorMatrix();
				cm.setSaturation(0);
				ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
				paint.setColorFilter(f);
				c.drawBitmap(bmSrc, 0, 0, paint);


原因:android sm-n9008v中系统没有这个创建图片的类,包nativmethod方法,网上很流行以上的灰度化方法,其实是有错的!
bmpGray = Bitmap.createBitmap(width, height,
						Bitmap.Config.RGB_565);// android4.4.2(SM-N9008V),NativeMethod

解决方案:

使用另外一种灰度化方法:

 // constant factors
	          final double GS_RED = 0.299;
	          final double GS_GREEN = 0.587;
	          final double GS_BLUE = 0.114;
	          
	          // create output bitmap
	          Bitmap bmOut = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
	          // pixel information
	          int A, R, G, B;
	          int pixel;
	          
	          // get image size
	          int width = bmSrc.getWidth();
	          int height = bmSrc.getHeight();
	          
	          // scan through every single pixel
	          for(int x = 0; x < width; ++x) {
	           for(int y = 0; y < height; ++y) {
	            // get one pixel color
	            pixel = bmSrc.getPixel(x, y);
	            // retrieve color of all channels
	            A = Color.alpha(pixel);
	            R = Color.red(pixel);
	            G = Color.green(pixel);
	            B = Color.blue(pixel);
	            // take conversion up to one single value
	            R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
	            // set new pixel color to output bitmap
	            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
	           }
	          }
	          
	          // return final image
	          return bmOut;

来源:http://zhidao.baidu.com/link?url=qKdqviboKNQdRW2e_1NCJKyDArCd3fqgpthYWUpZd9RxKuWJ_Qdn66wvACJ-gHvdtws7AqWfMnZqtlo96-lLPCfWNeqNGgL0y6pXejrBKPe



安卓复选框对话框:

Builder builder=new android.app.AlertDialog.Builder(this);
//设置对话框的图标
builder.setIcon(R.drawable.header);
//设置对话框的标题
builder.setTitle("复选框对话框");
builder.setMultiChoiceItems(R.array.hobby, flags,
new DialogInterface.OnMultiChoiceClickL




android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率。用addMovement(MotionEvent)函数将Motion event加入到VelocityTracker类实例中.你可以使用getXVelocity() getXVelocity()获得横向和竖向的速率到速率时,但是使用它们之前请先调用computeCurrentVelocity(int)来初始化速率的单位 。


private VelocityTracker mVelocityTracker;//生命变量 
    //在onTouchEvent(MotionEvent ev)中 
    if (mVelocityTracker == null) { 
             mVelocityTracker = VelocityTracker. obtain () ;//获得VelocityTracker类实例 
    } 
     mVelocityTracker. addMovement (ev); //将事件加入到VelocityTracker类实例中 
     //判断当ev事件是MotionEvent.ACTION_UP时:计算速率 
    final VelocityTracker velocityTracker = mVelocityTracker; 
     // 1000 provides pixels per second 
     velocityTracker.computeCurrentVelocity(1, (float)0.01) ;  //设置maxVelocity值为0.1时,速率大于0.01时,显示的速率都是0.01,速率小于0.01时,显示正常 
    Log.i("test","velocityTraker"+velocityTracker.getXVelocity());                     
    velocityTracker.computeCurrentVelocity(1000);  //设置units的值为1000,意思为一秒时间内运动了多少个像素 
    Log.i("test","velocityTraker"+velocityTracker.getXVelocity()); 

注意VelocityTracker不使用时要回收




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值