How can I change animated drawable(ball image) with other drawable when I met condition?
When ball hits bottom edge of display I want to change one (red ball image) with other (yellow ball image). Precisely, when ball hits bottom I want to change color(drawable) with yellow ball, but when bounce back(goes up) I want to change back to red. So I want ball change drawables when goes up and down until it stops.
I wrote code in android studio(java).
public class MainActivity extends AppCompatActivity {
private static final String TAG = "AnimationStarter";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bounceBallButton = (Button) findViewById(R.id.bounceBallButton);
final ImageView bounceBallImage = (ImageView) findViewById(R.id.bounceBallImage);
bounceBallButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
bounceBallImage.clearAnimation();
TranslateAnimation transAnim = new TranslateAnimation(0, 0, 0,
getDisplayHeight() - 530);
transAnim.setStartOffset(500);
transAnim.setDuration(5000);
transAnim.setFillAfter(true);
transAnim.setInterpolator(new BounceInterpolator());
transAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.i(TAG, "Starting button dropdown animation");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Log.i(TAG,
"Ending button dropdown animation. Clearing animation and setting layout");
bounceBallImage.clearAnimation();
final int left = bounceBallImage.getLeft();
final int top = bounceBallImage.getTop();
final int right = bounceBallImage.getRight();
final int bottom = bounceBallImage.getBottom();
bounceBallImage.layout(left, top, right, bottom);
}
});
bounceBallImage.startAnimation(transAnim);
}
});
}
private int getDisplayHeight() {
return this.getResources().getDisplayMetrics().heightPixels;
}
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.
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;
}
});
}
}
I am making some kind of puzzle game where there will be a lot of blocks that are made of ImageView(s). Now in this case, I have only setOnClickListener to imageView1, now rather than me writing the same block of setOnClickListener for each ImageView, how do I write the setOnclickListener once for all the ImageView(s) ??
Here's the sample code
MainActivity.java
public class MainActivity extends Activity {
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
imageView1.startAnimation(animation);
x += 0;
y += 50;
}
});
}
private class MyAnimationListener implements Animation.AnimationListener {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageView1.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageView1.getWidth(),
imageView1.getHeight());
lp.setMargins(x, y, 0, 0);
imageView1.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
}
All suggestions and improvements of my codes are greatly welcomed. Thanks in advance.
You can try to create your listener in a variable and then assign it to each ImageView. Something like this:
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
View.OnClickListener listener= new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
if(v==imageView1)
imageView1.startAnimation(animation);
else if(v==imageView2)
imageView2.startAnimation(animation);
else if(v==imageView3)
imageView3.startAnimation(animation);
x += 0;
y += 50;
}
};
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
Try to use this:
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
View.OnClickListener listener= new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
ImageView vista=(ImageView) v;
animation.setAnimationListener(new MyAnimationListener(vista));
vista.startAnimation(animation);
x += 0;
y += 50;
}
};
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
private class MyAnimationListener implements Animation.AnimationListener {
ImageView selected;
public MyAnimationListener(ImageView vista)
{
selected=vista;
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
selected.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(selected.getWidth(),
selected.getHeight());
lp.setMargins(x, y, 0, 0);
selected.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
You can implement your custom OnClickListener with the Activity like below,
public class MainActivity extends Activity implements View.OnClickListener{
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener(v));
v.startAnimation(animation);
x += 0;
y += 50;
}
private class MyAnimationListener implements Animation.AnimationListener {
private View mView;
public MyAnimationListener(View v)
{
this.mView = v;
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mView.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mView.getWidth(),
mView.getHeight());
lp.setMargins(x, y, 0, 0);
mView.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
}
Create a distinct listener:
View.OnclickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
v.startAnimation(animation);
x += 0;
y += 50;
}
};
then set the listener to your views:
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
You can always extend your class as activity and implement OnClickListener. Then you should override the OnClick function. It takes a view as parameter that you can check if it is your intended view (i.e. your Imageviews)
eg:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Rohan on 15-09-2015.
*/
public class MyActivity extends Activity implements View.OnClickListener{
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.image1 || v.getId() == R.id.image2 || v.getId() == R.id.image3 ){
//Do your stuff
}
}
}
Let MainActivity implements OnClickListener and call
public class MainActivity extends Activity implements OnClickListener {
ImageView imageView1, imageView2, imageView3;
int x, y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = 0;
y = 0;
imageView1 = (ImageView) findViewById(R.id.image1);
imageView2 = (ImageView) findViewById(R.id.image2);
imageView3 = (ImageView) findViewById(R.id.image3);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.image1:
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 50);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
imageView1.startAnimation(animation);
x += 0;
y += 50;
break;
case R.id.image2:
break;
case R.id.image3:
break;
}
}
}
Although your code is in Java, there is an easy way in kotlin that someone could find useful. You can create an extension function for a list of views.
Extension function:
fun List<View>.setOnClickListener(onClickView: () -> Unit) {
this.forEach { it.setOnClickListener { onClickView.invoke() } }
}
How to use it:
listOf(ImageView, ImageView2, ImageView3).setOnClickListener {
// put your action here
}
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;
}
});
}