参见英文答案 > Making Overlaid image transparent on touch in Android? 3个
我正在开发AndroID的绘图应用程序.现在我想实现一个橡皮擦,允许通过使用触摸输入擦除加载的位图的部分,并使位图沿手指的路径透明.
我尝试实现的一个很好的例子显示在AndroID应用程序Steamy Window中. Steamy Window模拟雾化窗口,用户可以通过触摸输入擦除部分雾.
有关我如何实现该功能的任何建议?
更新:我在下面发布了当前代码中最重要的部分.由于以下原因,我对它并不满意:
>绘画相当迟钝.我可以在这里改进什么?
>我正在寻找一种方法来使用Alpha蒙版来设置像素的像素透明度/ Alpha,因为我想模拟画笔.有任何建议如何实现这一目标?
public DrawVIEw(Context context, String imagename) { super(context); // Get the overlay image and its dimensions Bitmap overlayImageOriginal = BitmapFactory.decodeResource( getResources(), R.drawable.overlay); wIDth = overlayImageOriginal.getWIDth(); height = overlayImageOriginal.getHeight(); // Get a copy of the overlay image and recycle the original overlayImage = overlayImageOriginal.copy(Config.ARGB_8888, true); overlayImageOriginal.recycle(); // Get background image of this level int resID = getResources().getIDentifIEr(imagename, "drawable", "com.xxx"); backgroundImage = BitmapFactory.decodeResource(getResources(), resID); // copy pixels overlayImagePixels = new int[wIDth * height]; overlayImage.getPixels(overlayImagePixels, 0, wIDth, 0, 0, wIDth, height); getHolder().addCallback(this); _thread = new DrawThread(getHolder(), this); setFocusable(true); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { synchronized (_thread.getSurfaceHolder()) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touchstart(event.getX(), event.getY()); return true; case MotionEvent.ACTION_MOVE: touchmove(event.getX(), event.getY()); return true; case MotionEvent.ACTION_UP: touchStop(); return true; } return false; } } @OverrIDe public voID onDraw(Canvas canvas) { // draw the image to guess canvas.drawBitmap(backgroundImage, 0, 0, null); // apply user input to the overlay-image setRegionTransparency((int) mtouchX, (int) mtouchY, 20, 0); // draw the overlay-image (hIDing the image to guess) canvas.drawBitmap(overlayImage, 0, 0, null); } /** * * @param posX * @param posY * @param radius * @param Alpha - 0: full transparent, 255: full opaque */ private voID setRegionTransparency(int posX, int posY, int radius, int Alpha) { int left = posX - radius; int right = posX + radius; int top = posY - radius; int bottom = posY + radius; int color = 0; // onDraw() is called before any user input // -> assume 0/0 as invalID arguments if (posX == 0 && posY == 0) { return; } // Todo currently only rectangular area is supported // but we need circular // we should use a pre-made Alpha mask with smoothed edges // we should take into account: pressure and/or duration of touch for (int y = top; y = 0 && y = 0 && x = touch_TolERANCE || dy >= touch_TolERANCE) { mPath.quadTo(mtouchX, mtouchY, (x + mtouchX) / 2, (y + mtouchY) / 2); mtouchX = x; mtouchY = y; } }}
解决方法:
看一眼
Making Overlaid image transparent on touch in Android?
总结以上是内存溢出为你收集整理的android – 通过触摸输入在位图上绘制透明线[复制]全部内容,希望文章能够帮你解决android – 通过触摸输入在位图上绘制透明线[复制]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)