| Author |
Message |
mmin18 Junior Developer

Joined: 03 Feb 2008 Posts: 20 Location: China
|
Posted: Fri Apr 18, 2008 9:13 am Post subject: [Custom Widget] - HandyFlipper: makes everything flip |
|
|
HandyFlipper is a custom widget inherited from ViewAnimator. Is is first designed to use in my project handyCalc.
Here is a demo to show how it works. (sorry about the color, it's a gif)
Or you can watch the handyCalc video demo in http://www.anddev.org/my_submission_is_a_calculator-t1704.html
There is some other widget in android.widget that you can flip, like Spinner, Gallery. In Spinner, you cannot flip if it's child is focusable, like a button in a Spinner.
Now i'll just show you how to use it. you can view the code below or download a package and import in your eclipse to run it yourself: http://www.anddev.org/files/handycalc_library_155.zip
It's just a layout file to make it work:
| XML: | <?xml version="1.0" encoding="utf-8"?>
<org.mmin.handycalc.HandyFlipper xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:handy="http://schemas.android.com/apk/res/org.mmin.handycalc"
handy:flipHorizontal="true"
handy:flipVertical="true"
handy:circle="true"
handy:motionSpeed="60"
handy:motionInterval="500"
handy:motionDistance="60"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!--
motionSpeed is the minimum distance(in pixel) between the last ACTION_MOVE and ACTION_UP event
motionInterval is the maximum milliseconds between ACTION_DOWN and ACTION_UP event
motionDistance is the minimum distance(in pixel) between ACTION_DOWN and ACTION_UP event
-->
<TextView
android:text="1 / 3\nflip left|right, up|down"
android:textSize="36sp"
android:textAlign="center"
android:background="#6FFF"
android:layout_width="fill_parent"
android:layout_height="300px"
/>
<EditText
android:text="2 / 3\nthis is a EditText which you cannot flip in Spinner"
android:textSize="36sp"
android:textAlign="center"
android:layout_width="fill_parent"
android:layout_height="300px"
/>
<Button
android:text="3 / 3\nthis is a Button"
android:textSize="36sp"
android:textAlign="center"
android:layout_width="fill_parent"
android:layout_height="300px"
/>
</org.mmin.handycalc.HandyFlipper> |
Next section I'll show you how to make this custom widget.
| Description: |
| handyCalc library with samples |
|
 Download |
| Filename: |
handyCalc Library.zip |
| Filesize: |
21.82 KB |
| Downloaded: |
434 Time(s) |
| Description: |
|
 Download |
| Filename: |
HandyFlip.gif |
| Filesize: |
121.43 KB |
| Downloaded: |
309 Time(s) |
|
|
| Back to top |
|
 |
|
|
 |
mmin18 Junior Developer

Joined: 03 Feb 2008 Posts: 20 Location: China
|
Posted: Fri Apr 18, 2008 9:30 am Post subject: |
|
|
This is the java class for HandyFlipper. I hide most of the code just leave the most essential methods to make ViewAnimator flipable.
| Java: | package org. mmin. handycalc;
import ...;
public class HandyFlipper extends ViewAnimator {
// To view the full code, download the archive file http://www.anddev.org/files/handycalc_library_155.zip and import it in eclipse.
MotionEvent motionDown, motionPrev;
boolean motionCanceled;
@Override
public boolean onInterceptTouchEvent (MotionEvent event ) {
if (event. getAction() == MotionEvent. ACTION_DOWN) {
motionDown = event;
motionPrev = event;
motionCanceled = false;
// just record the ACTION_DOWN event and return false. the event will dispatch as a normal way.
return false;
}
if (motionCanceled )
return true; // canceled, then the child no need to process it.
if (event. getAction() == MotionEvent. ACTION_MOVE) {
motionPrev = event;
// just record the ACTION_MOVE event and return false. the event will dispatch as a normal way.
return false;
}
if (event. getAction() == MotionEvent. ACTION_UP) {
if (motionDown == null || motionPrev == null)
return false;
long time = event. getEventTime() - motionDown. getEventTime();
if (time > motionInterval )
return false;
// the event's getX will be modify when deliver to child. so use getRawX instead
float dx = event. getRawX() - motionDown. getRawX();
float absDx = Math. abs(dx );
float dy = event. getRawY() - motionDown. getRawY();
float absDy = Math. abs(dy );
float abs = Math. max(absDx, absDy );
if (abs < motionDistance )
return false;
float v = Math. abs(abs ) * 1000 / time;
if (v < motionSpeed ) {
motionPrev = event;
return false;
} else {
// start to flip. if it return true, the child will receive a event with MotionEvent.ACTION_CANCEL instead of MotionEvent.ACTION_UP
// so make sure your custom widget will handle MotionEvent.ACTION_CANCEL
motionDown = null;
motionPrev = null;
motionCanceled = true;
if (abs == absDx ) {
if (!flipHorizontal )
return false;
if (dx < 0)
return moveRight ();
else
return moveLeft ();
} else {
if (!flipVertical )
return false;
if (dy < 0)
return moveDown ();
else
return moveUp ();
}
}
} else if (event. getAction() == MotionEvent. ACTION_CANCEL) {
motionDown = null;
motionPrev = null;
return false;
}
return false;
}
@Override
public boolean dispatchTouchEvent (MotionEvent ev ) {
super. dispatchTouchEvent(ev );
// always return true to tell the parent the event is processed
return true;
}
@Override
public boolean onTouchEvent (MotionEvent event ) {
this. onInterceptTouchEvent(event );
// always return true to tell the parent the event is processed
return true;
}
}
|
Other codes and resources like attrs and anims can be found in the archive file
|
|
| Back to top |
|
 |
andreleitao Experienced Developer

Joined: 10 Mar 2009 Posts: 55 Location: Recife - Brazil
|
Posted: Thu Mar 26, 2009 9:45 pm Post subject: |
|
|
Nice job mmin18 It looks very nice!
I'll test it!
|
|
| Back to top |
|
 |
pcudb0189 Freshman

Joined: 09 Jun 2009 Posts: 4
|
Posted: Wed Jun 10, 2009 11:41 am Post subject: |
|
|
Hi I download you code
but I have a mistake about the view.ViewInflate
SDK can't be found the view.ViewInflate
Is it the wrong with the different edtion?
|
|
| Back to top |
|
 |
genxsol Junior Developer

Joined: 17 Feb 2009 Posts: 11
|
Posted: Tue Aug 11, 2009 2:56 pm Post subject: view.ViewInflate not resolved |
|
|
| i downloaded the code but there is error view.ViewInflate not resolved
|
|
| Back to top |
|
 |
genxsol Junior Developer

Joined: 17 Feb 2009 Posts: 11
|
Posted: Tue Aug 11, 2009 3:06 pm Post subject: please update the code |
|
|
| can you please upload your latest code for 1.5
|
|
| Back to top |
|
 |
|
|
 |
bennyb Junior Developer

Joined: 23 Sep 2009 Posts: 12
|
Posted: Sat Sep 26, 2009 11:38 pm Post subject: This is garbage ... a waste of time. Does not compile |
|
|
| Don't waste your time
|
|
| Back to top |
|
 |
okless Once Poster

Joined: 16 Dec 2009 Posts: 1 Location: USA
|
Posted: Fri Dec 18, 2009 7:13 pm Post subject: HandyCalc |
|
|
Can you pleeeeese resubmit code that will work with latest of 1.5?
Ohad
|
|
| Back to top |
|
 |
|