ImageView move refreshes? - android

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?

Related

How to improve scrolling in android webview?

i'm loading a website into an android webview and i would like to improve scrolling performance. In other words i would like to make scrolling faster and smoother for the user. Any ideas?
Thanks in advance
did you declare that you want software rendering in your application's manifest (or by setting the WebView's layer type)? Maybe try hardware mode
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
I also encountered sluggish scrolling with erratic fling scrolling when embedding WebView in a fragment inside a HorizontalScrollView, but it seems to happen in other cases also. After days of trial and error, here is a solution that seems to work. The critical part is the OnTouchListener; I don't believe that the WebView settings are that important. It is counterintuitive, but you have to explicitly tell the WebView to scroll... seems like an Android bug.
WebView myWebView;
VelocityTracker mVelocityTracker;
myWebView.setOnTouchListener(new WebViewOnTouchListener());
private class WebViewOnTouchListener implements View.OnTouchListener {
float currY = 0;
float yVeloc = 0f;
WebView localWebView = myWebView;
#Override
public boolean onTouch(View v, MotionEvent event) {
int index = event.getActionIndex();
int action = event.getActionMasked();
int pointerId = event.getPointerId(index);
switch(action){
case MotionEvent.ACTION_DOWN:
if (mVelocityTracker == null) {
// Retrieve a new VelocityTracker object to watch the velocity
// of a motion.
mVelocityTracker = VelocityTracker.obtain();
} else {
// Reset the velocity tracker back to its initial state.
mVelocityTracker.clear();
}
mVelocityTracker.addMovement(event);
currY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
yVeloc = mVelocityTracker.getYVelocity(pointerId);
float y = event.getY();
localWebView.scrollBy(0, (int) (currY - y));
currY = y;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
localWebView.flingScroll(0, -(int)yVeloc);
break;
}
return true;
}
}

How do I detect if a user has touched any part of the screen other than the inside of an EditText?

I am guessing this means making the whole screen touchable. How exactly is that done?
and secondly calculating if an X,Y is within the EditText.
override the onTouchEvent in the activity you want to implement this....and get the X, Y co-ordinates using event.getX() and event.getY()
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
but i suggest you must search thoroughly before posting a question.

Android - TranslateAnimations

When I try to apply a translate animation on an ImageView, the image just completely disappears. I am not sure why this would happen because I ran this code through the eclipse debugger and the value for the x_start and x_final seem to be correct.
Any ideas on why this would be happening or how I can end up getting my TranslateAnimation to work?
chargeButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
switch (event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
chargeButton.setLayoutParams(setPosition(x_cord, false));
break;
case MotionEvent.ACTION_UP:
int x_start = x_cord;
x_cord = 0;
slowMove(x_start, x_cord, false);
chargeButton.setLayoutParams(setPosition(x_cord, false));
break;
default:
break;
}
return true;
}
});
}
public void slowMove(int x_start, int x_final, boolean pay)
{
Animation transAnimation = new TranslateAnimation(x_start, x_final, 0, 0);
transAnimation.setFillAfter(true);
transAnimation.setDuration(1000);
if (pay)
payButton.startAnimation(transAnimation);
else
{
chargeButton.clearAnimation();
chargeButton.startAnimation(transAnimation);
}
}
A little more background, this function is being called from an onTouchListener under the MotionEvent.ACTION_UP case.
I feel as though I need to use Animation.Relative_TO_SELF or something like that for the positioning. However, I am not sure how to do that when I only have the absoute positioning of the ImageViews.
Any and all ideas will be greatly appreciated.
Do not use the layout parameters to move your views - it is really not a good idea. I have been down that road and nothing good came out of it.
Consider extending View and setup your own custom view where you can have full control of what goes on.

Is there a way in android to get the area (a collection of points) of my finger

Is there a way in android to get the area (a collection of points) of my finger when i touch the screen. Usually, the return is one point location. I need to get the several points which can represent the area of my touching. Any one knows? Thnaks
Just use View class's onTouchEvent() or implements OnTouchListener in your activity and get the X and Y co-ordinates like,
This is View class's OnTouchEvent()..
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
float x = ev.getX();
float y = ev.getY();
break;
}
case MotionEvent.ACTION_MOVE: {
float mLastTouchX = x;
float mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_CANCEL: {
break;
}
case MotionEvent.ACTION_POINTER_UP: {
break;
}
}
return true;
}
EDIT:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
For more details Android - View.
Does it have to be the real shape of the finger? Otherwise you could calculate a circle around the center of the touch.
I believe this is not possible in the touch's coordinate along with it's area. It's natively unsupported by Android API.
Ref:
http://developer.android.com/guide/topics/ui/ui-events.html
http://developer.android.com/reference/android/gesture/package-summary.html
Unfortunately not. I ran in to this a while ago - you could try normalising the data over several 'frames'. May be worth trying to figure out why you need an exact point, or set of points, and find another way of doing it.

Drag and drop the textview on imageview?

can any one help me how to drag and drop the text view on entire image view. i found from this drag n drop textview in android here the text view is static, in my app textview and image view's are dynamically change. `#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
}
return true; //done with this event so consume it
}
i wright this code, but it's not working properly.
You can always change the image and text of existing view programatically using setText sort of methods.
There are many drag and drop tutrials if you google it. strongly suggest you try them out.
Basically what you do is implement a OnTouchListener on the TextView like
textview.setOntouchListener(listen);
OnTouchListener onThumbTouch = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch(v.getId())
{
case R.id.slider_thumb_id:
{
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
}
case MotionEvent.ACTION_UP:
{
}
}
break;
}
}
return true;
}
};
Here from the event attribute you get the value of x and y and the touch event and
use that to alter the position of the textview. I did it by add add/subtracting the margin values.
EDIT:
This is what I did to update the position of my view
Look event.getRawY(); is going to give you the distance from the parent view. So if I get event.getRawY() = 50. I know I need the text view to be 50 pixels from the top.
LinearLayout.LayoutParams iconParams = (LinearLayout.LayoutParams)
v.getLayoutParams();
iconParams.topMargin = 50;
v.setLayoutParams(iconParams);
So now the view that i selected is 50 px from the top of the parent view.

Categories

Resources