【问题标题】:how to drop apple inside the basket using animation in android?如何使用android中的动画将苹果放入篮子中?
【发布时间】:2018-05-24 09:23:59
【问题描述】:

我需要帮助,通过将苹果放在桶图像中,在 android 中用苹果填充篮子的动画。下面是我的拖放代码,我将苹果从一个地方拖放到另一个地方。

@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;
    }

现在如何制作动画并显示篮子里的苹果?

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    嗨,您可以在 Empty LinearLayout(或任何其他布局,如 RelativeLayoutConstraintLayout)的帮助下制作您的篮子,然后像这样为其分配边框
    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>
    

    您可以改用 9patch 苹果图片和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(apple) 放入id linearLayout1LinearLayout 中,这样您就可以在ImageView(apple) 上应用普通动画,而添加到LinearLayout(篮子)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-23
      • 2011-03-05
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多