android 实现在照片上涂鸦
2015-05-19 17:00
225 查看
图片上涂鸦
在android设备上对照片进行涂鸦,要解决的不同手机屏幕大小不一致的问题,怎样做才能去适应不同的手机屏幕,这里,可以用到一个相似的数学知识。 就是,获取手机屏幕的像素值,再得到图片大小的像素值,计算出两个值的比值,然后监听触摸事件,监听触摸事件得到的数据,处理一下,就可对应到图片具体的像素点。看一下效果。 好了,看一下代码
public class ScrawlActivity extends Activity { ImageView imageView; double pictureRelativeLeft, pictureRelativeTop, pictureRelativeRight, pictureRelativeButtom; double imageViewLeft, imageViewTop, imageViewRight, imageViewButtom; double pictureRealLeft, pictureRealTop, pictureRealRight, pictureRealButtom; Bitmap bitmap; double proportionWidth, proportionHeight; double bitmapWidth, bitmapHeight; Canvas canvas; Path path; double preX, preY; Paint paint; boolean hasOut=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); InitToolBar.initToolBar(this); setContentView(R.layout.activity_scrawl); setActionBar(); imageView = (ImageView) findViewById(R.id.scrawlImageView); BitmapFactory.Options bfoOptions = new BitmapFactory.Options(); bfoOptions.inScaled = false; Intent intent=getIntent(); bitmap=((APP)getApplication()).bitmap.copy(Bitmap.Config.ARGB_8888, true); imageView.setImageBitmap(bitmap); bitmapWidth = bitmap.getWidth(); bitmapHeight = bitmap.getHeight(); canvas = new Canvas(); System.out.println(bitmap); canvas.setBitmap(bitmap); setPiont(); path = new Path(); System.out.println("bitmap: " + bitmapWidth + " " + bitmapHeight); } void setActionBar(){ ActionBar actionBar=getActionBar(); actionBar.setTitle(" "); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setBackgroundDrawable(getResources().getDrawable( R.drawable.actionbar)); } @Override public void onWindowFocusChanged(boolean hasFocus) { if (hasFocus == true) { Matrix matrix = imageView.getImageMatrix(); Rect rect = imageView.getDrawable().getBounds(); float[] values = new float[9]; matrix.getValues(values); pictureRelativeLeft = values[2]; pictureRelativeTop = values[5]; pictureRelativeRight = pictureRelativeLeft + rect.width() * values[0]; pictureRelativeButtom = pictureRelativeTop + rect.height() * values[0]; int[] location = new int[2]; // 获取组件在手机屏幕中的绝对像素位置 imageView.getLocationOnScreen(location); imageViewLeft = location[0]; imageViewTop = location[1]; System.out.println("imageView:" + imageViewLeft + " " + imageViewTop); imageViewRight = imageView.getRight(); imageViewButtom = imageView.getBottom(); setPictureRealPosition(); proportionWidth = bitmapWidth / (pictureRealRight - pictureRealLeft); proportionHeight = bitmapHeight / (pictureRealButtom - pictureRealTop); } } void setPiont() { // 设置画笔的颜色 paint = new Paint(Paint.DITHER_FLAG); paint.setColor(Color.RED); // 设置画笔风格 paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(2); // 反锯齿 paint.setAntiAlias(true); paint.setDither(true); } @Override public boolean onTouchEvent(MotionEvent event) { double x = event.getX(); double y = event.getY(); if (x >= pictureRealLeft && x <= pictureRealRight && y >= pictureRealTop && y <= pictureRealButtom) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: x = (x - pictureRealLeft) * proportionWidth; y = (y - pictureRealTop) * proportionHeight; path.moveTo((float) x, (float) y); preX = x; preY = y; break; case MotionEvent.ACTION_MOVE: System.out.println(x + " " + y); x = (x - pictureRealLeft) * proportionWidth; y = (y - pictureRealTop) * proportionHeight; if(hasOut==true){ path.reset(); path.moveTo((float) x, (float) y); preX=x; preY=y; System.out.println("reset"); hasOut=false; } path.quadTo((float) preX, (float) preY, (float) x, (float) y); preX = x; preY = y; break; case MotionEvent.ACTION_UP: System.out.println(x + " " + y); x = (x - pictureRealLeft) * proportionWidth; y = (y - pictureRealTop) * proportionHeight; canvas.drawPath(path, paint); // ① path.reset(); break; } } else { path.reset(); hasOut=true; //System.out.println("reset"); } // 将cacheBitmap绘制到该View组件上 //canvas.drawBitmap(bitmap, 0, 0, paint); // ② // 沿着path绘制 canvas.drawPath(path, paint); imageView.setImageBitmap(bitmap); // invalidate(); return false; } void setPictureRealPosition() { pictureRealLeft = imageViewLeft + pictureRelativeLeft; pictureRealTop = imageViewTop + pictureRelativeTop; pictureRealRight = imageViewLeft + pictureRelativeRight; pictureRealButtom = imageViewTop + pictureRelativeButtom; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.scrawl, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.scrawlSure) { Intent intent =new Intent(); intent.putExtra("result", true); setResult(1, intent); ((APP)getApplication()).bitmap=bitmap; finish(); return true; } if(id==R.id.scrawlCancel){ setResult(2); finish(); return true; } if(id==android.R.id.home){ setResult(2); finish(); return true; } return false; } @Override protected void onDestroy() { super.onDestroy(); setResult(2); } }
相关文章推荐
- Android照片处理——涂鸦和拼图实现
- Android照片墙应用实现,再多的图片也不怕崩溃
- Android瀑布流照片墙实现,体验不规则排列的美感
- android实现拍照后对照片截图
- Android瀑布流照片墙实现,体验不规则排列的美感
- Android照片墙应用实现,再多的图片也不怕崩溃
- Android照片墙应用实现,再多的图片也不怕崩溃
- LruCache之Android瀑布流照片墙实现,体验不规则排列的美感
- LruCache之Android照片墙应用实现,再多的图片也不怕崩溃
- Android瀑布流照片墙实现,体验不规则排列的美感
- android瀑布流照片墙实现代码详解
- Android照片墙应用实现使用硬件缓存跟内存缓存
- android实现涂鸦,保存涂鸦后的图片,清屏
- Android照片墙应用实现,再多的图片也不怕崩溃
- Android不规则瀑布流照片墙的实现+LruCache算法
- android 实现橡皮擦效果以及保存涂鸦的功能
- Android照片墙应用实现,再多的图片也不怕崩溃
- Android瀑布流照片墙实现,体验不规则排列的美感
- Android瀑布流照片墙实现,体验不规则排列的美感
- Android照片墙加强版,使用ViewPager实现画廊效果