Android custom control with translate animation - android

I'm pretty new in the android world.
I try to create in my app a custom control like the following description:
- Objects appears one by one (timer) at the right of that custom controls and are translated (with animation) to the left of the screen.
The rendering is the following right now:
And the code is like:
public abstract class SheetBase : SurfaceView
{
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
this.Update(canvas);
}
}
public class MusicSheet : SheetBase, ISurfaceHolderCallback
{
public MusicSheet(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public MusicSheet(Context context) : base(context)
{
}
public MusicSheet(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public MusicSheet(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
public MusicSheet(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
}
protected override void Update(Canvas canvas)
{
// Logic to draw staf on Canvas
}
public void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)
{
this.Invalidate();
}
public void SurfaceCreated(ISurfaceHolder holder)
{
this._thread = new Thread(this.Loop);
this._thread.Start();
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
this._thread?.Abort();
}
private void Loop()
{
while (true)
{
Canvas c = null;
try
{
c = this.Holder.LockCanvas();
c.DrawColor(Color.Transparent, PorterDuff.Mode.Clear);
// Draw content at the new position
}
finally
{
if (c != null)
this.Holder.UnlockCanvasAndPost(c);
}
Thread.Sleep(10);
}
}
}
What do you think about the solution / performances ?

You can use ViewPropertyAnimator to translate a View.
For example,
view.animate()
.translationX(100) // horizontal translation
.translationY(100) // vertical translation
To animate from right to left:
view.animate()
.translationX( - screenWidth )
Refer the doc for more info.

Related

Xamarin Android Editext wrapped text

I have a multine edittet control and need to get the text of this control with a carriage return. The problem is that i do not know where the text is wrapped.
Is ther a possibility to get the text formatted as seen in the control?
Or is there a OnWrapped event so that i can append then the /r/n on my own?
Thank you!
I found a dirty possibility which does nearly what I need
public sealed class CustomEditText : EditText
{
private int _prevLineCounter = 1;
public static bool DelIsPressed { get; set; }
private CustomEditText(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public CustomEditText(Context context) : base(context)
{
BeforeTextChanged += CustomEditText_BeforeTextChanged;
SetOnKeyListener(new KeyListner());
}
private void CustomEditText_BeforeTextChanged(object sender, TextChangedEventArgs e)
{
// not loaded yet
if (LineCount == 0)
return;
if (DelIsPressed)
{
DelIsPressed = false;
return;
}
// text got wrapped
if (_prevLineCounter < LineCount)
{
_prevLineCounter = LineCount;
// find last space an enter carriage return
int spaceIndex = Text.LastIndexOf(' ');
// just one long string
if (spaceIndex == -1)
spaceIndex = Text.Length - 2;
Text = Text.Insert(spaceIndex, "\r\n");
// set cursor to the end
SetSelection(Text.Length);
}
_prevLineCounter = LineCount;
}
public CustomEditText(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public CustomEditText(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs,
defStyleAttr)
{
}
public CustomEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context,
attrs, defStyleAttr, defStyleRes)
{
}
}
public class KeyListner: Java.Lang.Object, View.IOnKeyListener
{
public bool OnKey(View v, Keycode keyCode, KeyEvent e)
{
CustomEditText.DelIsPressed = e.KeyCode == Keycode.Del;
return false;
}
}

ClearableEdittext for xamarin android

I have visited this link and many other links on the stack but I'm unable to find a similar solution for xamarin android :
https://stackoverflow.com/a/14470930/7462031
I have implemented the frame layout solution but I want this solution throughout my application So the Clearable Edittext Looked interesting but the droid parts library is not available for xamarin so I am wondering if anyone has a solution for this problem in case you do kindly Help.!
I achieved it by doing the following it might not be the best solution but I guess it is the only solution for Xamarin.Android available for this:
public class ClearableEditext : EditText
{
Context mContext;
Drawable imgX;
public ClearableEditext(Context context) : base(context)
{
init(context, null);
}
public ClearableEditext(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
init(context, attrs);
}
public ClearableEditext(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
init(context, attrs);
}
public ClearableEditext(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
init(context, attrs);
}
public void init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
imgX = ContextCompat.GetDrawable(ctx, Android.Resource.Drawable.PresenceOffline);
imgX.SetBounds(0, 0, imgX.IntrinsicWidth, imgX.IntrinsicHeight);
manageClearButton();
this.SetOnTouchListener(new TouchHelper(this, imgX));
this.AddTextChangedListener(new TextListener(this));
}
public void manageClearButton()
{
if (this.Text.ToString().Equals(""))
removeClearButton();
else
addClearButton();
}
public void addClearButton()
{
this.SetCompoundDrawables(this.GetCompoundDrawables()[0],
this.GetCompoundDrawables()[1],
imgX,
this.GetCompoundDrawables()[3]);
}
public void removeClearButton()
{
this.SetCompoundDrawables(this.GetCompoundDrawables()[0],
this.GetCompoundDrawables()[1],
null,
this.GetCompoundDrawables()[3]);
}
}
public class TouchHelper : Java.Lang.Object, View.IOnTouchListener
{
ClearableEditext Editext;
public ClearableEditext objClearable { get; set; }
Drawable imgX;
public TouchHelper(ClearableEditext editext, Drawable imgx)
{
Editext = editext;
objClearable = objClearable;
imgX = imgx;
}
public bool OnTouch(View v, MotionEvent e)
{
ClearableEditext et = Editext;
if (et.GetCompoundDrawables()[2] == null)
return false;
// Only do this for up touches
if (e.Action != MotionEventActions.Up)
return false;
// Is touch on our clear button?
if (e.GetX() > et.Width - et.PaddingRight - imgX.IntrinsicWidth)
{
Editext.Text = string.Empty;
if (objClearable != null)
objClearable.removeClearButton();
}
return false;
}
}
public class TextListener : Java.Lang.Object, ITextWatcher
{
public ClearableEditext objClearable { get; set; }
public TextListener(ClearableEditext objRef)
{
objClearable = objRef;
}
public void AfterTextChanged(IEditable s)
{
}
public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
{
}
public void OnTextChanged(ICharSequence s, int start, int before, int count)
{
if (objClearable != null)
objClearable.manageClearButton();
}
}
To change the x icon as your custom one change the image in init()

An always expanded MvxExpandableListView

I'm trying to develop a version of the MvxExpandableListView that would always be expanded.
The code that I wrote seems to work, but I'm pretty sure I don't do things the right way. I tried to find some info about Android Widgets lifecycle, but found only info about Activity lifecycle.
More specifically, where should the ExpandAll() method be called ?
I've tried several places and ended by putting it into the OnDraw() handler.
To prevent collapsing, I called the ExpandGroup() method in the GroupCollapse Handler. hmm not sure it's the right way to do it either.
So, here is the code :
public class MvxExpandedListView : MvxExpandableListView
{
private bool _allExpanded = false;
private void ExpandAll()
{
var nbGroups = ThisAdapter.GroupCount;
for (int i = 0; i < nbGroups; i++)
{
ExpandGroup(i);
}
}
private void Init()
{
GroupCollapse += MvxExpandableListView_GroupCollapse;
SetSelector(Android.Resource.Color.Transparent);
CacheColorHint = Color.Transparent;
SetGroupIndicator(null);
}
public MvxExpandedListView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
Init();
}
public MvxExpandedListView(Context context, IAttributeSet attrs, MvxExpandableListAdapter adapter)
: base(context, attrs, adapter)
{
Init();
}
protected MvxExpandedListView(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
Init();
}
void MvxExpandableListView_GroupCollapse(object sender, ExpandableListView.GroupCollapseEventArgs e)
{
// There must be a better way...
this.ExpandGroup(e.GroupPosition);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
// Ugly !
if (!_allExpanded)
{
ExpandAll();
_allExpanded = true;
}
}
public override bool CollapseGroup(int groupPosition)
{
return false;
}
}
Thanks in advance for your help,
Kindanam

Android Remove Badge Image Triangle

is there any way to remove corner triangle of QuickContactBadge image ?
XML :
<QuickContactBadge
android:id="#+id/quickContactBadge1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"/>
Use :
setOverlay(null);
If you go to QuickContactBadge.java implementation in on draw function you will find :
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isEnabled()) {
// not clickable? don't show triangle
return;
}
if (mOverlay == null || mOverlay.getIntrinsicWidth() == 0 ||
mOverlay.getIntrinsicHeight() == 0) {
// nothing to draw
return;
}
...
finally just override QuickContactBadge class, but need to draw image in another view
public class CustomBadgeImage extends QuickContactBadge {
public CustomBadgeImage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomBadgeImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomBadgeImage(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
return;
}
}

