Rotate a button (or the text inside) in code - android

I have to rotate a button (or the text inside, it's the same) by random degree by coding. Is there any button.setRotate(x) in API level lower then 11??

Ok, had a look and the answer is: It's complicated.
You can rotate the button using the old animation framework, e.g. like this:
Button button = (Button) findViewById(R.id.button);
// rotation from 0 to 90 degrees here
RotateAnimation a = new RotateAnimation(0, 90);
a.setFillAfter(true);
a.setDuration(0);
button.startAnimation(a);
The problem here is that the button looks rotated, but can't be clicked correctly. The coordinates that trigger the click event are the ones at the area the button had before beeing rotated.
Since this is not a very good solution, your best bet is probably to write a custom view that extends the Button class and rotate the buttons canvas in onDraw(). You also have to override onMeasure() in this case. See Custom Components for a introduction what to do.
Apart from that you can try to intercept click events from the buttons parent layout and trigger the appropriate event when the click happened within the buttons current coordinates. This is somewhat "hacky" though.

Related

Android prevent views overlap on drag and drop

I am adding some ImageViews to a relative layout dynamically (on touch points)
Any ImageView has an OnTouchListener. In Action Move I move the ImageView(sth like Drag and Drop).
My problem is that ImageViews may overlap each other when they are moving. I want to prevent them from overlap in any way! (best is stop moving when collision happen)
Look at this image
All ImageViews are saved in a link list. I get the x & y of moving view and compare it with other views and if they had collision don't let them moving.
But I want that user can make them separate again. How can I do this work?
In brief when views collision happen I don't let them move any more but I want if the move was making them far from each other it happens.
Try this
Rect rc1 = new Rect();
imageView1.getDrawingRect(rc1);
Rect rc2 = new Rect();
imageView2.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2) {
// intersection is detected
// here is your method call
}

Transparent button Eclipse

I'm having troubles with a transparent button background. I have a 90 degree triangle with the second side transparent. The problem I'm facing is that you can activate the image button by pressing on the transparent side. is there anyway to limit it through the first side? Thanks.
ps: I'm sorry i'm new to all this.
You can achieve that by setOnTouchListener for your button (not OnClick) then you can calculate the onRawX and onRawY then compare the values if it's on your sensitive region do your work else ignore.
Yes it is possible, but you have to do it manually. It means you should build your own View class and override public boolean onTouchEvent(MotionEvent event) method. and then calculate if the x and y coordinates mathces your region.

How to drag button on screen and give translate animation from current position to original position?

I want to translate animation perform on button. Here Buttons are drag able in layout. I drag the button on any place of screen and when I remove my touch,I want to perform transform animation from current drop able points of button to original place which is initially button located on the screen. if any one have idea then well come.
You can animate any View very simply with a View Property Animator like this:
button.animate().translationX(deltaX)
.translationY(deltaY)
.setDuration(duration);
This works on API level 11 and above. If it is supposed to work before API level 11 then you need to use View Animations:
TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
animation.setDuration(duration);
button.startAnimation(animation);
If you have any further question feel free to ask.

How to set OnClickListener to rotated ImageView?

I need to set an OnClick listener on a rotated rectangle ImageView. I've got problem when setting itafter rotation (angle 45). When i rotated my ImageView, region of the OnClickListener answers in the same place as before rotate :(
Any ideas?
Tween Animations doesn't really "move" the View, it just alters where it is drawn, i.e. you're going to have to add an AnimationListener and resize (in your case, changing the margins should work)/move the object after the animation has completed for the "physical" location to update.
It's a lot better in Honeycomb / Ice cream sandwich with property animations.

Reversing view animation on Android?

So if I have a translation animation like
Animation anim = new TranslateAnimation(0, 0, 0, OFFSET);
anim.setDuration(1000L);
anim.setAnimationListener(listener);
anim.setFillAfter(true);
and I apply this to a textswitcher so that given a offset, the textswitcher will move from a location, A, up or down.
My textswitcher will stay at a given location, B, after the animation due to the setfillafter. How can I then from that location, B, reverse the animation and go back to location, A?
The Overall Question
So I'm translating a textswitcher down and then up, but of course, setfillafter only translates the bitmap of the view and not the view itself, so this may not be the best approach. I've also tried to do an animation and then offset of the view, but that looks glitchy. So basically, I want a view(textswitcher) which a swipe/fling down will move it down and touching(onClick) it when it's down will move it back up (same animation reversed). How can I do this?
I know how to do the swipe/fling and onClick stuff, just not how to implement the correct animation.
Perhaps you may want to consider adding an on touch listener to see where you touched it last, If above move up with a constant animation. This is if im understanding the question correctly.

Categories

Resources