Android move object to touch - java

I'm kinda new to building apps on Android. I do have some Java developing experience, but the project I do now is first on Android.
I'm building a basic cards game, and I want the card to move to the specific location I touch.
I did it, and the card moves to the touch point, but once the animation is over it bounces back to starting position.
I added a Image re-position code, but now it begins the animation from that point.
any ideas, anyone?
the code I did is:
public class FinallyActivity extends Activity {
/** Called when the activity is first created. */
EditText DBG;
ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBG = (EditText) findViewById(R.id.editText1);
iv = (ImageView) findViewById(R.id.imV1);
}
private void RunAnimations(MotionEvent event) {
// Animation b = AnimationUtils.loadAnimation(this,R.anim.cardtrans);
int xStart, yStart, dx, dy;
xStart = iv.getLeft();
yStart = iv.getTop();
Animation b = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,event.getX()-50,
Animation.ABSOLUTE,0,Animation.ABSOLUTE,event.getY()- 105);
b.setDuration(3000);
iv.clearAnimation();
iv.startAnimation(b);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
RunAnimations(event);
}
iv.layout((int)event.getX()-25,(int) event.getY()-105,
(int)event.getX()+25,(int) event.getY()-35);
return true;
}
}
Thanks All!!

b.fillAfter(true)
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.subview);
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
xDown = event.getX();
yDown = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float moveX,moveY;
moveX = event.getX();
moveY = event.getY();
float distanceX = moveX -xDown;
float distanceY = moveY -yDown;
imageView.setX(imageView.getX()+distanceX);
imageView.setY(imageView.getY()+distanceY);
break;
}
return true;
}
});
}

Related

Is there a way to draw on screen programmatically in android studio using java

I want to create an android app that draw on current layout of app without setting any other Contentview. This is the code that I have did but in this code it is creating an other view and then allow to draw but not on current layout.
**Canva Class:**
public CanvaClass(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
path = new Path();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float xPosition = event.getX();
float yPosition = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(xPosition,yPosition);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPosition,yPosition);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
}
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CanvaClass canvaClass = new CanvaClass(getApplicationContext(),null);
setContentView(canvaClass);
}
});
}
}

Android - How to click through transparent area of custom view

In order to position a button in the exact spot on all devices, I have created it with a custom view. The button takes up a small part of the screen but the actual view takes up most of the screen. All the area around the button is transparent. How can I allow the user to click through the transparent area to buttons underneath but still click the button area on the view?
Here is the relavant code:
public class MainActivity extends AppCompatActivity {
final float dotScale = 0.3f;
Dot dot1, dot2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.mainView);
MyView myView = new MyView(this);
myLayout.addView(myView);
// Two dots are created.
dot1 = new Dot(this);
dot1.xOffset = 2.9f;
dot1.yOffset = 3.3f;
myLayout.addView(dot1);
dot2 = new Dot(this);
dot2.xOffset = -2.4f;
dot2.yOffset = 1.1f;
myLayout.addView(dot2);
// Makes dots clickable
dot1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!dot1.isClicked) {
dot1.animate().setDuration(300).setInterpolator(new AnticipateInterpolator())
.scaleXBy(dotScale).scaleYBy(dotScale).alpha(1.0f);
dot1.isClicked = true;
}
}
});
dot2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!dot2.isClicked) {
dot2.animate().setDuration(300).setInterpolator(new AnticipateInterpolator())
.scaleXBy(dotScale).scaleYBy(dotScale).alpha(1.0f);
dot2.isClicked = true;
}
}
});
}
.......
//custom view for Dots
class Dot extends View {
int radius;
float xOffset;
float yOffset;
boolean isClicked = false;
public Dot(Context context) {
super(context);
setClickable(true);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
double ratio = (547d / 828d);
float circleX = (float)((x / 2) - (y * ratio) / xOffset);
float circleY = (float)(y / yOffset);
radius = (int)((float)y/13);
setPivotX(circleX);
setPivotY(circleY);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(circleX, circleY, radius, paint);
}
}
You should set your view clickable and don't override onTouch at all or just return false from it. Return false means that you didn't consume event and it will be forwarded to the next element in view hierarchy.

