You can also check out this GitHub repository: https://github.com/ResoCoder/DynamicAnimationXamarinAndroid
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using Android.Support.Animation;
using Android.Util;
namespace DynamicAnimationTut
{
[Activity(Label = "DynamicAnimationTut", MainLauncher = true)]
public class MainActivity : Activity
{
View viewSpring;
View viewFling;
bool shouldFlingUpwards = true;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
viewSpring = FindViewById<View>(Resource.Id.view_spring);
viewFling = FindViewById<View>(Resource.Id.view_fling);
viewSpring.Click += (o, e) =>
{
SpringAnimation springAnim = new SpringAnimation(viewSpring, DynamicAnimation.TranslationY, 0);
springAnim.Spring.SetStiffness(SpringForce.StiffnessLow);
springAnim.Spring.SetDampingRatio(SpringForce.DampingRatioHighBouncy);
springAnim.SetStartVelocity(DpToPx(-2000));
springAnim.Start();
};
viewFling.Click += (o, e) =>
{
FlingAnimation flingAnim = new FlingAnimation(viewFling, DynamicAnimation.TranslationY);
flingAnim.SetStartVelocity(DpToPx(shouldFlingUpwards ? -1500 : 1500));
//flingAnim.SetFriction(2);
flingAnim.Start();
shouldFlingUpwards = !shouldFlingUpwards;
};
}
private float DpToPx(float dp)
{
return TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, Resources.DisplayMetrics);
}
}
}
|
Main.axml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view_spring"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="70dp"
android:layout_centerVertical="true"
android:background="#F44336" />
<View
android:id="@+id/view_fling"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="70dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/view_spring"
android:background="#2196F3" />
</RelativeLayout>
|



0 comments:
Post a Comment
Note: only a member of this blog may post a comment.