Tuesday, December 24, 2013

Merry Christmas

I wish all my readers a merry christmas, some relaxing holidays and a happy new year.
Let us hope that as many people as possible around the world can enjoy these days.

private void Form1_Paint(object sender, PaintEventArgs e)
{
    System.Drawing.Drawing2D.GraphicsPath GP = new System.Drawing.Drawing2D.GraphicsPath();
    GP.AddArc(50, 50, 40, 40, 180, 180);
    GP.AddArc(90, 50, 40, 40, 180, 180);
    GP.AddLine(130, 70, 90, 120);
    GP.AddLine(90, 120, 50, 70);
    e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red), 3), GP);
}

As this blog is visited by people from many nationalities, I will write something in the languages of the 5 countries with the most visitors next to English speaking countries and Germany, hope that makes you feel a bit home (please do not laugh):

  1. मेरी क्रिसमस
  2. 메리 크리스마스
  3. Срећан Божић
  4. 圣诞节快乐
  5. З Різдвом

Friday, December 20, 2013

Determine Decimal Separator

Often it can be of advantage or even neccessary to determine the decimal separator of the current system or current user, for example to convert inputted numbers to the right format. In C# we can achieve this information with the class CultureInfo:

string DecimalSeparator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;

Thursday, December 19, 2013

Android: Determine Decimal Separator

In many applications you probably want to display numbers in the correct format of the user or read-in numbers.
Since Visual Studio and Xamarin use as a decimal separator the decimal point ("."), for this maybe a manual conversion has to be done, depending on the settings of the user.
Therefore I post here 2 lines of code, with which the decimal separator of the system can be determined:

Java.Text.DecimalFormatSymbols FormatSymbols = new Java.Text.DecimalFormatSymbols();
char DecimalSeparator = FormatSymbols.DecimalSeparator;

Monday, December 16, 2013

Android: Combine Animations

After I explained basics about animations in Android in the previous post I want to show today how multiple animations can be combined using an AnimatorSet, playing them simultaneously or sequentially.
The foundation is nearly identical to the one of the previous post, just this time we have 2 TextViews which are to be animated when the button is clicked:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Android.Animation;

namespace AnimationsExample
{
     [Activity (Label = "AnimationsExample", MainLauncher = true)]
     public class MainActivity : Activity
     {
          protected override void OnCreate (Bundle bundle)
          {
               base.OnCreate (bundle);

               LinearLayout Layout = new LinearLayout (this);
               Layout.Orientation = Orientation.Vertical;

               TextView TxtView1 = new TextView (this);
               TxtView1.Text = "I'm flying";
               Layout.AddView (TxtView1);

               TextView TxtView2 = new TextView (this);
               TxtView2.Text = "I'm flying 2";
               Layout.AddView (TxtView2);

               Button Btn1 = new Button (this);
               Btn1.Text = "Animation";
               Layout.AddView (Btn1);

               Btn1.Click += (object sender, EventArgs e) => {
                    // Animation
               };

               SetContentView (Layout);
          }
     }
}

Now we first create, as described in the previous post, 2 animations of the type ObjectAnimator, which move one TextView to the right:

ObjectAnimator Animator1 = ObjectAnimator.OfInt(TxtView1, "Left", 0, 100);
Animator1.SetDuration(1000);
ObjectAnimator Animator2 = ObjectAnimator.OfInt(TxtView2, "Left", 0, 100);
Animator2.SetDuration(1000);

To justify the title now an object of the type AnimatorSet comes into play. From this we first call the method Play(), which expects an animation as parameter. Special about AnimationSets is now, that further animations can be called. We either append them by calling the function With() or Before(), the animations are then played together with or before the first animation. As for single animations here also Start() has to be called eventually. The following code first moves the upper TextView to the right, then the lower:

ObjectAnimator Animator1 = ObjectAnimator.OfInt(TxtView1, "Left", 0, 100);
Animator1.SetDuration(1000);
ObjectAnimator Animator2 = ObjectAnimator.OfInt(TxtView2, "Left", 0, 100);
Animator2.SetDuration(1000);
AnimatorSet Combined = new AnimatorSet();
Combined.Play(Animator1).Before(Animator2);
Combined.Start();

Note here, that only 2 time levels are available, the call Play(A1).Before(A2).Before(A3) would thus play both the animations A2 and A3 together with animation A1. If the nesting is to be deeper, multiple AnimatorSets have to be used or the function PlaySequentially() called. This expects a list of elements of the type Animator (so single animations as well as complete AnimatorSets can be added) and then plays them after each other.
If one wants to order multiple animations, furthermore the function PlayTogether() is useful, which also expects a list of animations, which is then played at the same time.

Saturday, December 14, 2013

Android: Insert Animations

In today's post I want to describe, how to get some movement in your apps by using animations.
When programming for Android, there are basically 3 possibilites: There are View Animations, Drawable Animations and Property Animations. In this post I only want to cover the last class, which was introduced last (in Android 3.0) and therefor also provides the most possibilites and is generally used.
Of Property Animations there are again 3 subclasses: ValueAnimator, ObjectAnimator and AnimationSet. AnimatorSet is used for connecting animations, I will dedicate an own post to this class soon.
With the ValueAnimator arbitrary values can be changed over time. In this post though I will only describe the usage of the subclass ObjectAnimator, which was particularly designed for animating objects, which should be sufficient for most applications.
Now directly to the code. In the example we create a LinearLayout, in which a TextView and a Button are aligned below each other, and in which the TextView is moved to the right via an animation when the Button is clicked.
For animations we need the namespace Android.Animation, which we include via using. Then we create the Animator from the class ObjectAnimator and choose, whether we want to change an integer or float value. To this creation method we pass over the name of the object we want to animate, the name of the target value as a string, as well as start and end value. The following lines change the property Left of the object TxtView1 from 0 to 100, thus moving the object to the right. In the second line we set the duration of the animation, and in the third we eventually start it:

ObjectAnimator Animator = ObjectAnimator.OfInt(TxtView1, "Left", 0, 100);
Animator.SetDuration(1000);
Animator.Start();

The complete code is:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Android.Animation;

namespace AnimationsExample
{
     [Activity (Label = "AnimationsExample", MainLauncher = true)]
     public class MainActivity : Activity
     {
          protected override void OnCreate (Bundle bundle)
          {
               base.OnCreate (bundle);

               LinearLayout Layout = new LinearLayout (this);
               Layout.Orientation = Orientation.Vertical;

               TextView TxtView1 = new TextView (this);
               TxtView1.Text = "I'm flying";
               Layout.AddView (TxtView1);

               Button Btn1 = new Button (this);
               Btn1.Text = "Animation";
               Layout.AddView (Btn1);

               Btn1.Click += (object sender, EventArgs e) => {
                    ObjectAnimator Animator = ObjectAnimator.OfInt(TxtView1, "Left", 0, 100);
                    Animator.SetDuration(1000);
                    Animator.Start();
               };

               SetContentView (Layout);
          }
     }
}