Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

draw an icon in between swiped area android

public static final float ALPHA_FULL = 1.0f;

public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        // Get RecyclerView item from the ViewHolder
        View itemView = viewHolder.itemView;

        Paint p = new Paint();
        Bitmap icon;

        if (dX > 0) {
            /* Note, ApplicationManager is a helper class I created 
               myself to get a context outside an Activity class - 
               feel free to use your own method */

            icon = BitmapFactory.decodeResource(
                    ApplicationManager.getContext().getResources(), R.drawable.myleftdrawable);

            /* Set your color for positive displacement */
            p.setARGB(255, 255, 0, 0);

            // Draw Rect with varying right side, equal to displacement dX
            c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
                    (float) itemView.getBottom(), p);

            // Set the image icon for Right swipe
            c.drawBitmap(icon,
                    (float) itemView.getLeft() + convertDpToPx(16),
                    (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight())/2,
                    p);
        } else {
            icon = BitmapFactory.decodeResource(
                    ApplicationManager.getContext().getResources(), R.drawable.myrightdrawable);

            /* Set your color for negative displacement */
            p.setARGB(255, 0, 255, 0);

            // Draw Rect with varying left side, equal to the item's right side
            // plus negative displacement dX
            c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
                    (float) itemView.getRight(), (float) itemView.getBottom(), p);

            //Set the image icon for Left swipe
            c.drawBitmap(icon,
                    (float) itemView.getRight() - convertDpToPx(16) - icon.getWidth(),
                    (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight())/2,
                    p);
        }

        // Fade out the view as it is swiped out of the parent's bounds
        final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);

    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

private int convertDpToPx(int dp){
    return Math.round(dp * (getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
Comment

PREVIOUS NEXT
Code Example
Java :: android conditional api level 
Java :: is overriding only works with inherited methods? 
Java :: missingWords 
Java :: Uri/Beecrowd problem no - 1150 solution in Java 
Java :: fibonacci for biginteger 
Java :: how to print a sentence out in a string of camel case java 
Java :: thymeleaf Expression Object dialects 
Java :: delete row entity jpa java 
Java :: When different programmers write the same program in differing ways and all get the correct result. what is that known as? 
Java :: java singly linked list example 2 res 
Java :: Append Line Separator In StringBuilder 
Java :: findbyname in jpa 
Java :: Java @Target 
Java :: bytecode into source code 
Java :: exmple of methods in java 
Java :: Create hashmap from another maps java 
Java :: check is element present in queue java 
Java :: How to disable special characters on keyboard in android 
Java :: play default message ringtone android studio 
Java :: Java List Replace at Index using set() function 
Java :: /= java 
Java :: import class java 
Java :: java method exercises 
Java :: how to call a void method from another class in java 
Java :: add integers java 
Java :: @runwith junit 5 
Java :: polimorfismo java ejemplo 
Java :: java 8 list to map with occurrences 
Sql :: foreign key set 0 
Sql :: postgresql update sequence next value 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =