I have an SVG image and I have it set up so when I move my finger over the image it tells me that I've "entered an image". But I noticed that there's an invisible box shape around the image, so when my finger is a bit outside the image I still get the "entered image" message. I want to only have it so if I go in the image it will display the message.
How do you remove that invisible box so the only thing my MotionEvent registers is the exact shape of my svg ?
My code:
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
rect1= new Rect(dragon.getLeft(), dragon.getTop(),
dragon.getRight(), dragon.getBottom());
rect2= new Rect(buttonTwo.getLeft(), buttonTwo.getTop(),
buttonTwo.getRight(), buttonTwo.getBottom());
}
///////////////////////////////////////////////
if (event.getActionMasked() == (MotionEvent.ACTION_DOWN | MotionEvent.ACTION_MOVE)) {
if (rect1.contains((int) event.getX(), (int) event.getY())) {
System.out.println(" Dragon Entered ");
}
}
if (event.getActionMasked() == (MotionEvent.ACTION_DOWN | MotionEvent.ACTION_MOVE)) {
if (rect2.contains((int) event.getX(), (int) event.getY())) {
System.out.println(" in button2!!!!: ");
}
}
Related
I wanna smooth movement (non-linear) to active all the spots on the screen below without clicking on each single square. It is working only when I click cell by cell, but when I try to move in loopy directions, it does not select the empty boxes.
Here is my current code:
#Override
public void onItemTouched(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (view.getTextAlignment() == View.TEXT_ALIGNMENT_CENTER) {
view.setBackgroundColor(getResources().getColor(R.color.correct));
view.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
contTouchCounter += 2;
Log.i(TAG, "onItemTouched: " + contTouchCounter);
}
if (contTouchCounter == 100) {
disposable.dispose();
linearContTouch.setVisibility(View.GONE);
cont_touch.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_baseline_check_circle_outline_24));
cont_touchTxt.setVisibility(View.GONE);
multimediaInfo.setContTouch(String.valueOf(contTouchCounter));
isContTouchStarted = false;
goToNextActivity();
}
}
}
I tried to change ACTION_DOWN to ACTION_MOVE but it didn't work.
Here is my screen
I want to let the user to draw lines on the image and user can erase tline they draw. So far I can draw lines on the custom ImageView created as below. However, I tried a lot of method and all of them draw black line when I selected erase mode. I want the part after earse still is the Image I set and implement the erase just like drawing lines. How can I do this?
public class PaintView : ImageView
{
private Paint _Paint = new Paint();
private Paint ErasePaint= new Paint();
public Bitmap _Bmp;
private Canvas _Canvas = null;
private PointF _StartPt = new PointF();
private PointF _EndPt = new PointF();
public int status=1; //1 draw, 0 erase
public PaintView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
}
public void Initialize()
{
_Paint = new Paint ();
_Paint.Color = new Color (255, 255, 255);
_Paint.StrokeWidth = 5;
_Paint.StrokeCap = Paint.Cap.Round;
int alpha = 3;
_Bmp = Bitmap.CreateScaledBitmap(_Bmp,_Bmp.Width*alpha, _Bmp.Height*alpha, false);
_Canvas = new Canvas(_Bmp);
this.SetImageBitmap(_Bmp);
}
override public bool OnTouchEvent(MotionEvent e)
{
if (status == 1) {
switch (e.Action)
{
case MotionEventActions.Down:
_StartPt.Set(e.GetX() - 1, e.GetY() - 1);// for just a tapping
DrawLine(e);
break;
case MotionEventActions.Move:
DrawLine(e);
break;
case MotionEventActions.Up:
DrawLine(e);
break;
}
}
if (status == 0) {
switch (e.Action)
{
case MotionEventActions.Down:
_StartPt.Set(e.GetX() - 1, e.GetY() - 1);// for just a tapping
eraseCanvas(e);
break;
case MotionEventActions.Move:
eraseCanvas(e);
break;
case MotionEventActions.Up:
eraseCanvas(e);
break;
}
}
return true;
}
private void DrawLine(MotionEvent e)
{
_EndPt.Set(e.GetX(), e.GetY());
_Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, _Paint);
_StartPt.Set(_EndPt);
Invalidate();
}
private void eraseCanvas(MotionEvent e)
{
_EndPt.Set(e.GetX(), e.GetY());
ErasePaint.Color = Color.Transparent;
ErasePaint.StrokeWidth = 5;
ErasePaint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.Clear));
_Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, ErasePaint);
_StartPt.Set(_EndPt);
Invalidate();
}
}
Well then, the way I do it in my app is to set the bitmap I draw on as a background to the canvas View on initialization - this should make the line go transparent after the drawing operation is done. I am not really familiar with Xamarin but in Android Java I just go something like:
BitmapDrawable bd = new BitmapDrawable(bmp);
canvas.setBitmapDrawable(bd); //or this.setBitmapDrawable(bd);
after instantiating the Canvas. It's because the xfer mode clear scrapes off everything showing View beneath the Canvas, so the line stays black. I believe you should adapt it to your code fairly easy.
If it works as I described (the line is black during drawing, and then poof - goes transparent), then to make it transparent right away, the way to go for me was to turn off hardware acceleration when drawing with Mode.Clear, again, Android Java:
boolean bool = "rubber".equals(tool);
if (bool) {
this.setLayerType(LAYER_TYPE_SOFTWARE, canvasPaint); //or canvas.setLayer...
}
//drawing stuff
if (bool) {
this.setLayerType(LAYER_TYPE_HARDWARE, canvasPaint);
}
Good luck, let me know how did it go or if you need some clarification or something. :)
I have drawn a image on ONTOUCH with (X,Y)cordinates while moving the image it's cordinates should move along with the image when it reaches it end point the image should be drawn there or else the image should go back to it's starting position.
For eg:(in our mobile if kepad lock we will drag it to open if it reaches it end point the lock will open or else the image will reaches it's starting position).
Reffered link:http://www.vogella.com/tutorials/AndroidTouch/article.html
Here is my code:
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mLocked) {
return false;
}
final int action = event.getAction();
float x = event.getX();
float y = event.getY();
if (mOnDrawerScrollListener != null) {
mOnDrawerScrollListener.onScrollStarted();
}
final int pt = getSide();
mTouchDelta = (int)(y - pt);
prepareTracking(pt);
mVelocityTracker.addMovement(event);
final Rect frame = mFrame;
final View handle = mHandle;
If anyone have idea about this please help me friends.
If you want to get(x, y) cordinates of view (ImageView), just use view.getX(), view.getY(). But when you get it in onCreate(), the results will be (0, 0), because it is not created yet. You should use view.getX(), view.getY() in onGlobalLayoutListener(). For e.g:
rlRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
rlRoot.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.d("X", view.getX()+"");
Log.d("Y", view.getY()+"");
}
});
With rlRoot is the layout includes your view and view may be ImageView or any View.
Try it, hope it help.
i have a picture of a head and its on a transparent background and i want to get if the head is clicked not the area behind his head. this is what i had for the entire image not the image withing the box. pretend the grey part is the transparent part. i want nothing to happen when that area is clicked. only his body.
link to picture->link to picture
stage.addListener(new ClickListener()
{
#Override
public boolean touchDown(InputEvent event, float x, float y, int a , int b)
{
if(box.equals(stage.hit(x,y,false)))
{
box.setPosition(100, 100);
point.play();
score++;
}
return true;
}
});
I draw a circle where the user touches a image view. It draws the circle fine, but it is not where the user touches. It is always off the left by a lot. Here is the code:
getting the touch:
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
System.out.println("Touch recieved at "+arg1.getX() + " " + arg1.getY());
touchX = (arg1.getX());
touchY = (arg1.getY());
System.out.println("Touch recieved at "+touchX + " " + touchY);
image.setImageBitmap(createImage());
return true;
}
});
and drawing on the image:
public Bitmap createImage(){
Bitmap image = bmp.copy(Bitmap.Config.RGB_565, true);
Canvas canvas = new Canvas(image);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GREEN);
canvas.drawCircle(touchX, touchY, radius, paint);
System.out.println("Drew a circle at "+touchX+" " + touchY+" with a radius of "+radius+".");
return image;
}
Any ideas?
Try to draw at
touchX=touchX- image.getWidth() / 2;
touchY=touchY- image.getHeight() / 2;
this will draw the center of the image the place you touch
You have to add touch listener to the layout not to the image. Add setOnTouchListener to the layout. No need of touch listener to the image.