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.
Related
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;
}
}
I have added drag functinality to my custom editext by overriding the touchevent()
Now the problem is after the edittext is dragged and dropped in a particular position and i want to input text into the edittext by clicking on it, it still getting dragged maybe because the touch event has been overriden and keyboard does not appear to input text
The workaround maybe triggering the dragfunctionality on long press but now the default longpress functionality of the edittext may change
I dont want this to happen
What to do.
mainactivity.java
public class MainActivity extends Activity
{
RelativeLayout dropLayout;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dropLayout = (RelativeLayout) findViewById(R.id.ondraglayout);
dropLayout.setOnDragListener(new MyDragListener());
EditText et = (EditText) findViewById(R.id.mainEditText1);
}
}
my customedittext.java
public class CustomEdittext extends EditText
{
public CustomEdittext(Context context){
super(context);
}
public CustomEdittext(Context context, AttributeSet attr){
super(context, attr);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
break;
case MotionEvent.ACTION_MOVE :
ClipData dragdata = ClipData.newPlainText("","");
View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(this);
this.startDrag(dragdata, shdwbldr, this, 0);
this.setVisibility(View.INVISIBLE);
break;
}
return true;
}
}
mydraglistener.java
public class MyDragListener implements OnDragListener
{
private RelativeLayout.LayoutParams params;
#Override
public boolean onDrag(View v, DragEvent event)
{
View view = (View) event.getLocalState();
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
params = (RelativeLayout.LayoutParams) view.getLayoutParams();
break;
case DragEvent.ACTION_DRAG_ENTERED:
int x = (int) event.getX();
int y = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED :
break;
case DragEvent.ACTION_DRAG_LOCATION :
x= (int) event.getX();
y = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED :
break;
case DragEvent.ACTION_DROP:
x = (int) event.getX();
y = (int) event.getY();
params.leftMargin = x;
params.topMargin = y;
view.setLayoutParams(params);
view.setVisibility(View.VISIBLE);
break;
default: break;
}
return true;
}
}
my main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#CDC2C0"
android:id="#+id/ondraglayout">
<com.mycompany.myapp.CustomEdittext
android:layout_height="wrap_content"
android:ems="10"
android:layout_width="wrap_content"
android:id="#+id/mainEditText1"/>
Your whole code not making sense at all: First let discuss what happening inside your onTouchEvent:
#Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
break;
case MotionEvent.ACTION_MOVE :
ClipData dragdata = ClipData.newPlainText("","");
View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(this);
this.startDrag(dragdata, shdwbldr, this, 0);
this.setVisibility(View.INVISIBLE);
break;
}
return true;
When you put your finger in the screen, the system first trigger MotionEvent.ACTION_DOWN, and afterwards, she will deliver events of MotionEvent.ACTION_MOVE as long as nothing else in the system will consume this events. The meaning of this is your call for startDrag() will call on each movement of the finger, not really make sense right? So first I suggest you to move the code from MotionEvent.ACTION_MOVE into MotionEvent.ACTION_DOWN.
Now to the more important part. When you assign a DragListener to View, the meaning is that this View will receive all the DragEvent that the system will deliver as long as the Listener returns true for the DragEvent.ACTION_DRAG_STARTED.
Now, you assigned your DragListener, to the RelativeLayout. So lets look into your code:
case DragEvent.ACTION_DROP:
x = (int) event.getX();
y = (int) event.getY();
params.leftMargin = x;
params.topMargin = y;
view.setLayoutParams(params);
view.setVisibility(View.VISIBLE);
break;
You set new position for the LayoutParams, but who is the View that will receive this params? The RelativeLayout, not your EditText. Actually your EditText is now INVISBLE as you set it visibilty to INVISIBLE inside your onTouchEvent and never changed it back. Your code "view.setVisibility(View.VISIBLE);" inside the ACTION_DROP is not referring to the EditText but to the RelativeLayout. Unless there is other code you not shown here, this is the case.
Anyway, in your case I would recommend you to move your startDrag() call to a LongCLickListener. I have no clue which functionality of the EditText you think that may change ,as, at least as far as I know, there is no functionality for LongClick in EditText. If you want to avoid it you can also add a flag to your code and turn it on after the ACTION_DROP, and then make the code inside the ACTION_DOWN to run just if this flag is set to false.
i want to create a Expandable listview with drag and drop feature i.e i can arrange inner or outer item through Drag and Drop
You create a drag event listener object ("listeners") from a class that implements View.OnDragListener.You set the drag event listener object for a View with the View object's setOnDragListener() method. Each View object also has a onDragEvent() callback method. Both of these are described in more detail in the developer section .
Refer this link for working sample :
bit.ly/16r49T3
You need to add a few listeners to get this to work. These listeners I placed in the OnStart of the fragment they appear in.
First Listener is as follows, assuming you called your listview "lvListView" (this was for dragging the child around, I found it elsewhere on here):
lvListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPosition = ExpandableListView.getPackedPositionGroup(id);
int childPosition = ExpandableListView.getPackedPositionChild(id);
// You now have everything that you would as if this was an OnChildClickListener()
// Add your logic here.
//include this:
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, lvItems.getItemAtPosition(position), 0);
// Return true as we are handling the event.
return true;
}
return false;
}
});
That allows you to drag the child around, but now you need a listener for all your DubSteppin' (the Drop). I placed the following just after that in my code:
lvListView.setOnDragListener(new AdapterView.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
Log.i("msg", "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Log.i("msg", Integer.toString(x_cord) + "," + Integer.toString(y_cord));
int nPointToPosition = lvListView.pointToPosition(x_cord,y_cord);
if(lvListView.getItemAtPosition(nCheck)!= null) {
// THE FUN PART IS HERE!
// ******this is the header list number******
int ngroupPosition = lvListView.getPackedPositionGroup(lvListView.getExpandableListPosition(nCheck));
// ******this is the child position******
int nchildPosition = lvListView.getPackedPositionChild(lvListView.getExpandableListPosition(nCheck));
}
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_ENDED");
// This is where I added some activities....
break;
case DragEvent.ACTION_DROP:
Log.i("msg", "ACTION_DROP event");
// This is also a good place, place with it and see what you want to do
break;
default:
break;
}
//return value
return true;
}
});//end DragListener
Sorry to "necropost", but I wanted to pay it forward, since usually I find my answers in code blocks like this on here.
I also got most of this from other code blocks and figured my way through it, so if you recognize your code, thank you!
You can refer to these two links :
http://developer.android.com/guide/topics/ui/drag-drop.html
http://github.com/commonsguy/cwac-touchlist/blob/master/src/com/commonsware/cwac/tlv/TouchListView.java
If someone is still looking for a solution, I modified the drag and drop listview and updated it to drag and drop expandable listview, check the answer here
In path application the user can drag the profile info down to reveal the image of the user .
I need same animation in our app . Can anyone guide me how to create this animation in android ?
I am talking about animation in http://cl.ly/MBQh
Thanks
i think that you need to look at the PullToRefresh Library.
Here you can configure your HeaderView of ListView. and It will appear then you pull down.
Also if you pull enough you cause event to be done.
You'd basically have two views, the one that user can drag, and the one hiding underneath, both of them children of a RelativeLayout.
The tricky part is the dragging/animation of the top view. Please, take this snippet as an example I wrote a time ago for a similar effect in x axis. Notice this logic is inside an extended View.-
final RelativeLayout wrapperDraggable = (RelativeLayout) findViewById(R.id.viewWrapperDraggable);
wrapperDraggable.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initPoint = new Point((int) event.getRawX(), (int) event.getRawY());
break;
case MotionEvent.ACTION_MOVE:
// Disable parent scrolling if user is dragging the view
if (dragging || Math.abs(initPoint.x - event.getRawX()) > Math.abs(initPoint.y - event.getRawY()) * 2) {
dragging = true;
getParent().requestDisallowInterceptTouchEvent(true);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
}
int offset = (int) Math.max(Math.min(0, event.getRawX() - initPoint.x), -CurrentViewClass.this.getWidth() * 0.45f);
if (offset != lastOffset) {
TranslateAnimation anim = new TranslateAnimation(lastOffset, offset, 0, 0);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setDuration(0);
wrapperDraggable.startAnimation(anim);
lastOffset = offset;
}
break;
case MotionEvent.ACTION_UP:
if (!dragging) {
// The user clicked the view
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
dragging = false;
TranslateAnimation anim = new TranslateAnimation(lastOffset, 0, 0, 0);
anim.setFillAfter(true);
anim.setDuration(300);
wrapperDraggable.startAnimation(anim);
lastOffset = 0;
break;
}
return true;
}
});
Hope you find it useful.
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?