Detect end of fling on ScrollView

I've overridden ScrollView to pass MotionEvents to a GestureDetector to detect fling events on the ScrollView. I need to be able to detect when the scrolling stops. This doesn't coincide with the MotionEvent.ACTION_UP event because this usually happens at the start of a fling gesture, which is followed by a flurry of onScrollChanged() calls on the ScrollView.
So basically what we are dealing with here is the following events:
onFling
onScrollChanged, onScrollChanged, onScrollChanged, ... , onScrollChanged
There's no callback for when the onScrollChanged events are done firing. I was thinking of posting a message to the event queue using a Handler during onFling and waiting for the Runnable to execute to signal the end of the fling, unfortunately it fires after the first onScrollChanged call.
Any other ideas?
I've combined a few of the answers from here to construct a working listener that resembles the way AbsListView does it. It's essentially what you describe, and it works well in my testing.
Note: you can simply override ScrollView.fling(int velocityY) rather than use your own GestureDetector.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
private static final int DELAY_MILLIS = 100;
public interface OnFlingListener {
public void onFlingStarted();
public void onFlingStopped();
}
private OnFlingListener mFlingListener;
private Runnable mScrollChecker;
private int mPreviousPosition;
public CustomScrollView(Context context) {
this(context, null, 0);
}
public CustomScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScrollChecker = new Runnable() {
#Override
public void run() {
int position = getScrollY();
if (mPreviousPosition - position == 0) {
mFlingListener.onFlingStopped();
removeCallbacks(mScrollChecker);
} else {
mPreviousPosition = getScrollY();
postDelayed(mScrollChecker, DELAY_MILLIS);
}
}
};
}
#Override
public void fling(int velocityY) {
super.fling(velocityY);
if (mFlingListener != null) {
mFlingListener.onFlingStarted();
post(mScrollChecker);
}
}
public OnFlingListener getOnFlingListener() {
return mFlingListener;
}
public void setOnFlingListener(OnFlingListener mOnFlingListener) {
this.mFlingListener = mOnFlingListener;
}
}
Thank you #PaulBurke +1
Xamarin Solution
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;
public class CustomScrollView : ScrollView
{
public event EventHandler FlingEnded;
public event EventHandler FlingStarted;
private Action ScrollChecker;
private int PreviousPosition;
private const int DELAY_MILLIS = 100;
public CustomScrollView(Context context) : base(context) => Init();
public CustomScrollView(Context context, IAttributeSet attrs) : base(context, attrs) => Init();
public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) => Init();
public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) => Init();
public CustomScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }
private void Init()
{
ScrollChecker = () =>
{
int position = ScrollY;
if (PreviousPosition - position == 0)
{
FlingEnded?.Invoke(this, new EventArgs());
RemoveCallbacks(ScrollChecker);
}
else
{
PreviousPosition = ScrollY;
PostDelayed(ScrollChecker, DELAY_MILLIS);
}
};
}
public override void Fling(int velocityY)
{
base.Fling(velocityY);
FlingStarted?.Invoke(this, new EventArgs());
Post(ScrollChecker);
}
}
Usage:
myCustomScrollView.FlingEnded += myCustomScrollView_FlingEnded;
protected void myCustomScrollView_FlingEnded(object sender, EventArgs e) =>
{
//Do onFlingEnded code here
};

Categories

Resources