I have a view in the UI that is technically an extended ImageView inside a RelativeLayout that I'd rather not change, if possible. I want to show a Drawable from resources (a small PNG with a gradient or a frame) at specific coordinates as a visual feedback when user is tapping the screen (there even is a onShowPress callback). What is the simplest way to do this? I don't want to modify the layout.xml from which UI is inflated, and I'm not too enthusiastic about overriding onDraw to do extra job there.
This is one easy way to do it using view animation class:
final ImageView aboutButton = (ImageView) findViewById(R.id.AboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation myAnim2 = AnimationUtils.loadAnimation(LaunchActivity.this, R.anim.buttons);
aboutButton.startAnimation(myAnim2);
myAnim2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//do on click after animation ends
Intent intent = new Intent(LaunchActivity.this, MyDialog.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
//change the backround of the button etc on click etc...
}
});
}});
Related
I want to make it so that once I press a button, it moves and does an animation by quickly swapping the src of the imageview mid animation.
I've tried to use Thread.sleep .
first.setImageResource(R.drawable.cata2);
first.setImageResource(R.drawable.catb2);
ObjectAnimation Code
x += 10
first.setImageResource(R.drawable.cata2);
ObjectAnimator d = ObjectAnimator.ofFloat(first, "translationX", x);
d.setDuration(50);
d.start();
It always just switches to cata2.
Try this one. If the changing image resource not working, try to change it in background resource. Just adjust some duration and the thread sleep.
in C#:
Try to put this all inside the listener of your button
Setting up the Animation
var clockwise = AnimationUtils.LoadAnimation(this.Activity, Resource.Animation.clockwise);
clockwise.Duration = 2000;
To set your first to cata2
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.cata2);
});
To change with animation from cata2 to catb2
Thread t = new Thread(delegate ()
{
first.StartAnimation(clockwise);
Thread.Sleep(500);
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.catb2);
first.ClearAnimation();
return;
});
});
t.Start();
We can add a listeter to your animation
d.addListener(this);
Then generate overridess
#Override
public void onAnimationStart(Animator animation){
//add some codes
}
#Override
public void onAnimationEnd(Animator animation){
//add some codes
}
#Override
public void onAnimationRepeat(Animator animation){
//add some codes
}
#Override
public void onAnimationCancel(Animator animation){
}
I am trying to make a animation on a textView and to change the content on onAnimationEnd but it change jump one text and take the next one. I don't know if you understand great, my english is not so good.
This is my code:
final Integer[][] listeQuestions = new Integer [][]{
{R.string.c1}, {R.string.c2}, {R.string.c3}, {R.string.c4}, {R.string.c5}, {R.string.mot1}, {R.string.c6}, {R.string.c7}, {R.string.c8}, {R.string.c9}, {R.string.c10}, {R.string.mot2},
{R.string.c11}, {R.string.c12}, {R.string.c13}, {R.string.c14}, {R.string.c15}, {R.string.mot3}, {R.string.c16}, {R.string.c17}, {R.string.c18}, {R.string.c19}, {R.string.c20}, {R.string.mot4},
{R.string.c21}, {R.string.c22}, {R.string.c23}, {R.string.c24}, {R.string.c25}, {R.string.mot5}, {R.string.c26}, {R.string.c27}, {R.string.c28}, {R.string.c29}, {R.string.c30}, {R.string.mot6},
{R.string.c31}, {R.string.c32}, {R.string.c33}, {R.string.c34}, {R.string.c35}, {R.string.mot7}
};
boutonResultat.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
resultatTexte.setVisibility(View.VISIBLE);
resultatTexte.setText(listeQuestions[0][0]);
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.laps_temps);
resultatTexte.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
Integer curseur=0;
#Override public void onAnimationStart(Animation animation) {}
#Override public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
if(curseur<42) {
curseur++;
resultatTexte.setText(listeQuestions[curseur][0]);
animation.setAnimationListener(this);
resultatTexte.startAnimation(animation);
}
}
});
My animation is simple, it's just a alpha who begin at 0.2 to 1.0 and the duration is 3000.
I tryied a lot of possibilities (change the animation duration, put some elements in onAnimationStart, put a new code to try to block the double, I leaved the setAnimationListener in onAnimationEnd ...) but always the same problem: R.string.c1 appear great and the animation is correct, when it's finish, R.string.c2 appear maybe 0.1seconds and directly appear R.string.c3 and the animation work correctly, and R.string.c4 appear 0.1 seconds and R.string.c5 works great and like this to the end.
Maybe someone already had an equivalent problem and can help me to resolve it.
Thank you and sorry for my english :)
I'm new to android, and I'm facing a problem where I'm trying to make my ImageView, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the Imageview becomes invisible. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it!
To override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the Imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?
This code change visibility only after the animation is done
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
face.setVisibility(View.INVISIBLE);
}
});
}
this code changes the animation "while" the original is playing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
face.startAnimation(hideAnimation);
}
});
}
You should not call face.setVisibility(...), instaed tt should be:
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.INVISIBLE);
}
});
I'm trying to change the style attribute "colorControlNormal" of my app programmatically and during runtime, but I didn't have any results.
This property is the color that will tint the hamburger & back icons of the new Toolbar viewGroup. Beside, I'm using the v7 compatibility library.
I heard that we cannot change app theme during runtime, but I'm looking for an answer, even if it's not so clean way.
Edit:
I just figured that gmail is doing what i want, when you click on the search icon, the white hamburger icon turn into grey back.
Waiting for more.
I spent one day, played with different implementation. So my opinion, the best way todo that it copy paste DrawerArrowDrawable from AppCompat v7 library.
https://gist.github.com/IstiN/5d542355935fd7f0f357 - take a look on the code with some optimization
than you can use it in your main activity with code below
DrawerArrowDrawable drawable = new DrawerArrowDrawable(this, this);
ImageView menuButton = (ImageView) findViewById(R.id.arrow);
menuButton.setImageDrawable(drawable);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((DrawerLayout)findViewById(R.id.drawer)).openDrawer(Gravity.START);
}
});
when you start new fragment, you need to create one more view on the same place and add second code to your fragment
private DrawerArrowDrawable mArrowDrawable;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mArrowDrawable = new DrawerArrowDrawable(getActivity(), getActivity());
ImageView topButton = (ImageView) view.findViewById(R.id.arrow);
topButton.setImageDrawable(mArrowDrawable);
topButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeSearch();
}
});
//run animation from hamburger to arrow
animate(0, 1, null);
....
private void animate(int startValue, int endvalue, Animator.AnimatorListener listener) {
ValueAnimator anim = ValueAnimator.ofFloat(startValue, endvalue);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mArrowDrawable.setProgress(slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(300);
if (listener != null) {
anim.addListener(listener);
}
anim.start();
}
to make animation from arrow to hamburger handle back button and execute code
animate(1, 0, null);
you also need to wait in your fragment while animation will not finish, but it another questions.
If you have any questions ask in comments.
Now my question is this, is there any way to apply any default animation when each of the layout is set to visible? Instead of appearing all of a sudden, any sliding animation would look sleek.
//to show home page
homeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
liMenu.setVisibility(View.VISIBLE);
}
});
//to show .net page
dotnetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
liDotnet.setVisibility(View.VISIBLE);
}
});