Sunday, January 12, 2014

Bouncy Scrollview

Bouncy Scrollview Like Facebook Scrollview.


Create one class  BounceScrollView.java in src Folder.
- Basically That is custom view and that is extends to ScrollView 




package com.valkesh.utility;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

public class BounceScrollView extends ScrollView {
    private View inner;

    private float y;
    private Rect normal = new Rect();

    private boolean isCount = false;

    public BounceScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        if (getChildCount() > 0) {
            inner = getChildAt(0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (inner != null) {
            commOnTouchEvent(ev);
        }

        return super.onTouchEvent(ev);
    }

    public void commOnTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_UP:
            if (isNeedAnimation()) {
                animation();
                isCount = false;
            }
            break;

        case MotionEvent.ACTION_MOVE:
            final float preY = y;
            float nowY = e.getY();
            int deltaY = (int) (preY - nowY);
            if (!isCount) {
                deltaY = 0;
            }

            y = nowY;
            if (isNeedMove()) {
                if (normal.isEmpty()) {
                    normal.set(inner.getLeft(), inner.getTop(),
                            inner.getRight(), inner.getBottom());
                }
                inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2,
                        inner.getRight(), inner.getBottom() - deltaY / 2);
            }
            isCount = true;
            break;

        default:
            break;
        }
    }

    public void animation() {
        TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
                normal.top);
        ta.setDuration(200);
        inner.startAnimation(ta);
        inner.layout(normal.left, normal.top, normal.right, normal.bottom);

        normal.setEmpty();

    }

    public boolean isNeedAnimation() {
        return !normal.isEmpty();
    }

    public boolean isNeedMove() {
        int offset = inner.getMeasuredHeight() - getHeight();
        int scrollY = getScrollY();
        if (scrollY == 0 || scrollY == offset) {
            return true;
        }
        return false;
    }

}


Now, open your xml file as your view file.


  <com.valkesh.utility.BounceScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

                 Your view

 </com.valkesh.utility.BounceScrollView>


Wednesday, January 8, 2014

Opacity use with any ColorCode in persontage

Opacity with any ColorCode

Ex: Black color and white color with opacity code.

Below code for black:-
Black color [<color name="black">#000000</color>]
White color [<color name="white">#ffffff</color>]
  
Now if i want to use opacity than you can use below code :-
 
60% opacity with black color
<color name="black">#99000000</color>
 
50% opacity with white color 
<color name="white">#80ffffff</color>

YOU CAN USE ANY COLOR WITH OPACITY CODE.

and below for opacity code:-

Hex Opacity Values
100%  FF
95%  F2
90%  E6
85%  D9
80%  CC
75%  BF
70%  B3
65%  A6
60%  99
55%  8C
50%  80
45%  73
40%  66
35%  59
30%  4D
25%  40
20%  33
15%  26
10%  1A
5%  0D
0%  00