I have many circles drawn on the Canvas and want to know which of them is touched.
One solution is to create a bitmap as the size of the screen and draw a copy of the circles that are on the screen only with a different color so that when the call getPixel () the bitmap poosso recognize which circle is clicked. My problem is that I do not know how to draw a circle on a bitmap ... that is, how to draw the bitmap as a drawing normally on Canvas.
Paint paint;
Bitmap screen;
int w,h;
int px=-1,py=-1; //coordinate
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//registriamo le dimensioni della view
w=MeasureSpec.getSize(widthMeasureSpec);
h=MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(w,h);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
Bitmap.Config conf = Bitmap.Config.ARGB_8888; //Each pixel is stored on 4 bytes
screen=Bitmap.createBitmap(w,h, conf);
paint=new Paint(); // pennello
paint.setColor(Color.RED);
paint.setAntiAlias(true);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if(px==-1&&py==-1){ // se non abbiamo ancora settato le coordinate, posizioniamo la bmp al centro
px=w/2-bw/2; //metà della larghezza view, meno metà della figura
py=h/2-bh/2; //metà dell'altezza view, meno metà della figura
}
canvas.drawCircle(px, py, 70, paint);
}
public void updatePosition(int x, int y) {
invalidate();
}
}
You want to create Canvas associated with your bitmap something like this
gCanvas = new Canvas();
gCanvas.setBitmap(yourBitmap);
and then use it as a canvas and draw what you want
EDIT: In case I wasn't clear, everything you draw on this canvas is like you draw on your bitmap. Then according to your X, Y get the color and compare
Hope this helps and enjoy your work
EDIT: Working example
public class MainActivity extends Activity {
private Bitmap image;
private int width;
private int height;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clickete);
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
createClickArea();
findViewById(R.id.clickLayout).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int result = whichCircle((int)event.getRawX(), (int)event.getRawY());
Log.i("Color clicked: ", String.valueOf(result));
return false;
}
});
Drawable dr = new BitmapDrawable(getResources(), image);
findViewById(R.id.clickLayout).setBackgroundDrawable(dr);
}
private void createClickArea(){
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
image = Bitmap.createBitmap(width, height, conf);
Canvas gCanvas = new Canvas();
gCanvas.setBitmap(image);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
gCanvas.drawRGB(0, 0, 0);
for(int i = 0; i < 4; i++){
switch(i){
case 0: paint.setColor(Color.WHITE); break;
case 1: paint.setColor(Color.YELLOW); break;
case 2: paint.setColor(Color.BLUE); break;
case 3: paint.setColor(Color.GREEN); break;
}
gCanvas.drawCircle((i + 1) * 80, (i + 1) * 80, 40, paint);
}
}
private int whichCircle(int x, int y){
int result = 0;
int color = image.getPixel(x, y);
switch(color){
case Color.WHITE: result = 1; break;
case Color.YELLOW: result = 2; break;
case Color.BLUE: result = 3; break;
case Color.GREEN: result = 4; break;
}
return result;
}
}
Just resolve deprecated methods and hide your bitmap, sorry but I don't have time to replace this now, my lunch is getting cold :)'. And remember work this in full screen or adjust your rawX, rawY according to your bitmap position.
You can try this one as to you can draw the bitmap as the background you want to draw first on the canvas in the ondraw function by implementing the surfaceholder.callback
canvas.drawBitmap(bgrReverse, fromRect2, toRect2, null);
then implement the onTouch() event in which you will get the pointer of the movement of the finger and getting those movement you can draw your circle to the received particular onTouch() and move of the finger.
Related
I have created a drawing application in android and was wondering, what is the correct way to draw on canvas while been concise of RAM allocation.
I have noticed that when the number of paths or points within the paths become too large, delays in onDraw(Canvas canvas) become apparent. It is obvious there is a solution to this, but I am looking for the standard way to solve this.
I have not found much information online, but I am currently trying to save the current canvas to a Bitmap and then setting my View for the canvas (LinearLayout).
I am currently trying the following, but it is not setting the LinearLayout to the Background:
public Bitmap saveTempCanvas() {
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
this.setBackgroundColor(Color.WHITE);
Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
return bmp;
}
//. . .
//. . .
Drawable ob = new BitmapDrawable(getResources(), pv.saveTempCanvas());
LinearLayoutCanvas.setBackgroundDrawable(ob);
Is this the standard way to create an "Paint" activity?
Why is the following code not working to set the BitmapDrawable?
If you are calling this function in onDraw() each time, I am not surprised why it is more and more laggy. Here is my approach and it works well:
public class TouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
Bitmap bitMap;
Canvas myCanvas;
Canvas defaultCanvas;
public TouchEventView(Context context) {
this(context, null);
}
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
bitMap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.BEVEL);
}
#Override
protected void onDraw(Canvas canvas) {
myCanvas = new Canvas(bitMap);
defaultCanvas = canvas;
canvas.drawPath(path, paint);
myCanvas.drawColor(Color.WHITE);
myCanvas.setBitmap(bitMap);
myCanvas.drawPath(path, paint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
performClick();
break;
default:
return false;
}
invalidate();
return true;
}
#Override
public boolean performClick() {
super.performClick();
return true;
}
}
I am using below code to erase bitmap from canvas on touch event.
it's working fine in android 2.3 but in 4.0 it show me black spot instead of erasing bitmap.
anyone can help.
public class DemoTouch extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchView(this));
}
class TouchView extends View {
Bitmap bgr;
Bitmap overlayDefault;
Bitmap overlay;
Paint pTouch;
int X = -100;
int Y = -100;
Canvas c2;
public TouchView(Context context) {
super(context);
bgr = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
overlayDefault = BitmapFactory.decodeResource(getResources(),
R.drawable.a2);
overlay = BitmapFactory.decodeResource(getResources(),
R.drawable.a2).copy(Config.ARGB_4444, true);
c2 = new Canvas(overlay);
pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
pTouch.setColor(Color.TRANSPARENT);
pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
pTouch.setAntiAlias(true);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
X = (int) ev.getX();
Y = (int) ev.getY();
invalidate();
break;
}
case MotionEvent.ACTION_MOVE: {
X = (int) ev.getX();
Y = (int) ev.getY();
invalidate();
break;
}
case MotionEvent.ACTION_UP:
break;
}
return true;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw background
canvas.drawBitmap(bgr, 0, 0, null);
// copy the default overlay into temporary overlay and punch a hole
// in it
// c2.drawBitmap(overlayDefault, 0, 0, null); // exclude this line
// to
// show all as you draw
c2.drawCircle(X, Y, 20, pTouch);
// draw the overlay over the background
canvas.drawBitmap(overlay, 0, 0, null);
}
}
}
Have a look at this method of mine. The view EditImageView is a custom ImageView that I also applied that erase function to. I had the same problem until I applied the code below inside the if statement. That fixes the problem for 3.0 and up
/**
* Sets the image to the view in the content view
* #param view
*/
private void setImageView(EditImageView view) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
// #### THIS IS THE IMPORTANT PART ######
if (Build.VERSION.SDK_INT >= 11) {
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
LinearLayout main = (LinearLayout) findViewById(R.id.galleryImage);
main.removeAllViews();
main.addView(view);
}
EDIT
You can probably do something like this in your code.
public TouchView(Context context) {
super(context);
// This should fix it from 3.0 and up
if (Build.VERSION.SDK_INT >= 11) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
bgr = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
overlayDefault = BitmapFactory.decodeResource(getResources(),
R.drawable.a2);
overlay = BitmapFactory.decodeResource(getResources(),
R.drawable.a2).copy(Config.ARGB_4444, true);
c2 = new Canvas(overlay);
pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
pTouch.setColor(Color.TRANSPARENT);
pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
pTouch.setAntiAlias(true);
}
Hope this also helps you
I am working on some project, in which I have to animate image depending upon the users touch, but the problem is I am not getting all the touch points, therefore I do not get the smooth animation. I am working on Android tablet os 4.0.3.
public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";
List<Points> points = new ArrayList<Points>();
Paint paint = new Paint();
Button button = null;
int movement = 0;
int[] imageIds = {R.drawable.zero,R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,
R.drawable.five,R.drawable.six,R.drawable.seven,R.drawable.eight,R.drawable.ninie,R.drawable.ten,
R.drawable.eleven,R.drawable.twelve,R.drawable.thirteen,R.drawable.fourteen,R.drawable.fifteen,
R.drawable.sixteen,R.drawable.seventeen,R.drawable.eighteen,R.drawable.ninteen,R.drawable.twenty,
R.drawable.twentyone,R.drawable.twentytwo,R.drawable.twentythree,R.drawable.twentyfour,
R.drawable.twentyfive,R.drawable.twentysix,R.drawable.twentyseven,R.drawable.twentyeight,
R.drawable.twentynine,R.drawable.thirty,R.drawable.thirtyone,R.drawable.thirtytwo,
R.drawable.thirtythree,R.drawable.thirtyfour,R.drawable.thirtyfive,R.drawable.thirtysix,
R.drawable.thirtyseven,R.drawable.thirtyeight,R.drawable.thirtynine,R.drawable.fortyone,
R.drawable.fortytwo,R.drawable.fortytwo,R.drawable.fortyfour,R.drawable.fortyfive,
R.drawable.fortysix,R.drawable.fortyseven,R.drawable.fortyeight,R.drawable.fortynine,
R.drawable.fifty,R.drawable.fiftyone,R.drawable.fiftyone,R.drawable.fiftythree,R.drawable.fiftyfour,
R.drawable.fiftyfive,R.drawable.fiftysix,R.drawable.fiftyseven,R.drawable.fiftyeight,
R.drawable.fiftynine,R.drawable.sixty,R.drawable.sixtyone,R.drawable.sixtytwo,
R.drawable.sixtythree,R.drawable.sixtyfour,R.drawable.sixtyfive,R.drawable.sixtysix,
R.drawable.sixtyseven,R.drawable.sixtyeight,R.drawable.sixtynine,R.drawable.seventy,
R.drawable.seventyone,R.drawable.seventytwo,R.drawable.seventythree,R.drawable.seventyfour,
R.drawable.seventyfive,R.drawable.seventysix,R.drawable.seventyseven,R.drawable.seventyeight,
R.drawable.seventynine,R.drawable.eighty,R.drawable.eightyone,R.drawable.eightytwo,R.drawable.thirtythree,
R.drawable.eightyfour,R.drawable.eightyfive,R.drawable.eightysix,R.drawable.eightyseven,
R.drawable.seventyeight,R.drawable.eightynine,R.drawable.ninty,R.drawable.nintyone,
R.drawable.nintytwo,R.drawable.nintythree,R.drawable.nintyfour,R.drawable.nintyfive,
R.drawable.nintysix,R.drawable.nintyseven,R.drawable.nintyeight,R.drawable.nintynine,
R.drawable.hundred
};
int touchPoint=0,currentImage=0;
float previousX = 0, starting = 0, difference = 0, remaining=0;
RelativeLayout relativeLayout;
Boolean update = false;
int changeImageFrequency = 4;
ImageView image;
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
}
#SuppressLint("DrawAllocation")
#TargetApi(11)
#Override
public void onDraw(Canvas canvas) {
Log.i("testing", "i am in draw");
if (movement==0) {
Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
canvas.drawBitmap(bimap, 0, 0,null);
invalidate();
}
else if (movement==1) {
if (currentImage+changeImageFrequency<100) {
currentImage+=changeImageFrequency;
Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
canvas.drawBitmap(bimap, 0, 0,null);
invalidate();
}
else {
Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
canvas.drawBitmap(bimap, 0, 0,null);
currentImage=0;
}
movement=0;
invalidate();
}
else if (movement==2) {
if (currentImage-changeImageFrequency>=0) {
currentImage-=changeImageFrequency;
Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
canvas.drawBitmap(bimap, 0, 0,null);
invalidate();
}
else {
Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
canvas.drawBitmap(bimap, 0, 0,null);
currentImage=99;
}
movement=0;
invalidate();
}
}
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch(action) {
case MotionEvent.ACTION_DOWN:
starting = event.getX();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
difference = event.getX()-starting;
if (difference>0)
movement = 1;
else if (difference<0)
movement = 2;
invalidate();
break;
case MotionEvent.ACTION_UP:
invalidate();
break;
}
return true;
}
If your images are of average resolution, then loading them (BitmapFactory.decodeResource) in the Main thread will block the UI, causing some touch events to be skipped untill the loading is performed. A better implementation is to use SurfaceView instead of View since it performs its drawing in a separate thread. Check this link for more details on using SurfaceView.
Can anyone help me on how to redraw the canvas. I tried many examples and source code from the internet, but it still didn't work on my PC like the invalidate func, canvas.save, canvas.restore, etc. I want to do some translation and scaling for the canvas, but when I follow the step on the internet it shows nothing. This is my source code. (I'm still new to Java/Android programming.)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawMaps.j=1;
resources = this.getResources();
try {
GetAttributes("path");
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar1);
panel = new Panel(this);
setContentView(R.layout.main);
panel.onDraw(canvas2);
ImageView image = (ImageView) findViewById(R.id.mapImage);
image.setImageBitmap(bufMaps);
}
class Panel extends View{
Paint paint = new Paint();
public Panel(Context context) {
super(context);
setFocusable(true);
}
public Bitmap quicky_XY(Bitmap bitmap,int pos_x,int pos_y){
Bitmap bufMap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bufMap);
canvas.save();
final Paint paint = new Paint();
width = canvas.getWidth();//start
height = canvas.getHeight();//end
drawMaps.xpos = width / 30;
drawMaps.ypos = height/ 20;
paint.setStrokeWidth(0);
for (int i = 0; i < 30; i++) {
paint.setColor(Color.DKGRAY);
canvas.drawLine(drawMaps.xpos +(drawMaps.xpos*i), 0,
drawMaps.xpos +(drawMaps.xpos*i), height, paint);
//canvas.drawLine(startX, startY, stopX, stopY, paint)
}
for (int i = 0; i < 20; i++) {
paint.setColor(Color.DKGRAY);
canvas.drawLine(0, drawMaps.ypos+(drawMaps.ypos*i),
width, drawMaps.ypos+(drawMaps.ypos*i), paint);
}
canvas.translate(pos_x,pos_y);
drawMaps.addPath(canvas);
canvas.restore();
invalidate();
return bufMap;
}
#Override
public void onDraw(Canvas canvas) {
canvas.save();
bufMaps = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
bufMaps = quicky_XY(emptyBmap,positionX,positionY);
}
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
positionX = (int)event.getRawX();
positionY = (int)event.getRawY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: {
prevX = positionX;
prevY = positionY;
}
break;
case MotionEvent.ACTION_MOVE: {
final int distY = Math.abs(positionY - prevY);
final int distX = Math.abs(positionX - prevX);
if (distX > mTouchSlop || distY > mTouchSlop){
panel.getDrawingCache();
panel.invalidate();
}
Log.e("LSDEBUG", "touch X, " + positionX);
}
break;
}
return true;
}
You do not call onDraw() yourself. Instead, you call to invalidate() and it will make sure onDraw() is called as soon as the it can.
Also, if you are trying to draw on the canvas from outside the onDraw() method, you need to get a reference to the canvas.
Inside your onDraw() the canvas is not being changed. only saved (again, called on invalidate() or whenever the system needs to redraw this View):
#Override
public void onDraw(Canvas canvas) {
canvas.save();
bufMaps = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
bufMaps = quicky_XY(emptyBmap,positionX,positionY);
}
Accessing the canvas from outside the onDraw() is done using a Holder().lockCanvas() to get reference to the canvas. After drawing, you unlock it again, using unlockAndPost() and that's it.
You will also need to implement the Callback.surfaceCreated interface to find out when the Surface is available to use.
Take a look at the android reference for SurfaceHolder.
This post explains it pretty well.
I try to erase parts of a bitmap in my Android application by using Porter-Duff Xfermodes.
I have a green background which is overlayed by a blue bitmap. When I touch the screen a "hole" in the overlaying bitmap is supposed to be created making the green background visible. Instead of a hole my current code produces a black dot.
Below is my code. Any ideas, what I am doing wrong here?
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new DrawView(this));
}
public class DrawView extends View implements OnTouchListener {
private int x = 0;
private int y = 0;
Bitmap bitmap;
Canvas bitmapCanvas;
private final Paint paint = new Paint();
private final Paint eraserPaint = new Paint();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
// Set background
this.setBackgroundColor(Color.GREEN);
// Set bitmap
bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.RGB_565);
bitmapCanvas = new Canvas();
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.drawColor(Color.BLUE);
// Set eraser paint properties
eraserPaint.setAlpha(0);
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
eraserPaint.setAntiAlias(true);
}
#Override
public void onDraw(Canvas canvas) {
bitmapCanvas.drawColor(Color.BLUE);
bitmapCanvas.drawCircle(x, y, 10, eraserPaint);
canvas.drawBitmap(bitmap, 0, 0, paint);
}
public boolean onTouch(View view, MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
invalidate();
return true;
}
}
Here is working code... may help somebody
public class ImageDemo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Panel(this));
}
class Panel extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mPaint;
Bitmap bitmap;
Canvas pcanvas;
int x = 0;
int y =0;
int r =0;
public Panel(Context context) {
super(context);
Log.v("Panel", ">>>>>>");
setFocusable(true);
setBackgroundColor(Color.GREEN);
// setting paint
mPaint = new Paint();
mPaint.setAlpha(0);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mPaint.setAntiAlias(true);
// getting image from resources
Resources r = this.getContext().getResources();
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.mickey);
// converting image bitmap into mutable bitmap
bitmap = bm.createBitmap(295, 260, Config.ARGB_8888);
pcanvas = new Canvas();
pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
pcanvas.drawBitmap(bm, 0, 0, null);
}
#Override
protected void onDraw(Canvas canvas) {
// draw a circle that is erasing bitmap
pcanvas.drawCircle(x, y, r, mPaint);
canvas.drawBitmap(bitmap, 0, 0,null);
super.onDraw(canvas);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// set parameter to draw circle on touch event
x = (int) event.getX();
y = (int) event.getY();
r =20;
// At last invalidate canvas
invalidate();
return true;
}
}
}
First thought, I'm not sure if setting alpha to 0 on your erase paint object is a good idea. That might make the whole thing ineffective.
Also, you should always use Bitmap.Config.ARGB_8888 if you're dealing with alphas.
If you're having trouble with the PorterDuff stuff, though, I would suggest simplifying your approach to ONLY do that (temporarily). That will help you narrow down the part which isn't working. Comment out everything to do with touch and view updates.
Then you can single out what part of the drawing isn't working right. Set up your constructor like this:
DrawView()
{
/* Create the background green bitmap */
...
/* Create foreground transparent bitmap */
...
/* Draw a blue circle on the foreground bitmap */
...
/* Apply the foreground to the background bitmap
using a PorterDuff method */
...
}
onDraw()
{
/* Simply draw the background bitmap */
...
}
If you set things up like that, you should be able to tell how your PD method is affecting the green bitmap, and change things accordingly.
Here is another advancement for your solution ... See Demo example
public class MainActivity extends Activity {
Bitmap bp;
Canvas bitmapCanvas;
DrawView drawImg;
LinearLayout ln1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln1 = (LinearLayout) findViewById(R.id.ln1);
drawImg = new DrawView(this);
ln1.addView(drawImg);
}
public class DrawView extends View implements View.OnTouchListener {
private int x = 0;
private int y = 0;
Bitmap bitmap;
Path circlePath;
Paint circlePaint;
private final Paint paint = new Paint();
private final Paint eraserPaint = new Paint();
public DrawView(Context context){
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
// Set background
this.setBackgroundColor(Color.CYAN);
bp = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
// Set bitmap
bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas();
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.drawColor(Color.TRANSPARENT);
bitmapCanvas.drawBitmap(bp, 0, 0, null);
circlePath = new Path();
circlePaint = new Paint();
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
// Set eraser paint properties
eraserPaint.setAlpha(0);
eraserPaint.setStrokeJoin(Paint.Join.ROUND);
eraserPaint.setStrokeCap(Paint.Cap.ROUND);
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
eraserPaint.setAntiAlias(true);
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmapCanvas.drawCircle(x, y, 30, eraserPaint);
canvas.drawPath(circlePath, circlePaint);
}
public boolean onTouch(View view, MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
bitmapCanvas.drawCircle(x, y, 30, eraserPaint);
circlePath.reset();
circlePath.addCircle(x, y, 30, Path.Direction.CW);
int ac=event.getAction();
switch(ac){
case MotionEvent.ACTION_UP:
Toast.makeText(MainActivity.this, String.valueOf(x), Toast.LENGTH_SHORT).show();
circlePath.reset();
break;
}
invalidate();
return true;
}
}
}
read more