как бросить яблоко в корзину с помощью анимации в Android? - PullRequest
0 голосов
/ 24 мая 2018

Мне нужна помощь для анимации заполнения корзины с яблоком в андроиде путем добавления яблока внутрь изображения ведра.Ниже приведен мой код перетаскивания, в котором я перетаскиваю яблоко из одного места в другое.

@Override
    public boolean onDrag(View v, DragEvent event) {
        // Store the action type for the incoming event
        final int action = event.getAction();
        // Handles each of the expected events
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                // Invalidate the view to force a redraw in the new tint
                v.invalidate();

                // Returns true to indicate that the View can accept the
                // dragged data.
                return true;
            case DragEvent.ACTION_DRAG_ENTERED:
                v.invalidate();
                return true;
            case DragEvent.ACTION_DRAG_LOCATION:
                // Ignore the event
                return true;
            case DragEvent.ACTION_DRAG_EXITED:
                v.invalidate();
                return true;
            case DragEvent.ACTION_DROP:
                v.invalidate();
                if (v instanceof ImageView) {
                    ImageView dropView = (ImageView) v;
                    Drawable dropViewDrawable = dropView.getDrawable();
                    Log.v("dropViewDrawable", "" + dropViewDrawable);
                    for (int i = 0; i < numberOfObject; i++) {
                        if (containerImageViewList.get(i).getDrawable() == null) {
                            containerImageViewList.get(i).setImageResource(R.drawable.sp_glass_cut);
                            if (selectedImageColor.equals(getResources().getString(R.string.title_images_color_grey))) {
                                containerImageViewList.get(i).setImageBitmap(CommonMethods.convertColoredImageIntoBlackAndWhiteImage(containerImageViewList.get(i)));
                            }
                            break;
                        }
                    }
                    dragImageView.setImageBitmap(null);
                    successCount++;                        
                } else {

                    CommonMethods.showSweetAlertDialog(SPLevelOneMoveObjectTBActivity.this, false);
                }
                return true;
            case DragEvent.ACTION_DRAG_ENDED:
                v.invalidate();
                Log.v("ACTION_DRAG_ENDED", "ACTION_DRAG_ENDED");
                if (!isSuccess && !isFail) {
                    isFail = true;
                    CommonMethods.showSweetAlertDialog(SPLevelOneMoveObjectTBActivity.this, false);
                    return true;
                }
                return true;
            default:
                break;
        }
        return false;
    }

Теперь, как его анимировать и показать яблоко внутри корзины?

1 Ответ

0 голосов
/ 25 мая 2018

Привет. Вы можете сделать свою корзину с помощью Пустого LinearLayout (или любого другого макета, например RelativeLayout и ConstraintLayout), а затем назначить ему границу следующим образом. res / drawable / basket_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="45dp"
android:bottomRightRadius="45dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#9BA8A3"
android:startColor="#6BA9E8"
android:endColor="#FFFFFF"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="120dp"
/>
<stroke
android:width="5dp"
android:color="#878787"
/>
</shape>

или вы также можете использовать 9-полосное изображение корзины на заднем плане

здесь я использовал TextView и сделал его кругом, а не яблоком, используя файл ниже res / drawable / border_circle.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
  <!--  <gradient
        android:angle="0"
        android:startColor="#f00"
        android:centerColor="@android:color/transparent"
        android:centerX="0.01" />-->
    <stroke android:width="5dp" android:color="#3AFB03" />
    <solid android:color="#FAFB03" />
    <corners android:radius="4dp" />
</shape>

Вы можете использовать9пакетное изображение яблока и ImageView вместо него.

Теперь перейдите к main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >
<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="260dp"
        android:layout_height="98dp"


        android:layout_marginEnd="8dp"

        android:layout_marginStart="8dp"

        android:layout_marginTop="8dp"
        android:background="@drawable/basket_border"
        android:orientation="vertical" >
    </LinearLayout>
   <LinearLayout
android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="108dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="4">

        <TextView
            android:id="@+id/textView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/border_circle"
            android:gravity="center"
            android:text="Text 1"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textColor="@android:color/holo_blue_bright"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="284dp"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/border_circle"
            android:gravity="center"
            android:text="Text 2"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textColor="@android:color/holo_red_dark"
            tools:layout_editor_absoluteX="90dp"
            tools:layout_editor_absoluteY="284dp"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/border_circle"
            android:gravity="center"
            android:text="Text 3"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textColor="@android:color/holo_purple"
            tools:layout_editor_absoluteX="162dp"
            tools:layout_editor_absoluteY="284dp"/>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/border_circle"
            android:gravity="center"
            android:text="Text 4"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            tools:layout_editor_absoluteX="230dp"
            tools:layout_editor_absoluteY="283dp"/>
    </LinearLayout>

</LinearLayout>

, с помощью которого вы можете использовать концепцию перетаскивания, чтобы поместить ImageView (яблоко) вLinearLayout для id linearLayout1, который является корзиной, поэтому здесь вы можете применить обычную анимацию к ImageView (яблоко) при добавлении к LinearLayout (корзина)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...