I am developing an small application in a android. Before this i had done surfing on internet but i can't get useful material. In this application i have two tabs I had made the these two tabs but I want after click on first tab i want some images these Images can move on right side when user touch it on right and left side when user touch it left means user can slide the images on both left and right by finger. Can anyone suggest me any tutorial or source code so that i can able to do functionality like this.
Thanks in advance
See this blog post on how to do it. You need to look at SurfaceView and the onTouchListener. Taken from the blog:
surf.setOnTouchListener( new SurfaceView.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
case MotionEvent.ACTION_MOVE:
if( moving ){
final int x_new = (int)event.getX();
final int y_new = (int)event.getY();
mDrawTiles.draw( new DrawLogic(){
#Override
public void draw(Rect _surface) {
mTiles.setBounds(
x_new - mDrawWidth/2,
y_new - mDrawHeight/2,
x_new + mDrawWidth/2,
y_new + mDrawHeight/2);
}
});
}
int x1 = (int) event.getX();
int y1 = (int) event.getY();
imageobject.layout(x1,x1+(width_of_imageobject),y1,y1+(width_of_imageobject));
Related
Is it possible? I like to drag the floating button across the screen anywhere the user want. Like make the floating button movable. For example if the user wants it on the top - right , he will drag the button to the top-right of the screen.
I wonder if it's possible. If so, how?
Yes, you can move views across the screen by setting its x and y coordinates.
So, in your case, as you want to move floating button on the screen according to users inputs(Drag and drop), you have to get the coordinates of user touch on the screen using MotionEvent.ACTION_MOVE action and set that coordinates to your button at the same time.
Try the following code:
yourFloatingButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me){
if (me.getAction() == MotionEvent.ACTION_DOWN){
oldXvalue = me.getX();
oldYvalue = me.getY();
Log.i(myTag, "Action Down " + oldXvalue + "," + oldYvalue);
}else if (me.getAction() == MotionEvent.ACTION_MOVE ){
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight(),(int)(me.getRawX() - (v.getWidth() / 2)), (int)(me.getRawY() - (v.getHeight())));
v.setLayoutParams(params);
}
return true;
}
});
Hope it helps you!
I'm working on an Android app and I want to add some eyecandy to the UI. I have an activity (let's call it MainActivity) which has some text fields, buttons and a gallery on it.
What I would like to achieve is: When the user touches somewhere on this activity, there should be some visual effect at the point where he touched (e.g. something like sparkles etc.).
So the essential parts of my question are:
a) How can I determine where the user touched
b) how can I draw my effects on to the screen (as an 'overlay' to the activity).
Thanks in advance for every helpful answer.
(I've checked out this answer but it doesn't seem to be applicable to my situation. )
First, you have to add onTouchListener to your root Layout, then you can get coordinates, where the user has touched the screen.
example :
float x,y;
rl = (RelativeLayout) findViewById(R.id.root_layout);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return super.onTouchEvent();
}
});
You should override the onTouchEvent method of your activity. You can then obtain the coordinates of the touch event:
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
break;
case MotionEvent.ACTION_MOVE:
// movement
break;
case MotionEvent.ACTION_UP:
// touch up
break;
default:
// default
}
return super.onTouchEvent(event);
}
To answer further on your question, to have a overlay, look at the APIDemos -> Graphics -> OpenGL ES -> Translucent GLSurfaceView. This will help you to create overlay on your activity. There may be some other example in API demos which will get you some help. Android's API Demos are good set of examples to address some known issues.
If you are working on ICS then I recommend you to look at the Developer Options in Settings application. If you are not working with ICS, I believe you can look at the APIDEMOS for touchevents. They draw lines on touches. The code in that, can get you started.
I've make a method which moved my ImageView via my touch points , But I wanna see the refreshes like Motion tween in Adobe flash if it possible , here is my code :
int x=0;
int y=0;
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int ActionEvent = event.getAction();
switch (ActionEvent){
case MotionEvent.ACTION_MOVE:{
x = (int) event.getX();
y = (int) event.getY();
}
break;
case MotionEvent.ACTION_UP:{
AbsoluteLayout.LayoutParams ActionMove = new
AbsoluteLayout.LayoutParams(FstBall.getLayoutParams());
ActionMove.x = x;
ActionMove.y = y;
FstBall.setLayoutParams(ActionMove);
}
break;
}
return true;
}
Any ideas?
First of all, you do not want to use AbsoluteLayout as it is deprecated. There are plenty of tutorials on Android Drag-and-Drop. Here's one I recall being useful. I believe it uses FrameLayout and adjusts the left and top margins. You'll also want to check the API Demos. I think I remember an example project there. What do you mean by Adobe motion tween?
Desired effect
I have a bunch of small images that I'd like to show on a "wall" and then let the user fling this wall in any direction and select an image.
Initial Idea
as a possible implementation I was thinking a GridView that is larger than the screen can show - but all examples of using this widget indicate that the Gallery doesn't extend beyond the size of the screen.
Question
What is the best widget to use to implement the desired effect ?
A code sample would be especially beneficial.
EDIT...
if someone has example code that will let me put about 30 images on a "wall" (table would be good) then I will accept that as the answer. Note that the "wall" should look like it extends beyond the edges of the display and allow a user to use the finger to drag the "wall" up down left right. Dragging should be in "free-form" mode. A single tap on an image selects it and a callback shall be detectable. I have created a bounty for this solution.
The solution is actually quite simple, but a pain. I had to implement exactly this within a program I recently did. There are a few tricksy things you have to get around though. It must be noted that different OS versions have different experiences. Certain expertise or cludging around is required to make this work as drawing is sometimes adversely affected by this method. While I have a working copy, the code I provide is not for everyone, and is subject to whatever other changes or customizations have been made in your code.
set android:layout_width to wrap_content
set android:layout_height to wrap_content
In your code:
determine how many rows you will have.
divide number of items by the number of rows.
add gridView.setStretchMode(NO_STRETCH);
add gridView.setNumColumns( number of Columns );
add gridView.setColumnWidth( an explicit width in pixels );
To Scroll:
You may simply use gridView.scrollBy() in your onTouchEvent()
These are the steps. All are required in order to get it to work. The key is the NO_STRETCH with an explicit number of columns at a specific width. Further details can be provided, if you need clarification. You may even use a VelocityTracker or onFling to handle flinging. I have snappingToPage enabled in mine. Using this solution, there is not even a requirement to override onDraw() or onLayout(). Those three lines of code get rid of the need for a WebView or any other embedding or nesting.
Bi-directional scrolling is also quite easy to implement. Here is a simple code solution:
First, in your class begin tracking X and Y positions by making two class members. Also add State tracking;
// Holds the last position of the touch event (including movement)
int myLastX;
int myLastY;
// Tracks the state of the Touch handling
final static private int TOUCH_STATE_REST = 0;
final static private int TOUCH_STATE_SCROLLING = 1;
int myState = TOUCH_STATE_REST;
Second, make sure to check for Scrolling, that way you can still click or longclick the images themselves.
#Override public boolean onInterceptTouchEvent(final MotionEvent ev)
{//User is already scrolling something. If we haven't interrupted this already,
// then something else is handling its own scrolling and we should let this be.
// Once we return TRUE, this event no longer fires and instead all goes to
// onTouch() until the next TouchEvent begins (often beginning with ACTION_DOWN).
if ((ev.getAction() == MotionEvent.ACTION_MOVE)
&& (myState != TOUCH_STATE_REST))
return false;
// Grab the X and Y positions of the MotionEvent
final float _x = ev.getX();
final float _y = ev.getY();
switch (ev.getAction())
{ case MotionEvent.ACTION_MOVE:
final int _diffX = (int) Math.abs(_x - myLastX);
final int _diffY = (int) Math.abs(_y - myLastY);
final boolean xMoved = _diffX > 0;
final boolean yMoved = _diffY > 0;
if (xMoved || yMoved)
myState = TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_DOWN:
// Remember location of down touch
myLastX = _x;
myLastY = _y;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (myState == TOUCH_STATE_SCROLLING)
// Release the drag
myState = TOUCH_STATE_REST;
}
//If we are not At Rest, start handling in our own onTouch()
return myState != TOUCH_STATE_REST;
}
After the GridView knows that you are Scrolling, it will send all Touch Events to onTouch. Do your Scrolling here.
#Override public boolean onTouchEvent(final MotionEvent ev)
{
final int action = ev.getAction();
final float x = ev.getX();
final float y = ev.getY();
final View child;
switch (action)
{
case MotionEvent.ACTION_DOWN:
//Supplemental code, if needed
break;
case MotionEvent.ACTION_MOVE:
// This handles Scrolling only.
if (myState == TOUCH_STATE_SCROLLING)
{
// Scroll to follow the motion event
// This will update the vars as long as your finger is down.
final int deltaX = (int) (myLastX - x);
final int deltaY = (int) (myLastY - y);
myLastX = x;
myLastY = y;
scrollBy(deltaX, deltaY);
}
break;
case MotionEvent.ACTION_UP:
// If Scrolling, stop the scroll so we can scroll later.
if (myState == TOUCH_STATE_SCROLLING)
myState = TOUCH_STATE_REST;
break
case MotionEvent.ACTION_CANCEL:
// This is just a failsafe. I don't even know how to cancel a touch event
myState = TOUCH_STATE_REST;
}
return true;
}
If course, this solution moves at X and Y at the same time. If you want to move just one direction at a time, you can differentiate easily by checking the greater of the X and Y differences. (i.e. Math.abs(deltaX) > Math.abs(deltaY)) Below is a partial sample for one directional scrolling, but can switch between X or Y direction.
3b. Change this in your OnTouch() if you want to handle one direction at a time:
case MotionEvent.ACTION_MOVE:
if (myState == TOUCH_STATE_SCROLLING)
{
//This will update the vars as long as your finger is down.
final int deltaX = (int) (myLastX - x);
final int deltaY = (int) (myLastY - y);
myLastX = x;
myLastY = y;
// Check which direction is the bigger of the two
if (Math.abs(deltaX) > Math.abs(deltaY))
scrollBy(deltaX, 0);
else if (Math.abs(deltaY) > Math.abs(deltaX))
scrollBy(0, deltaY);
// We do nothing if they are equal.
}
break;
FuzzicalLogic
A GridView can have content that extends beyond the size of the screen, but only in the vertical direction.
Question: What is the best widget to use to implement the desired effect ?
The are no default widgets (at least prior to 3.0) which implement 2D scrolling. All of them implement 1D (scrolling in one direction) only. Sadly the solution to your problem is not that easy.
You could:
Place a TableLayout inside a custom ScrollView class, and handle the touch events yourself. This would allow you to do proper 2D panning, but would not be so good for large data-sets since there would be no view recycling
Modify GridView if possible to enable horizontal scrolling functionality (probably the best solution if you can do it)
Try putting a TableLayout inside a ScrollView (with layout_width="wrap_content) which you put inside a HorizontalScrollView - though this may work it wouldn't be the optimum solution
Do something with OpenGL and draw straight to a canvas - this is how the default Gallery 3D app does it's "wall of pictures"
I think that the only Android native solution for this is to have an embebed WebView in which you can have that bi-dimensional scroll (and by the way is the only thing that has this capability).
Be pressing an image you can control the behavior by a callback from JavaScript to JAva.
This is not very "pretty" but... it's the only viable way to do what you want "out of the box".
I have implemented a GridView that overrides the onInterceptTouchEvent methods. I set the OnTouchListeneras explained by Fuzzical Logic. My issue is that setting a custom OnTouchListener, the objects of my gridView are not clickable anymore. Is that because the default OnTouchListener of a GridView calls onItemClickListener ?
I would like to have a 2 directions scrollable GridView but that has still cliackable objects. Should I implement my own method to check whether a touch matches a item or is there a easier way to achieve this ?
Grodak
i am developing game.i am displaying gun object center bottom of the screen.when user tap on screen i need to rotate gun that direction.i done rotating image.but when user tap on screen i need to rotate image that direction.can you please help me
Thanks in advance
Aswan
is your image a bitmap? could you convert it to one? Would an onClick listener not work and when the user clicks do something like.. http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Seeing some code as to what you've attempting would be nice also. The following will help you with checking if the bitmap has been clicked in case you're stuck on that also http://developer.android.com/reference/android/view/View.html .
I've never attemped this myself but I think that would work. Try redrawing the bitmap when you have resized it.
Just use onTouchListener for your View and use this code for that
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
Log.i(TAG, "action type is"+event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
Log.i(TAG, "Entering in onTouch");
double rotationAngleRadians = Math.atan2(currentX - dialer.centerX, dialer.centerY - currentY);
dialer.rotationAngle = (int) Math.toDegrees(rotationAngleRadians);
Log.i(TAG, "rotaion angle"+dialer.rotationAngle);
dialer.invalidate();
return true;
}
}
return true;
}