How to use touch screen and keyboard (external keyboard) at the same on android 5.1 app?

In my android app when i add touch screeen methods and i want to read at the same the keyboard with onkey method, i just lost one of them.
My question is : how can i use them both at the same time.
here is my main class :
public class SingleTouchActivity extends AppCompatActivity {
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_single_touch);
setContentView(new SingleTouchEventView(this, null));
edit= (EditText) findViewById(R.id.editText);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_0){
edit.setText("here we go");
}
return super.onKeyDown(keyCode,event);
}
}
And i have a class for touch screen which i test with draw method:
public class SingleTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setFocusable(true);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
}
#Override
public boolean onTouchEvent(MotionEvent 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:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
}
Does your device support multi-touch?
If yes, try this: (I hope this works)
public class SingleTouchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_single_touch);
setContentView(new SingleTouchEventView(this, null));
try {
ViewGroup vx = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
EditText et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
vx.addView(et);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I have found a solution which is more simple for app. Hope it will helps someone.
`public class MainActivity extends AppCompatActivity {
View myView;
public boolean tmpflag = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView= findViewById(R.id.Rec);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (tmpflag) {
myView.setBackgroundColor(Color.RED);
tmpflag = false;
} else {
myView.setBackgroundColor(Color.BLUE);
tmpflag = true;
}
return true;
}
return super.dispatchTouchEvent(ev);
}
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keycode= event.getKeyCode();
if (action == KeyEvent.ACTION_DOWN) {
if (keycode ==KeyEvent.KEYCODE_ENTER){
if (tmpflag) {
myView.setBackgroundColor(Color.RED);
tmpflag = false;
} else {
myView.setBackgroundColor(Color.BLUE);
tmpflag = true;
} }
return true;
}
return super.dispatchKeyEvent(event);
}
}
`

Android drawing

I am following a tutorial, and have the following code, but I want to modify it a little bit, so I want when its clicked inside the circle it must be filled with another collor, how I can do it, as I am just learning this concept I dont have any idea of how it can be done :/
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
}
}
you have to add a touch listener on your view
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View myView = new MyView(this);
setContentView(myView);
myView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
//if(x==... your code here
invalidate(); //this repaints the view
return false;
}
});
}
}

What is the mistake with my Android animation of my ImageView?

This code works only for the first animation. Though the final position of the ImageView is not exactly the point of the MotionEvent and the animation looks not smooth. The result of all other MotionEvents is actually no animation. The ImageView just appears at the point i touch the screen.
Here is the code.
ImageView image;
MyAnimationListener am;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
image = (ImageView)findViewById(R.id.imageView1);
am = new MyAnimationListener(image);
}
#Override
public boolean onTouchEvent(MotionEvent mEvent){
if(mEvent.getAction()==MotionEvent.ACTION_DOWN){
TranslateAnimation tAnimation = new TranslateAnimation(image.getX(),mEvent.getX(),image.getY(),mEvent.getY());
tAnimation.setDuration(500);
am.setCoordinates(mEvent.getX(), mEvent.getY());
tAnimation.setAnimationListener(am);
AnimationSet aSet = new AnimationSet(true);
aSet.addAnimation(tAnimation);
image.startAnimation(aSet);
}
return true;
}
And the interesting methods of MyAnimationListener.
ImageView i;
float x,y;
public MyAnimationListener(ImageView i){
this.i = i;
}
public void setCoordinates(float x, float y){
this.x = x;
this.y = y;
}
#Override
public void onAnimationEnd(Animation animation) {
i.setX(x);
i.setY(y);
}
Thanks in advance.
may be you need to do call a fillAfter so that the postion is not reset at the end of the animation. see link

Categories

Resources