我是Android新手,我想在手指触摸时模糊图像。
我搜索了一些例子,但我发现像基于搜索条值的整个图像变得模糊。
但我想要的东西就像我可以设置手指触摸的半径,然后根据触摸,图像的那部分变得模糊。
MYANSWER
MainActivity1.java
这里,MainActivity.java文件包含从MainActivity.java传递的位图。
public class MainActivity1 extends Activity implements OnClickListener {
// button
private ImageButton opacityBtn;
// custom view
private DrawingView drawView;
private Object bmpimg;
private ImageButton currPaint;
Bitmap b;
private int originalheight;
private int originalwidth;
public Bitmap tem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
Intent i1 = getIntent();
String img = i1.getStringExtra("imgpath");
Log.e("img", "" + img);
b = BitmapFactory.decodeFile(img);
// get button
opacityBtn = (ImageButton) findViewById(R.id.opacity_btn);
// listen
opacityBtn.setOnClickListener(this);
// custom view instance
// LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
// currPaint = (ImageButton)paintLayout.getChildAt(0);
// currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
Log.e("mainactivity1", "" + b);
// bmpimg = Bitmap.createScaledBitmap(srcimg, 100, 50, true);
// Display display = getWindowManager().getDefaultDisplay();
// Point size = new Point();
// display.getSize(size);
// int width = size.x;
// int height = size.y;
// tem=Bitmap.createScaledBitmap(b, width,
// height - 200, true);
drawView = (DrawingView) findViewById(R.id.drawing);
// fetching height and width of device
int widthPx = getWindowManager().getDefaultDisplay().getWidth();
int heightPx = getWindowManager().getDefaultDisplay().getHeight();
// set new height and width to custom class drawing view
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) drawView
.getLayoutParams();
if (b.getHeight() < heightPx && b.getWidth() < widthPx) {
params.height = b.getHeight();
params.width = b.getWidth();
} else {
if (b.getHeight() > heightPx && b.getWidth() > widthPx) {
params.height = heightPx;
params.width = widthPx;
} else if (b.getWidth() > widthPx) {
params.width = widthPx;
params.height = b.getHeight();
} else {
params.width = b.getWidth();
params.height = heightPx;
}
}
drawView.setLayoutParams(params);
drawView.setCanvasBitmap(b, b.getHeight(),
b.getWidth(), widthPx, heightPx);
if(b.getHeight()<heightPx&&b.getWidth()<widthPx){
this.originalheight=b.getHeight();
this.originalwidth=b.getWidth();
}else{
if(b.getHeight()>heightPx&&b.getWidth()>widthPx){
this.originalheight=heightPx;
this.originalwidth=widthPx;
}
else if(b.getWidth()>widthPx){
this.originalwidth=widthPx;
this.originalheight=b.getHeight();
}
else{
this.originalwidth=b.getWidth();
this.originalheight=heightPx;
}
}
tem=Bitmap.createScaledBitmap(b, originalwidth,
originalheight, true);
Bitmap bitmap=createBitmap_ScriptIntrinsicBlur(tem,40);
drawView.firstsetupdrawing(bitmap);
// drawView.setScree_w(width);
// drawView.setScreen_h(height);
}
/*
* @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
* menu; this adds items to the action bar if it is present.
* getMenuInflater().inflate(R.menu.main, menu); return true; }
*/
/* public void paintClicked(View view) {
//use chosen color
//set erase false
// drawView.setErase(false);
// drawView.setPaintAlpha(100);
// drawView.setBrushSize(drawView.getLastBrushSize());
if(view!=currPaint){
Bitmap bitmap=createBitmap_ScriptIntrinsicBlur(tem,40);
drawView.firstsetupdrawing(bitmap);
// ImageButton imgView = (ImageButton)view;
// String color = view.getTag().toString();
// drawView.setColor(color);
// //update ui
// imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
// currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
// currPaint=(ImageButton)view;
}
}
*/
@Override
public void onClick(View view) {
if (view.getId() == R.id.opacity_btn) {
// launch opacity chooser
final Dialog seekDialog = new Dialog(this);
seekDialog.setTitle("Opacity level:");
seekDialog.setContentView(R.layout.opacity_chooser);
// get ui elements
final TextView seekTxt = (TextView) seekDialog
.findViewById(R.id.opq_txt);
final SeekBar seekOpq = (SeekBar) seekDialog
.findViewById(R.id.opacity_seek);
// set max
seekOpq.setMax(40);
// show current level
int currLevel = drawView.getPaintAlpha();
seekTxt.setText(currLevel + "%");
seekOpq.setProgress(currLevel);
// update as user interacts
seekOpq.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
seekTxt.setText(Integer.toString(progress) + "%");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// listen for clicks on ok
Button opqBtn = (Button) seekDialog.findViewById(R.id.opq_ok);
opqBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap=createBitmap_ScriptIntrinsicBlur(tem, seekOpq.getProgress());
drawView.setPaintAlpha(seekOpq.getProgress(),bitmap);
seekDialog.dismiss();
}
});
// show dialog
seekDialog.show();
}
}
public Bitmap createBitmap_ScriptIntrinsicBlur(Bitmap src, int radius) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// Radius range (0 < r <= 25)
/* if (r <= 1) {
r = 1;
} else if (r > 25) {
r = 25;
}
Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);
RenderScript renderScript = RenderScript.create(this);
Allocation blurInput = Allocation.createFromBitmap(renderScript, src);
Allocation blurOutput = Allocation.createFromBitmap(renderScript,
bitmap);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
Element.U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(r);
blur.forEach(blurOutput);
blurOutput.copyTo(bitmap);
renderScript.destroy();
return bitmap;*/
//here i can make radius up to 40 use for it below code
int w = src.getWidth();
int h = src.getHeight();
int[] pix = new int[w * h];
src.getPixels(pix, 0, w, 0, 0, w, h);
for(int r = radius; r >= 1; r /= 2)
{
for(int i = r; i < h - r; i++)
{
for(int j = r; j < w - r; j++)
{
int tl = pix[(i - r) * w + j - r];
int tr = pix[(i - r) * w + j + r];
int tc = pix[(i - r) * w + j];
int bl = pix[(i + r) * w + j - r];
int br = pix[(i + r) * w + j + r];
int bc = pix[(i + r) * w + j];
int cl = pix[i * w + j - r];
int cr = pix[i * w + j + r];
pix[(i * w) + j] = 0xFF000000 |
(((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) +
(br & 0xFF) + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF |
(((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00)
+ (br & 0xFF00) + (bc & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00 |
(((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) +
(bl & 0xFF0000) + (br & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000) +
(cr & 0xFF0000)) >> 3) & 0xFF0000;
}
}
}
Bitmap blurred = Bitmap.createBitmap(w, h, src.getConfig());
blurred.setPixels(pix, 0, w, 0, 0, w, h);
return blurred;
}
}
DrawingView.java
这里,DrawingView.java包含canavas,因此我们可以在画布上输出图像并模糊它。
public class DrawingView extends View {
// drawing path
private Path drawPath;
// drawing and canvas paint
private Paint drawPaint, canvasPaint;
// initial color
private int paintColor = 0xFFC0C0C0, paintAlpha = 255;
// canvas
private Canvas drawCanvas;
// canvas bitmap
private Bitmap canvasBitmap;
int originalheight,originalwidth;
/**
* @return the scree_w
*/
private BlurMaskFilter blurMaskFilter;
public int scree_w, screen_h;
public void setCanvasBitmap(Bitmap bitmap1, int i, int j, int widthPx, int heightPx) {
this.canvasBitmap = bitmap1;
if(i<heightPx&&j<widthPx){
this.originalheight=i;
this.originalwidth=j;
}else{
if(i>heightPx&&j>widthPx){
this.originalheight=heightPx-1;
this.originalwidth=widthPx-1;
}
else if(j>widthPx){
this.originalwidth=widthPx-1;
this.originalheight=i;
}
else{
this.originalwidth=j;
this.originalheight=heightPx-1;
}
}
}
public void setScree_w(int width) {
// TODO Auto-generated method stub
this.scree_w = width;
}
public void setScreen_h(int height) {
// TODO Auto-generated method stub
this.screen_h = height;
}
// constructor
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
setupDrawing();
}
// prepare drawing
private void setupDrawing() {
drawPath = new Path();
drawPaint = new Paint();
//
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(30);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint();
// BitmapShader patternBMPshader = new BitmapShader(canvasBitmap,
// Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
// //color and shader
// drawPaint.setColor(0xFFFFFFFF);
// drawPaint.setShader(patternBMPshader);
blurMaskFilter = new BlurMaskFilter( 5,
BlurMaskFilter.Blur.NORMAL);
drawPaint.setMaskFilter(blurMaskFilter);
}
public void firstsetupdrawing( Bitmap bitmap){
drawPath = new Path();
drawPaint = new Paint();
//
// drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(30);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint();
// BitmapShader patternBMPshader = new BitmapShader(canvasBitmap,
// Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
// //color and shader
// drawPaint.setColor(0xFFFFFFFF);
// drawPaint.setShader(patternBMPshader);
blurMaskFilter = new BlurMaskFilter( 5,
BlurMaskFilter.Blur.NORMAL);
drawPaint.setMaskFilter(blurMaskFilter);
drawPaint.setColor(paintColor);
// drawPaint.setAlpha(paintAlpha);
BitmapShader patternBMPshader = new BitmapShader(bitmap,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//color and shader
drawPaint.setColor(0xFFFFFFFF);
drawPaint.setShader(patternBMPshader);
}
// view assigned size
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// int i=canvasBitmap.getHeight();
// int y=canvasBitmap.getWidth();
Log.e("hiyt wid-------------------------", "" + scree_w
+ "dsgvdfg sunita u will be happy" + screen_h);
canvasBitmap = Bitmap.createScaledBitmap(canvasBitmap,originalwidth+1,originalheight+1, true);
drawCanvas = new Canvas(canvasBitmap);
}
// draw view
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
// respond to touch interaction
float x = 0, y = 0;
public boolean onTouchEvent(MotionEvent event) {
Log.e("getalph drawing view", "" + getPaintAlpha());
float touchX = 0, touchY = 0;
x = touchX;
y = touchY;
touchX = event.getX();
touchY = event.getY();
// respond to down, move and up events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
/*
* if (x == touchX && y == touchY) {
*
* drawPath.moveTo(touchX + 1, touchY + 1); } else {
*/
drawPath.moveTo(touchX, touchY);
// }
break;
case MotionEvent.ACTION_MOVE:
/*
* if (x == touchX && y == touchY) {
*
* drawPath.lineTo(touchX + 1, touchY + 1);
*
* } else {
*/
drawPath.lineTo(touchX, touchY);
// }
break;
case MotionEvent.ACTION_UP:
/*
* if (x == touchX && y == touchY) {
*
* drawPath.lineTo(touchX + 1, touchY + 1);
* drawCanvas.drawPath(drawPath, drawPaint); drawPath.reset(); }
*
* else {
*/
drawPath.lineTo(touchX, touchY);
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
// }
break;
default:
return false;
}
// redraw
invalidate();
return true;
}
// return current alpha
public int getPaintAlpha() {
return Math.round((float) paintAlpha / 255 * 40);
}
// set alpha
public void setPaintAlpha(int newAlpha, Bitmap bitmap) {
paintAlpha = Math.round((float) newAlpha / 40 * 255);
drawPaint.setColor(paintColor);
drawPaint.setAlpha(paintAlpha);
BitmapShader patternBMPshader = new BitmapShader(bitmap,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//color and shader
drawPaint.setColor(0xFFFFFFFF);
drawPaint.setShader(patternBMPshader);
}
public void setColor(String newColor) {
invalidate();
//check whether color value or pattern name
if(newColor.startsWith("#")){
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
drawPaint.setShader(null);
}
else{
//pattern
int patternID = getResources().getIdentifier(
newColor, "drawable", "com.example.drawingfun");
//decode
// Bitmap patternBMP = BitmapFactory.decodeResource(getResources(), patternID);
Bitmap patternBMP = BitmapFactory.decodeResource(getResources(),R.drawable.sun);
//create shader
Log.e("drawing view pattern+getalpha", "" + patternBMP+"dsfsdsd"+getPaintAlpha());
BitmapShader patternBMPshader = new BitmapShader(patternBMP,
Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
//color and shader
drawPaint.setColor(0xFFFFFFFF);
drawPaint.setShader(patternBMPshader);
}
}
}